Allow returns true if the historical total plus incoming size is less than or equal to the max. The historical total is kept alive and incremented even if not allowed, so that long-running traces are cutoff as expected.
(traceID []byte, sz, maxSize int)
| 40 | // or equal to the max. The historical total is kept alive and incremented even |
| 41 | // if not allowed, so that long-running traces are cutoff as expected. |
| 42 | func (s *Tracker) Allow(traceID []byte, sz, maxSize int) AllowResult { |
| 43 | s.mtx.Lock() |
| 44 | defer s.mtx.Unlock() |
| 45 | |
| 46 | token := s.token(traceID) |
| 47 | tr := s.sizes[token] |
| 48 | if tr == nil { |
| 49 | tr = &traceSize{ |
| 50 | size: 0, // size added below |
| 51 | } |
| 52 | s.sizes[token] = tr |
| 53 | } |
| 54 | |
| 55 | tr.timestamp = time.Now() |
| 56 | tr.size += sz |
| 57 | |
| 58 | return AllowResult{ |
| 59 | tr.size <= maxSize, |
| 60 | tr.size, |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | func (s *Tracker) ClearIdle(idleSince time.Time) { |
| 65 | s.mtx.Lock() |