NewDBBatcher creates a DBBatcher that batches writes to the database and starts its background processing loop. Close must be called to flush remaining entries on shutdown.
(ctx context.Context, store database.Store, log slog.Logger, opts ...DBBatcherOption)
| 153 | // and starts its background processing loop. Close must be called to |
| 154 | // flush remaining entries on shutdown. |
| 155 | func NewDBBatcher(ctx context.Context, store database.Store, log slog.Logger, opts ...DBBatcherOption) *DBBatcher { |
| 156 | b := &DBBatcher{ |
| 157 | store: store, |
| 158 | log: log, |
| 159 | clock: quartz.NewReal(), |
| 160 | } |
| 161 | |
| 162 | for _, opt := range opts { |
| 163 | opt(b) |
| 164 | } |
| 165 | |
| 166 | if b.interval == 0 { |
| 167 | b.interval = defaultFlushInterval |
| 168 | } |
| 169 | if b.maxBatchSize == 0 { |
| 170 | b.maxBatchSize = defaultBatchSize |
| 171 | } |
| 172 | |
| 173 | b.timer = b.clock.NewTimer(b.interval) |
| 174 | b.itemCh = make(chan database.UpsertConnectionLogParams, b.maxBatchSize) |
| 175 | b.dedupedBatch = make(map[uuid.UUID]batchEntry, b.maxBatchSize) |
| 176 | b.retryCh = make(chan database.BatchUpsertConnectionLogsParams, retryQueueSize) |
| 177 | |
| 178 | b.ctx, b.cancel = context.WithCancel(ctx) |
| 179 | b.wg.Add(2) |
| 180 | go func() { |
| 181 | defer b.wg.Done() |
| 182 | b.run(b.ctx) |
| 183 | }() |
| 184 | go func() { |
| 185 | defer b.wg.Done() |
| 186 | b.retryLoop() |
| 187 | }() |
| 188 | |
| 189 | return b |
| 190 | } |
| 191 | |
| 192 | // Upsert enqueues a connection log entry for batched writing. It |
| 193 | // blocks if the internal buffer is full, ensuring no logs are dropped. |