Skip to content

Commit 5b62aeb

Browse files
committed
docs: add explain
1 parent 636cd83 commit 5b62aeb

File tree

3 files changed

+6
-4
lines changed

3 files changed

+6
-4
lines changed

gmicro/registry/consul/registry.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,14 +231,15 @@ func (r *Registry) resolve(ctx context.Context, ss *serviceSet) error {
231231
}
232232

233233
go func() {
234+
// 长轮询的思想: 如果服务有变化, 则立即返回, 否则等待一段时间后返回, 以减少请求次数
234235
ticker := time.NewTicker(time.Second)
235236
defer ticker.Stop()
236237
for {
237238
select {
238239
case <-ticker.C:
239-
timeoutCtx, cancel := context.WithTimeout(context.Background(), r.timeout)
240-
tmpService, tmpIdx, err := r.cli.Service(timeoutCtx, ss.serviceName, idx, true)
241-
cancel()
240+
timeoutContext, c := context.WithTimeout(context.Background(), r.timeout)
241+
tmpService, tmpIdx, err := r.cli.Service(timeoutContext, ss.serviceName, idx, true)
242+
c()
242243
if err != nil {
243244
time.Sleep(time.Second)
244245
continue

gmicro/server/rpcserver/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ func dial(ctx context.Context, insecure bool, opts ...ClientOption) (*grpc.Clien
130130
grpc.WithChainStreamInterceptor(streamInts...),
131131
}
132132

133-
//服务发现的选项
133+
//服务发现的选项 这里调用 resolver 的直连模式或者是服务发现模式
134134
if &options.discovery != nil {
135135
grpcOpts = append(grpcOpts, grpc.WithResolvers(
136136
discovery.NewBuilder(options.discovery,

gmicro/server/rpcserver/resolver/discovery/resolver.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ type discoveryResolver struct {
2626
}
2727

2828
func (r *discoveryResolver) watch() {
29+
// 轮询监听
2930
for {
3031
select {
3132
case <-r.ctx.Done():

0 commit comments

Comments
 (0)