Skip to content

Commit

Permalink
fix: concurrent issue on updateMetaDataMs (IBM#2522)
Browse files Browse the repository at this point in the history
Signed-off-by: napallday <[email protected]>
Signed-off-by: Ioan Zicu <[email protected]>
  • Loading branch information
napallday authored and Ioan Zicu committed Jul 31, 2023
1 parent cb2e368 commit 0e2f1d5
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 0e2f1d5

Please sign in to comment.