@@ -24,13 +24,13 @@ const (
24
24
defaultFlushInterval = time .Second
25
25
)
26
26
27
- type StatsBatcher interface {
27
+ type Batcher interface {
28
28
Add (now time.Time , agentID uuid.UUID , templateID uuid.UUID , userID uuid.UUID , workspaceID uuid.UUID , st * agentproto.Stats ) error
29
29
}
30
30
31
- // Batcher holds a buffer of agent stats and periodically flushes them to
31
+ // DBBatcher holds a buffer of agent stats and periodically flushes them to
32
32
// its configured store.
33
- type Batcher struct {
33
+ type DBBatcher struct {
34
34
store database.Store
35
35
log slog.Logger
36
36
@@ -54,39 +54,39 @@ type Batcher struct {
54
54
}
55
55
56
56
// Option is a functional option for configuring a Batcher.
57
- type BatcherOption func (b * Batcher )
57
+ type BatcherOption func (b * DBBatcher )
58
58
59
59
// BatcherWithStore sets the store to use for storing stats.
60
60
func BatcherWithStore (store database.Store ) BatcherOption {
61
- return func (b * Batcher ) {
61
+ return func (b * DBBatcher ) {
62
62
b .store = store
63
63
}
64
64
}
65
65
66
66
// BatcherWithBatchSize sets the number of stats to store in a batch.
67
67
func BatcherWithBatchSize (size int ) BatcherOption {
68
- return func (b * Batcher ) {
68
+ return func (b * DBBatcher ) {
69
69
b .batchSize = size
70
70
}
71
71
}
72
72
73
73
// BatcherWithInterval sets the interval for flushes.
74
74
func BatcherWithInterval (d time.Duration ) BatcherOption {
75
- return func (b * Batcher ) {
75
+ return func (b * DBBatcher ) {
76
76
b .interval = d
77
77
}
78
78
}
79
79
80
80
// BatcherWithLogger sets the logger to use for logging.
81
81
func BatcherWithLogger (log slog.Logger ) BatcherOption {
82
- return func (b * Batcher ) {
82
+ return func (b * DBBatcher ) {
83
83
b .log = log
84
84
}
85
85
}
86
86
87
87
// NewBatcher creates a new Batcher and starts it.
88
- func NewBatcher (ctx context.Context , opts ... BatcherOption ) (* Batcher , func (), error ) {
89
- b := & Batcher {}
88
+ func NewBatcher (ctx context.Context , opts ... BatcherOption ) (* DBBatcher , func (), error ) {
89
+ b := & DBBatcher {}
90
90
b .log = slog .Make (sloghuman .Sink (os .Stderr ))
91
91
b .flushLever = make (chan struct {}, 1 ) // Buffered so that it doesn't block.
92
92
for _ , opt := range opts {
@@ -131,7 +131,7 @@ func NewBatcher(ctx context.Context, opts ...BatcherOption) (*Batcher, func(), e
131
131
}
132
132
133
133
// Add adds a stat to the batcher for the given workspace and agent.
134
- func (b * Batcher ) Add (
134
+ func (b * DBBatcher ) Add (
135
135
now time.Time ,
136
136
agentID uuid.UUID ,
137
137
templateID uuid.UUID ,
@@ -178,7 +178,7 @@ func (b *Batcher) Add(
178
178
}
179
179
180
180
// Run runs the batcher.
181
- func (b * Batcher ) run (ctx context.Context ) {
181
+ func (b * DBBatcher ) run (ctx context.Context ) {
182
182
// nolint:gocritic // This is only ever used for one thing - inserting agent stats.
183
183
authCtx := dbauthz .AsSystemRestricted (ctx )
184
184
for {
@@ -203,7 +203,7 @@ func (b *Batcher) run(ctx context.Context) {
203
203
}
204
204
205
205
// flush flushes the batcher's buffer.
206
- func (b * Batcher ) flush (ctx context.Context , forced bool , reason string ) {
206
+ func (b * DBBatcher ) flush (ctx context.Context , forced bool , reason string ) {
207
207
b .mu .Lock ()
208
208
b .flushForced .Store (true )
209
209
start := time .Now ()
@@ -260,7 +260,7 @@ func (b *Batcher) flush(ctx context.Context, forced bool, reason string) {
260
260
}
261
261
262
262
// initBuf resets the buffer. b MUST be locked.
263
- func (b * Batcher ) initBuf (size int ) {
263
+ func (b * DBBatcher ) initBuf (size int ) {
264
264
b .buf = & database.InsertWorkspaceAgentStatsParams {
265
265
ID : make ([]uuid.UUID , 0 , b .batchSize ),
266
266
CreatedAt : make ([]time.Time , 0 , b .batchSize ),
@@ -284,7 +284,7 @@ func (b *Batcher) initBuf(size int) {
284
284
b .connectionsByProto = make ([]map [string ]int64 , 0 , size )
285
285
}
286
286
287
- func (b * Batcher ) resetBuf () {
287
+ func (b * DBBatcher ) resetBuf () {
288
288
b .buf .ID = b .buf .ID [:0 ]
289
289
b .buf .CreatedAt = b .buf .CreatedAt [:0 ]
290
290
b .buf .UserID = b .buf .UserID [:0 ]
0 commit comments