1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: Apache-2.0
12 type throttleEnt struct {
13 last time.Time // last attempt that was allowed
16 type throttle struct {
18 seen map[string]*throttleEnt
24 // Check checks whether there have been too many recent attempts with
25 // the given uuid, and returns true if it's OK to attempt [again] now.
26 func (t *throttle) Check(uuid string) bool {
30 t.setupOnce.Do(t.setup)
32 defer t.updated.Broadcast()
34 ent, ok := t.seen[uuid]
36 t.seen[uuid] = &throttleEnt{last: time.Now()}
39 if time.Since(ent.last) < t.hold {
46 func (t *throttle) setup() {
47 t.seen = make(map[string]*throttleEnt)
50 for range time.NewTicker(t.hold).C {
52 for uuid, ent := range t.seen {
53 if time.Since(ent.last) >= t.hold {
57 // don't bother cleaning again until the next update