Skip to content

test(agent): fix TestAgent_Metadata/Once flake #8613

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

Merged
merged 11 commits into from
Jul 20, 2023
Prev Previous commit
Next Next commit
fixup! more debug logging
  • Loading branch information
coadler committed Jul 20, 2023
commit 07a5b9ab940ea7e813f2e866d1fc23c40290b5f5
21 changes: 15 additions & 6 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,17 +298,26 @@ type metadataResultAndKey struct {
}

type trySingleflight struct {
m sync.Map
mu sync.Mutex
m map[string]struct{}
}

func (t *trySingleflight) Do(key string, fn func()) {
_, loaded := t.m.LoadOrStore(key, struct{}{})
if !loaded {
// There is already a goroutine running for this key.
t.mu.Lock()
_, ok := t.m[key]
if ok {
t.mu.Unlock()
return
}

defer t.m.Delete(key)
t.m[key] = struct{}{}
t.mu.Unlock()
defer func() {
t.mu.Lock()
delete(t.m, key)
t.mu.Unlock()
}()

fn()
}

Expand All @@ -328,7 +337,7 @@ func (a *agent) reportMetadataLoop(ctx context.Context) {
// a goroutine running for a given key. This is to prevent a build-up of
// goroutines waiting on Do when the script takes many multiples of
// baseInterval to run.
var flight trySingleflight
var flight = trySingleflight{m: map[string]struct{}{}}

for {
var delay time.Duration
Expand Down