Skip to content

Commit

Permalink
fixed bug of the grpc builder
Browse files Browse the repository at this point in the history
  • Loading branch information
dobyte committed Oct 25, 2024
1 parent 63ee59e commit e039f33
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 12 deletions.
14 changes: 4 additions & 10 deletions transport/grpc/internal/resolver/direct/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"google.golang.org/grpc/resolver"
"net"
"sync"
"sync/atomic"
"time"
)

Expand All @@ -23,7 +22,6 @@ type Builder struct {
ctx context.Context
cancel context.CancelFunc
watcher registry.Watcher
state atomic.Bool
rw sync.RWMutex
addresses map[string]string
}
Expand All @@ -36,17 +34,17 @@ func NewBuilder(dis registry.Discovery) *Builder {
b.ctx, b.cancel = context.WithCancel(context.Background())
b.addresses = make(map[string]string)

if err := b.init(); err != nil {
log.Fatalf("init client builder failed: %v", err)
}

return b
}

func (b *Builder) Build(target resolver.Target, cc resolver.ClientConn, opts resolver.BuildOptions) (resolver.Resolver, error) {
addr := target.URL.Host

if _, _, err := net.SplitHostPort(target.URL.Host); err != nil {
if err = b.init(); err != nil {
return nil, err
}

b.rw.RLock()
address, ok := b.addresses[target.URL.Host]
b.rw.RUnlock()
Expand All @@ -70,10 +68,6 @@ func (b *Builder) Scheme() string {

func (b *Builder) init() error {
if b.dis == nil {
return errors.ErrMissDiscovery
}

if b.state.CompareAndSwap(false, true) == true {
return nil
}

Expand Down
4 changes: 2 additions & 2 deletions transport/grpc/internal/resolver/discovery/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ func NewBuilder(dis registry.Discovery) *Builder {
log.Fatalf("init client builder failed: %v", err)
}

go b.watch()

return b
}

Expand Down Expand Up @@ -87,6 +85,8 @@ func (b *Builder) init() error {
b.watcher = watcher
b.updateInstances(services)

go b.watch()

return nil
}

Expand Down

0 comments on commit e039f33

Please sign in to comment.