@@ -2,7 +2,6 @@ package grpcc_resolver
22
33import (
44 "context"
5- "fmt"
65 "sync"
76
87 "github.com/kr/pretty"
@@ -42,11 +41,6 @@ func (d *discovBuilder) updateService(services ...*registry.Service) {
4241 // 更新服务信息
4342 for j := 0 ; j < Replica ; j ++ {
4443 addr := n .Address
45- // 如果port不存在, 那么addr中包含port
46- //if !strings.Contains(n.Address, ":") {
47- addr = fmt .Sprintf ("%s:%d" , "localhost" , n .Port )
48- //}
49-
5044 res := newAddr (addr , services [i ].Name )
5145 val , ok := d .services .LoadOrStore (getServiceUniqueId (n .Id , j ), & res )
5246 if ok {
@@ -104,40 +98,41 @@ func (d *discovBuilder) Build(target resolver.Target, cc resolver.ClientConn, op
10498 w , err := r .Watch (srv )
10599 xerror .PanicF (err , "target.Endpoint: %s" , srv )
106100
107- cancel := syncx .GoCtx (func (ctx context.Context ) {
108- defer func () { xerror .Panic (w .Stop ()) }()
101+ return & baseResolver {
102+ cancel : syncx .GoCtx (func (ctx context.Context ) {
103+ defer func () { xerror .Panic (w .Stop ()) }()
109104
110- for {
111- select {
112- case <- ctx .Done ():
113- return
114- default :
115- res , err := w .Next ()
116- if err == registry .ErrWatcherStopped {
105+ for {
106+ select {
107+ case <- ctx .Done ():
117108 return
109+ default :
110+ res , err := w .Next ()
111+ if err == registry .ErrWatcherStopped {
112+ return
113+ }
114+
115+ if err != nil {
116+ logs .WithErr (err ).Error ("error" )
117+ continue
118+ }
119+
120+ // 注册中心删除服务
121+ if res .Action == event .EventType_DELETE {
122+ d .delService (res .Service )
123+ } else {
124+ d .updateService (res .Service )
125+ }
126+
127+ xerror .TryCatch (func () (interface {}, error ) {
128+ var addrList = d .getAddrList (srv )
129+ return nil , cc .UpdateState (newState (addrList ))
130+ }, func (err error ) {
131+ logs .WithErr (err ).Error ("update resolver address error" )
132+ })
118133 }
119-
120- if err != nil {
121- logs .WithErr (err ).Error ("error" )
122- continue
123- }
124-
125- // 注册中心删除服务
126- if res .Action == event .EventType_DELETE {
127- d .delService (res .Service )
128- } else {
129- d .updateService (res .Service )
130- }
131-
132- xerror .TryCatch (func () (interface {}, error ) {
133- var addrList = d .getAddrList (srv )
134- return nil , cc .UpdateState (newState (addrList ))
135- }, func (err error ) {
136- logs .WithErr (err ).Error ("update resolver address error" )
137- })
138134 }
139- }
140- })
141-
142- return & baseResolver {cancel : cancel , builder : DiscovScheme }, nil
135+ }),
136+ builder : DiscovScheme ,
137+ }, nil
143138}
0 commit comments