NewTracker returns a new Tracker. It is the caller's responsibility to call Close().
(s Store, opts ...TrackerOption)
| 44 | // NewTracker returns a new Tracker. It is the caller's responsibility |
| 45 | // to call Close(). |
| 46 | func NewTracker(s Store, opts ...TrackerOption) *UsageTracker { |
| 47 | tr := &UsageTracker{ |
| 48 | log: slog.Make(sloghuman.Sink(os.Stderr)), |
| 49 | m: &uuidSet{}, |
| 50 | s: s, |
| 51 | tickCh: nil, |
| 52 | stopTick: nil, |
| 53 | stopCh: make(chan struct{}), |
| 54 | doneCh: make(chan struct{}), |
| 55 | flushCh: nil, |
| 56 | } |
| 57 | for _, opt := range opts { |
| 58 | opt(tr) |
| 59 | } |
| 60 | if tr.tickCh == nil && tr.stopTick == nil { |
| 61 | tick := time.NewTicker(DefaultFlushInterval) |
| 62 | tr.tickCh = tick.C |
| 63 | tr.stopTick = tick.Stop |
| 64 | } |
| 65 | go tr.loop() |
| 66 | return tr |
| 67 | } |
| 68 | |
| 69 | type TrackerOption func(*UsageTracker) |
| 70 |