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

Method flush

enterprise/coderd/connectionlog/connectionlog.go:340–382  ·  view source on GitHub ↗

flush builds the batch params, clears the in-memory batch, and writes to the database. On failure, the batch is queued for retry by the single retry worker goroutine. If the retry queue is full, the batch is dropped.

(ctx context.Context)

Source from the content-addressed store, hash-verified

338// by the single retry worker goroutine. If the retry queue is full,
339// the batch is dropped.
340func (b *DBBatcher) flush(ctx context.Context) {
341 count := b.batchLen()
342 if count == 0 {
343 return
344 }
345
346 params := b.buildParams()
347
348 // Clear the batch before writing so the run loop can start
349 // accumulating new entries.
350 b.dedupedBatch = make(map[uuid.UUID]batchEntry, b.maxBatchSize)
351 b.nullConnIDBatch = nil
352
353 // Use the batcher's context for normal operation so Close()
354 // can cancel hung writes. During shutdown (ctx already canceled),
355 // fall back to a bounded timeout.
356 writeCtx := b.ctx
357 if writeCtx.Err() != nil {
358 var cancel context.CancelFunc
359 writeCtx, cancel = context.WithTimeout(context.Background(), shutdownWriteTimeout)
360 defer cancel()
361 }
362 //nolint:gocritic // System-level batch operation for connection logs.
363 err := b.store.BatchUpsertConnectionLogs(dbauthz.AsConnectionLogger(writeCtx), params)
364 if err == nil {
365 return
366 }
367
368 b.log.Error(ctx, "batch upsert failed, queueing for retry",
369 slog.Error(err), slog.F("count", count))
370
371 // Don't retry on shutdown.
372 if ctx.Err() != nil {
373 return
374 }
375
376 select {
377 case b.retryCh <- params:
378 default:
379 b.log.Error(ctx, "retry queue full, dropping batch",
380 slog.F("dropped", count))
381 }
382}
383
384func (b *DBBatcher) buildParams() database.BatchUpsertConnectionLogsParams {
385 count := b.batchLen()

Callers 1

runMethod · 0.95

Calls 6

batchLenMethod · 0.95
buildParamsMethod · 0.95
AsConnectionLoggerFunction · 0.92
ErrMethod · 0.80
ErrorMethod · 0.45

Tested by

no test coverage detected