Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: concurrent issue on updateMetadataMs #2522

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,10 @@ const (
)

type client struct {
// updateMetaDataMs stores the time at which metadata was lasted updated.
// updateMetadataMs stores the time at which metadata was lasted updated.
// Note: this accessed atomically so must be the first word in the struct
// as per golang/go#41970
updateMetaDataMs int64
updateMetadataMs int64

conf *Config
closer, closed chan none // for shutting down background metadata updater
Expand Down Expand Up @@ -975,8 +975,8 @@ func (client *client) tryRefreshMetadata(topics []string, attemptsRemaining int,
time.Sleep(backoff)
}

t := atomic.LoadInt64(&client.updateMetaDataMs)
if time.Since(time.Unix(t/1e3, 0)) < backoff {
t := atomic.LoadInt64(&client.updateMetadataMs)
if time.Since(time.UnixMilli(t)) < backoff {
return err
}
attemptsRemaining--
Expand All @@ -1000,10 +1000,7 @@ func (client *client) tryRefreshMetadata(topics []string, attemptsRemaining int,

req := NewMetadataRequest(client.conf.Version, topics)
req.AllowAutoTopicCreation = allowAutoTopicCreation
t := atomic.LoadInt64(&client.updateMetaDataMs)
if !atomic.CompareAndSwapInt64(&client.updateMetaDataMs, t, time.Now().UnixNano()/int64(time.Millisecond)) {
return nil
}
atomic.StoreInt64(&client.updateMetadataMs, time.Now().UnixMilli())

response, err := broker.GetMetadata(req)
var kerror KError
Expand Down