Skip to content

Commit

Permalink
fix: concurrent issue on updateMetaDataMs (#2522)
Browse files Browse the repository at this point in the history
Signed-off-by: napallday <[email protected]>
  • Loading branch information
napallday authored Jul 25, 2023
1 parent e07f521 commit ecf43f4
Showing 1 changed file with 5 additions and 8 deletions.
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

0 comments on commit ecf43f4

Please sign in to comment.