Skip to content

Commit b7ddb4a

Browse files
committed
code cleanup in agent/
1 parent 95c8ada commit b7ddb4a

File tree

2 files changed

+27
-11
lines changed

2 files changed

+27
-11
lines changed

agent/agent.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,12 @@ func (a *agent) collectMetadata(ctx context.Context, md codersdk.WorkspaceAgentM
223223
var out bytes.Buffer
224224

225225
result := &codersdk.WorkspaceAgentMetadataResult{
226+
// CollectedAt is set here for testing purposes and overrided by
227+
// the server to the time the server received the result to protect
228+
// against clock skew.
229+
//
230+
// In the future, the server may accept the timestamp from the agent
231+
// if it is certain the clocks are in sync.
226232
CollectedAt: time.Now(),
227233
}
228234
cmd, err := a.createCommand(ctx, md.Script, nil)
@@ -237,7 +243,7 @@ func (a *agent) collectMetadata(ctx context.Context, md codersdk.WorkspaceAgentM
237243
// The error isn't mutually exclusive with useful output.
238244
err = cmd.Run()
239245

240-
const bufLimit = 10 << 14
246+
const bufLimit = 10 << 10
241247
if out.Len() > bufLimit {
242248
err = errors.Join(
243249
err,
@@ -293,7 +299,7 @@ func (a *agent) reportMetadataLoop(ctx context.Context) {
293299
// If we're backpressured on sending back results, we risk
294300
// runaway goroutine growth and/or overloading coderd. So,
295301
// we just skip the collection. Since we never update
296-
// "lastCollectedAt" for this key, we'll retry the collection
302+
// the collections map, we'll retry the collection
297303
// on the next tick.
298304
a.logger.Debug(
299305
ctx, "metadata collection backpressured",
@@ -317,8 +323,9 @@ func (a *agent) reportMetadataLoop(ctx context.Context) {
317323
}
318324
}
319325

320-
// Spawn a goroutine for each metadata collection, and use channels
321-
// to synchronize the results and avoid messy mutex logic.
326+
// Spawn a goroutine for each metadata collection, and use a
327+
// channel to synchronize the results and avoid both messy
328+
// mutex logic and overloading the API.
322329
for _, md := range manifest.Metadata {
323330
collectedAt, ok := lastCollectedAts[md.Key]
324331
if ok {

agent/agent_test.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -905,16 +905,21 @@ func TestAgent_Metadata(t *testing.T) {
905905

906906
t.Run("Many", func(t *testing.T) {
907907
if runtime.GOOS == "windows" {
908-
// Shell scripting in Windows is a pain, but we test that it works in the simpler "CollectOnce"
909-
// test.
908+
// Shell scripting in Windows is a pain, and we have already tested
909+
// that the OS logic works in the simpler "Once" test above.
910910
t.Skip()
911911
}
912912
t.Parallel()
913913

914914
dir := t.TempDir()
915+
916+
// In tests, the interval unit is 100 milliseconds while in production
917+
// it is 1 second.
915918
const reportInterval = 2
916-
greetingPath := filepath.Join(dir, "greeting")
917-
script := "echo hello | tee -a " + greetingPath
919+
var (
920+
greetingPath = filepath.Join(dir, "greeting")
921+
script = "echo hello | tee -a " + greetingPath
922+
)
918923
_, client, _, _, _ := setupAgent(t, agentsdk.Manifest{
919924
Metadata: []codersdk.WorkspaceAgentMetadataDescription{
920925
{
@@ -949,12 +954,16 @@ func TestAgent_Metadata(t *testing.T) {
949954
var (
950955
numGreetings = bytes.Count(greetingByt, []byte("hello"))
951956
idealNumGreetings = time.Since(start) / (reportInterval * 100 * time.Millisecond)
952-
upperBound = int(idealNumGreetings) + 1
953-
lowerBound = (int(idealNumGreetings) / 2)
957+
// We allow a 50% error margin because the report loop may backlog
958+
// in CI and other toasters. In production, there is no hard
959+
// guarantee on timing either, and the frontend gives similar
960+
// wiggle room to the staleness of the value.
961+
upperBound = int(idealNumGreetings) + 1
962+
lowerBound = (int(idealNumGreetings) / 2)
954963
)
955964

956965
if idealNumGreetings < 5 {
957-
// Not enough time has passed to get a good sample size.
966+
// There is an insufficient sample size.
958967
continue
959968
}
960969

0 commit comments

Comments
 (0)