fix(registry/consul): fix concurrency issues and improve performance #3511
+427
−109
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
修复了一些并发相关的问题:
Registry
锁的范围太大,比如Watch
对整个函数加锁,不能同时处理多个(不同的)Service;kratos/contrib/registry/consul/registry.go
Lines 183 to 185 in 5087366
resolve
,只要Watch
就会新开一个resolve
协程;kratos/contrib/registry/consul/registry.go
Lines 211 to 213 in 5087366
Client
注销任一 Service 都会直接cancel
掉Client.ctx
,导致所有的heartbeat
协程退出;kratos/contrib/registry/consul/client.go
Lines 246 to 249 in 5087366
Watch
时,由于w
先被添加到set
中,然后才会写入services event
,如果在两步中间正好遇到之前的resolve
在broadcast
,209行的写入则会被卡住(w
还没有返回,不会有调用者主动调用Next
消费)。kratos/contrib/registry/consul/registry.go
Lines 196 to 214 in 5087366
ctx.Err()
避免做冗长的无用功、sleepCtx
等。