Batcher holds a buffer of agent metadata updates and periodically flushes them to the database and pubsub. This reduces database write frequency and pubsub publish rate.
| 75 | // flushes them to the database and pubsub. This reduces database write |
| 76 | // frequency and pubsub publish rate. |
| 77 | type Batcher struct { |
| 78 | store database.Store |
| 79 | ps pubsub.Pubsub |
| 80 | log slog.Logger |
| 81 | |
| 82 | // updateCh is the buffered channel that receives metadata updates from Add() calls. |
| 83 | updateCh chan update |
| 84 | |
| 85 | // batch holds the current batch being accumulated. For updates with the same composite key the most recent value wins. |
| 86 | batch map[compositeKey]value |
| 87 | currentBatchLen atomic.Int64 |
| 88 | maxBatchSize int |
| 89 | |
| 90 | clock quartz.Clock |
| 91 | timer *quartz.Timer |
| 92 | interval time.Duration |
| 93 | // Used to only log at warn level for dropped keys infrequently, as it could be noisy in failure scenarios. |
| 94 | warnTicker *quartz.Ticker |
| 95 | |
| 96 | // ctx is the context for the batcher. Used to check if shutdown has begun. |
| 97 | ctx context.Context |
| 98 | cancel context.CancelFunc |
| 99 | done chan struct{} |
| 100 | |
| 101 | // Metrics collects Prometheus metrics for the batcher. |
| 102 | Metrics Metrics |
| 103 | } |
| 104 | |
| 105 | // Option is a functional option for configuring a Batcher. |
| 106 | type Option func(b *Batcher) |
nothing calls this directly
no outgoing calls
no test coverage detected