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

Method flush

coderd/workspacestats/batcher.go:207–261  ·  view source on GitHub ↗

flush flushes the batcher's buffer.

(ctx context.Context, forced bool, reason string)

Source from the content-addressed store, hash-verified

205
206// flush flushes the batcher's buffer.
207func (b *DBBatcher) flush(ctx context.Context, forced bool, reason string) {
208 b.mu.Lock()
209 b.flushForced.Store(true)
210 start := time.Now()
211 count := len(b.buf.ID)
212 defer func() {
213 b.flushForced.Store(false)
214 b.mu.Unlock()
215 if count > 0 {
216 elapsed := time.Since(start)
217 b.log.Debug(ctx, "flush complete",
218 slog.F("count", count),
219 slog.F("elapsed", elapsed),
220 slog.F("forced", forced),
221 slog.F("reason", reason),
222 )
223 }
224 // Notify that a flush has completed. This only happens in tests.
225 if b.flushed != nil {
226 select {
227 case <-ctx.Done():
228 close(b.flushed)
229 default:
230 b.flushed <- count
231 }
232 }
233 }()
234
235 if len(b.buf.ID) == 0 {
236 return
237 }
238
239 // marshal connections by proto
240 payload, err := json.Marshal(b.connectionsByProto)
241 if err != nil {
242 b.log.Error(ctx, "unable to marshal agent connections by proto, dropping data", slog.Error(err))
243 b.buf.ConnectionsByProto = json.RawMessage(`[]`)
244 } else {
245 b.buf.ConnectionsByProto = payload
246 }
247
248 // nolint:gocritic // (#13146) Will be moved soon as part of refactor.
249 err = b.store.InsertWorkspaceAgentStats(ctx, *b.buf)
250 elapsed := time.Since(start)
251 if err != nil {
252 if database.IsQueryCanceledError(err) {
253 b.log.Debug(ctx, "query canceled, skipping insert of workspace agent stats", slog.F("elapsed", elapsed))
254 return
255 }
256 b.log.Error(ctx, "error inserting workspace agent stats", slog.Error(err), slog.F("elapsed", elapsed))
257 return
258 }
259
260 b.resetBuf()
261}
262
263// initBuf resets the buffer. b MUST be locked.
264func (b *DBBatcher) initBuf(size int) {

Callers 1

runMethod · 0.95

Calls 9

resetBufMethod · 0.95
IsQueryCanceledErrorFunction · 0.92
LockMethod · 0.45
StoreMethod · 0.45
UnlockMethod · 0.45
DoneMethod · 0.45
MarshalMethod · 0.45
ErrorMethod · 0.45

Tested by

no test coverage detected