| 1 | package jobs |
| 2 | |
| 3 | import ( |
| 4 | "slices" |
| 5 | "sync" |
| 6 | "time" |
| 7 | ) |
| 8 | |
| 9 | // A tally collects how long finished jobs waited, until somebody takes them. |
| 10 | type tally struct { |
| 11 | mu sync.Mutex |
| 12 | waits []time.Duration |
| 13 | } |
| 14 | |
| 15 | func (t *tally) add(wait time.Duration) { |
| 16 | t.mu.Lock() |
| 17 | defer t.mu.Unlock() |
| 18 | t.waits = append(t.waits, wait) |
| 19 | } |
| 20 | |
| 21 | // take hands over the waits, shortest first, and starts a new page. |
| 22 | func (t *tally) take() []time.Duration { |
| 23 | t.mu.Lock() |
| 24 | waits := t.waits |
| 25 | t.waits = nil |
| 26 | t.mu.Unlock() |
| 27 | slices.Sort(waits) |
| 28 | return waits |
| 29 | } |