MCPcopy Index your code
hub / github.com/coder/coder / NewDBBatcher

Function NewDBBatcher

enterprise/coderd/connectionlog/connectionlog.go:155–190  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

153// and starts its background processing loop. Close must be called to
154// flush remaining entries on shutdown.
155func 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.

Callers 3

NewFunction · 0.92
TestDBBackendIntegrationFunction · 0.92
Test_batcherFlushFunction · 0.85

Calls 4

runMethod · 0.95
retryLoopMethod · 0.95
AddMethod · 0.65
DoneMethod · 0.45

Tested by 2

TestDBBackendIntegrationFunction · 0.74
Test_batcherFlushFunction · 0.68