Skip to content

Commit c2bdf24

Browse files
authored
client: remove the write operation on *registry.Node in LoadNodeConfig to avoid data race during selecting Node (#138)
cherry-pick from internal code within the company
1 parent cc272f5 commit c2bdf24

File tree

4 files changed

+12
-5
lines changed

4 files changed

+12
-5
lines changed

client/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ func selectorFilter(ctx context.Context, req interface{}, rsp interface{}, next
393393
if err != nil {
394394
return OptionsFromContext(ctx).fixTimeout(err)
395395
}
396-
ensureMsgRemoteAddr(msg, node.Network, node.Address)
396+
ensureMsgRemoteAddr(msg, findFirstNonEmpty(node.Network, opts.Network), node.Address)
397397

398398
// Start to process the next filter and report.
399399
begin := time.Now()

client/client_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ func TestClientFail(t *testing.T) {
182182
client.WithSelectorNode(node), client.WithProtocol("fake")))
183183
require.Equal(t, node.Address, "127.0.0.1:8080")
184184
require.Equal(t, node.ServiceName, "127.0.0.1:8080")
185-
require.Equal(t, node.Network, "tcp")
185+
require.Empty(t, node.Network)
186186

187187
// test encode failure
188188
reqBody = &codec.Body{Data: []byte("failbody")}

client/options.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -695,8 +695,6 @@ func (opts *Options) LoadNodeConfig(node *registry.Node) {
695695
if node.Network != "" {
696696
opts.Network = node.Network
697697
opts.CallOptions = append(opts.CallOptions, transport.WithDialNetwork(node.Network))
698-
} else {
699-
node.Network = opts.Network
700698
}
701699
if node.Protocol != "" {
702700
WithProtocol(node.Protocol)(opts)

client/stream.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ func (s *stream) Init(ctx context.Context, opt ...Option) (*Options, error) {
154154
report.SelectNodeFail.Incr()
155155
return nil, err
156156
}
157-
ensureMsgRemoteAddr(msg, node.Network, node.Address)
157+
ensureMsgRemoteAddr(msg, findFirstNonEmpty(node.Network, opts.Network), node.Address)
158158
const invalidCost = -1
159159
opts.Node.set(node, node.Address, invalidCost)
160160
if opts.Codec == nil {
@@ -166,6 +166,15 @@ func (s *stream) Init(ctx context.Context, opt ...Option) (*Options, error) {
166166
return s.opts, nil
167167
}
168168

169+
func findFirstNonEmpty(ss ...string) string {
170+
for _, s := range ss {
171+
if s != "" {
172+
return s
173+
}
174+
}
175+
return ""
176+
}
177+
169178
// Invoke implements Stream.
170179
func (s *stream) Invoke(ctx context.Context) error {
171180
return s.opts.StreamTransport.Init(ctx, s.opts.CallOptions...)

0 commit comments

Comments
 (0)