DBBatcher batches connection log upserts and periodically flushes them to the database to reduce per-event write pressure.
| 118 | // DBBatcher batches connection log upserts and periodically flushes |
| 119 | // them to the database to reduce per-event write pressure. |
| 120 | type DBBatcher struct { |
| 121 | store database.Store |
| 122 | log slog.Logger |
| 123 | |
| 124 | itemCh chan database.UpsertConnectionLogParams |
| 125 | |
| 126 | // dedupedBatch holds entries keyed by connection ID so that |
| 127 | // PostgreSQL never sees the same row twice in one INSERT … |
| 128 | // ON CONFLICT DO UPDATE. Connection IDs are globally unique |
| 129 | // (each new session gets a fresh UUID). Entries with a NULL |
| 130 | // connection_id (web events) go into nullConnIDBatch instead |
| 131 | // because NULL != NULL in SQL unique constraints. |
| 132 | dedupedBatch map[uuid.UUID]batchEntry |
| 133 | nullConnIDBatch []batchEntry |
| 134 | maxBatchSize int |
| 135 | |
| 136 | // retryCh is a bounded channel of failed batches awaiting |
| 137 | // retry. A single retry worker goroutine processes this |
| 138 | // channel, retrying each batch up to maxRetries times before |
| 139 | // dropping it. If the channel is full, new failures are |
| 140 | // dropped immediately. |
| 141 | retryCh chan database.BatchUpsertConnectionLogsParams |
| 142 | |
| 143 | clock quartz.Clock |
| 144 | timer *quartz.Timer |
| 145 | interval time.Duration |
| 146 | |
| 147 | ctx context.Context |
| 148 | cancel context.CancelFunc |
| 149 | wg sync.WaitGroup |
| 150 | } |
| 151 | |
| 152 | // NewDBBatcher creates a DBBatcher that batches writes to the database |
| 153 | // and starts its background processing loop. Close must be called to |
nothing calls this directly
no outgoing calls
no test coverage detected