Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite8f0e3e

Browse files
feat: add backoff to workspace agent polling (#20157)
## SummaryIn this pull request we're adding a simple backoff to the workspaceagent polling. This backoff is being added to address seemingly randomcases of elevated number of calls that we've seen to the`api/v2/workspaceagent/{agent_id}` endpoint.For more information on the investigation, see:coder/internal#725### Changes- Updated the polling to use predefined progressive intervals forpolling instead of continuously polling every 500ms### Testing- Added a test for the function used to calculate the progressivepolling intervalCo-authored-by: Spike Curtis <spike@coder.com>
1 parenta1fa5c8 commite8f0e3e

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

‎cli/cliui/agent.go‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ func Agent(ctx context.Context, writer io.Writer, agentID uuid.UUID, opts AgentO
5353
t:=time.NewTimer(0)
5454
defert.Stop()
5555

56+
startTime:=time.Now()
57+
baseInterval:=opts.FetchInterval
58+
5659
for {
5760
select {
5861
case<-ctx.Done():
@@ -68,7 +71,11 @@ func Agent(ctx context.Context, writer io.Writer, agentID uuid.UUID, opts AgentO
6871
return
6972
}
7073
fetchedAgent<-fetchAgent{agent:agent}
71-
t.Reset(opts.FetchInterval)
74+
75+
// Adjust the interval based on how long we've been waiting.
76+
elapsed:=time.Since(startTime)
77+
currentInterval:=GetProgressiveInterval(baseInterval,elapsed)
78+
t.Reset(currentInterval)
7279
}
7380
}
7481
}()
@@ -293,6 +300,24 @@ func safeDuration(sw *stageWriter, a, b *time.Time) time.Duration {
293300
returna.Sub(*b)
294301
}
295302

303+
// GetProgressiveInterval returns an interval that increases over time.
304+
// The interval starts at baseInterval and increases to
305+
// a maximum of baseInterval * 16 over time.
306+
funcGetProgressiveInterval(baseInterval time.Duration,elapsed time.Duration) time.Duration {
307+
switch {
308+
caseelapsed<60*time.Second:
309+
returnbaseInterval// 500ms for first 60 seconds
310+
caseelapsed<2*time.Minute:
311+
returnbaseInterval*2// 1s for next 1 minute
312+
caseelapsed<5*time.Minute:
313+
returnbaseInterval*4// 2s for next 3 minutes
314+
caseelapsed<10*time.Minute:
315+
returnbaseInterval*8// 4s for next 5 minutes
316+
default:
317+
returnbaseInterval*16// 8s after 10 minutes
318+
}
319+
}
320+
296321
typecloseFuncfunc()error
297322

298323
func (ccloseFunc)Close()error {

‎cli/cliui/agent_test.go‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -866,3 +866,31 @@ func TestConnDiagnostics(t *testing.T) {
866866
})
867867
}
868868
}
869+
870+
funcTestGetProgressiveInterval(t*testing.T) {
871+
t.Parallel()
872+
873+
baseInterval:=500*time.Millisecond
874+
875+
testCases:= []struct {
876+
namestring
877+
elapsed time.Duration
878+
expected time.Duration
879+
}{
880+
{"first_minute",30*time.Second,baseInterval},
881+
{"second_minute",90*time.Second,baseInterval*2},
882+
{"third_to_fifth_minute",3*time.Minute,baseInterval*4},
883+
{"sixth_to_tenth_minute",7*time.Minute,baseInterval*8},
884+
{"after_ten_minutes",15*time.Minute,baseInterval*16},
885+
{"boundary_first_minute",59*time.Second,baseInterval},
886+
{"boundary_second_minute",61*time.Second,baseInterval*2},
887+
}
888+
889+
for_,tc:=rangetestCases {
890+
t.Run(tc.name,func(t*testing.T) {
891+
t.Parallel()
892+
result:=cliui.GetProgressiveInterval(baseInterval,tc.elapsed)
893+
require.Equal(t,tc.expected,result)
894+
})
895+
}
896+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp