Skip to content

Commit f5f8d75

Browse files
committed
rename receiver
1 parent cc72868 commit f5f8d75

File tree

1 file changed

+28
-28
lines changed

1 file changed

+28
-28
lines changed

coderd/workspaceusage/tracker.go

+28-28
Original file line numberDiff line numberDiff line change
@@ -110,24 +110,24 @@ func WithTickChannel(c chan time.Time) Option {
110110

111111
// Add marks the workspace with the given ID as having been used recently.
112112
// Tracker will periodically flush this to its configured Store.
113-
func (wut *Tracker) Add(workspaceID uuid.UUID) {
114-
wut.m.Add(workspaceID)
113+
func (tr *Tracker) Add(workspaceID uuid.UUID) {
114+
tr.m.Add(workspaceID)
115115
}
116116

117117
// flush updates last_used_at of all current workspace IDs.
118118
// If this is held while a previous flush is in progress, it will
119119
// deadlock until the previous flush has completed.
120-
func (wut *Tracker) flush(now time.Time) {
120+
func (tr *Tracker) flush(now time.Time) {
121121
// Copy our current set of IDs
122-
ids := wut.m.UniqueAndClear()
122+
ids := tr.m.UniqueAndClear()
123123
count := len(ids)
124-
if wut.flushCh != nil { // only used for testing
124+
if tr.flushCh != nil { // only used for testing
125125
defer func() {
126-
wut.flushCh <- count
126+
tr.flushCh <- count
127127
}()
128128
}
129129
if count == 0 {
130-
wut.log.Debug(context.Background(), "nothing to flush")
130+
tr.log.Debug(context.Background(), "nothing to flush")
131131
return
132132
}
133133

@@ -136,47 +136,47 @@ func (wut *Tracker) flush(now time.Time) {
136136
defer cancel()
137137
// nolint: gocritic // system function
138138
authCtx := dbauthz.AsSystemRestricted(ctx)
139-
wut.flushLock.Lock()
140-
defer wut.flushLock.Unlock()
141-
if err := wut.s.BatchUpdateWorkspaceLastUsedAt(authCtx, database.BatchUpdateWorkspaceLastUsedAtParams{
139+
tr.flushLock.Lock()
140+
defer tr.flushLock.Unlock()
141+
if err := tr.s.BatchUpdateWorkspaceLastUsedAt(authCtx, database.BatchUpdateWorkspaceLastUsedAtParams{
142142
LastUsedAt: now,
143143
IDs: ids,
144144
}); err != nil {
145145
// A single failure to flush is likely not a huge problem. If the workspace is still connected at
146146
// the next iteration, either another coderd instance will likely have this data or the CLI
147147
// will tell us again that the workspace is in use.
148-
wut.flushErrors++
149-
if wut.flushErrors > 1 {
150-
wut.log.Error(ctx, "multiple failures updating workspaces last_used_at", slog.F("count", count), slog.F("consecutive_errors", wut.flushErrors), slog.Error(err))
148+
tr.flushErrors++
149+
if tr.flushErrors > 1 {
150+
tr.log.Error(ctx, "multiple failures updating workspaces last_used_at", slog.F("count", count), slog.F("consecutive_errors", tr.flushErrors), slog.Error(err))
151151
// TODO: if this keeps failing, it indicates a fundamental problem with the database connection.
152152
// How to surface it correctly to admins besides just screaming into the logs?
153153
} else {
154-
wut.log.Warn(ctx, "failed updating workspaces last_used_at", slog.F("count", count), slog.Error(err))
154+
tr.log.Warn(ctx, "failed updating workspaces last_used_at", slog.F("count", count), slog.Error(err))
155155
}
156156
return
157157
}
158-
wut.flushErrors = 0
159-
wut.log.Info(ctx, "updated workspaces last_used_at", slog.F("count", count), slog.F("now", now))
158+
tr.flushErrors = 0
159+
tr.log.Info(ctx, "updated workspaces last_used_at", slog.F("count", count), slog.F("now", now))
160160
}
161161

162162
// Loop periodically flushes every tick.
163163
// If Loop is called after Close, it will panic. Don't do this.
164-
func (wut *Tracker) Loop() {
164+
func (tr *Tracker) Loop() {
165165
// Calling Loop after Close() is an error.
166166
select {
167-
case <-wut.doneCh:
167+
case <-tr.doneCh:
168168
panic("developer error: Loop called after Close")
169169
default:
170170
}
171171
defer func() {
172-
wut.log.Debug(context.Background(), "workspace usage tracker loop exited")
172+
tr.log.Debug(context.Background(), "workspace usage tracker loop exited")
173173
}()
174174
for {
175175
select {
176-
case <-wut.stopCh:
177-
close(wut.doneCh)
176+
case <-tr.stopCh:
177+
close(tr.doneCh)
178178
return
179-
case now, ok := <-wut.tickCh:
179+
case now, ok := <-tr.tickCh:
180180
if !ok {
181181
return
182182
}
@@ -186,18 +186,18 @@ func (wut *Tracker) Loop() {
186186
// then we could capture the exact usage time of each workspace. For now however, as
187187
// we perform this query at a regular interval, the time of the flush is 'close enough'
188188
// for the purposes of both dormancy (and for autostop, in future).
189-
wut.flush(now.UTC())
189+
tr.flush(now.UTC())
190190
}
191191
}
192192
}
193193

194194
// Close stops Tracker and returns once Loop has exited.
195195
// After calling Close(), Loop must not be called.
196-
func (wut *Tracker) Close() {
197-
wut.stopOnce.Do(func() {
198-
wut.stopCh <- struct{}{}
199-
wut.stopTick()
200-
<-wut.doneCh
196+
func (tr *Tracker) Close() {
197+
tr.stopOnce.Do(func() {
198+
tr.stopCh <- struct{}{}
199+
tr.stopTick()
200+
<-tr.doneCh
201201
})
202202
}
203203

0 commit comments

Comments
 (0)