Skip to content

Commit 0143e11

Browse files
yoannchaudetCopilot
andcommitted
Add backoff and jitter to deployment polling
Co-authored-by: Copilot App <[email protected]>
1 parent 5e98f10 commit 0143e11

6 files changed

Lines changed: 158 additions & 11 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ jobs:
5151
| `token` | `true` | `${{ github.token }}` | The GitHub token used to create an authenticated client - Provided for you by default! |
5252
| `timeout` | `false` | `"600000"` | Time in milliseconds after which to timeout and cancel the deployment (default: 10 minutes) |
5353
| `error_count` | `false` | `"10"` | Maximum number of status report errors before cancelling a deployment (default: 10) |
54-
| `reporting_interval` | `false` | `"5000"` | Time in milliseconds between two deployment status reports (default: 5 seconds) |
54+
| `reporting_interval` | `false` | `"5000"` | Initial time in milliseconds between deployment status reports. Subsequent intervals use exponential backoff capped at 30 seconds and ±20% jitter (default: 5 seconds). |
5555
| `artifact_name` | `false` | `"github-pages"` | The name of the artifact to deploy |
5656
| `preview` | `false` | `"false"` | Is this attempting to deploy a pull request as a GitHub Pages preview site? (NOTE: This feature is only in alpha currently and is not available to the public!) |
5757

‎action.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ inputs:
1818
required: false
1919
default: '10'
2020
reporting_interval:
21-
description: 'Time in milliseconds between two deployment status report (default: 5 seconds)'
21+
description: 'Initial time in milliseconds between deployment status reports; subsequent intervals use capped backoff and jitter (default: 5 seconds)'
2222
required: false
2323
default: '5000'
2424
artifact_name:

‎dist/index.js‎

Lines changed: 21 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎dist/index.js.map‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/__tests__/internal/deployment.test.js‎

Lines changed: 113 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@ const nock = require('nock')
44
// For mocking network calls with native Fetch (octokit)
55
const { MockAgent, setGlobalDispatcher } = require('undici')
66

7-
const { Deployment, MAX_TIMEOUT, ONE_GIGABYTE, SIZE_LIMIT_DESCRIPTION } = require('../../internal/deployment')
7+
const {
8+
Deployment,
9+
MAX_TIMEOUT,
10+
MAX_REPORTING_INTERVAL,
11+
ONE_GIGABYTE,
12+
SIZE_LIMIT_DESCRIPTION
13+
} = require('../../internal/deployment')
814

915
const fakeJwt =
1016
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiJiNjllMWIxOC1jOGFiLTRhZGQtOGYxOC03MzVlMzVjZGJhZjAiLCJzdWIiOiJyZXBvOnBhcGVyLXNwYS9taW55aTplbnZpcm9ubWVudDpQcm9kdWN0aW9uIiwiYXVkIjoiaHR0cHM6Ly9naXRodWIuY29tL3BhcGVyLXNwYSIsInJlZiI6InJlZnMvaGVhZHMvbWFpbiIsInNoYSI6ImEyODU1MWJmODdiZDk3NTFiMzdiMmM0YjM3M2MxZjU3NjFmYWM2MjYiLCJyZXBvc2l0b3J5IjoicGFwZXItc3BhL21pbnlpIiwicmVwb3NpdG9yeV9vd25lciI6InBhcGVyLXNwYSIsInJ1bl9pZCI6IjE1NDY0NTkzNjQiLCJydW5fbnVtYmVyIjoiMzQiLCJydW5fYXR0ZW1wdCI6IjIiLCJhY3RvciI6IllpTXlzdHkiLCJ3b3JrZmxvdyI6IkNJIiwiaGVhZF9yZWYiOiIiLCJiYXNlX3JlZiI6IiIsImV2ZW50X25hbWUiOiJwdXNoIiwicmVmX3R5cGUiOiJicmFuY2giLCJlbnZpcm9ubWVudCI6IlByb2R1Y3Rpb24iLCJqb2Jfd29ya2Zsb3dfcmVmIjoicGFwZXItc3BhL21pbnlpLy5naXRodWIvd29ya2Zsb3dzL2JsYW5rLnltbEByZWZzL2hlYWRzL21haW4iLCJpc3MiOiJodHRwczovL3Rva2VuLmFjdGlvbnMuZ2l0aHVidXNlcmNvbnRlbnQuY29tIiwibmJmIjoxNjM4ODI4MDI4LCJleHAiOjE2Mzg4Mjg5MjgsImlhdCI6MTYzODgyODYyOH0.1wyupfxu1HGoTyIqatYg0hIxy2-0bMO-yVlmLSMuu2w'
@@ -35,7 +41,7 @@ describe('Deployment', () => {
3541
case 'token':
3642
return process.env.GITHUB_TOKEN
3743
case 'reporting_interval':
38-
return 50 // Lower reporting interval to speed up test
44+
return process.env.INPUT_REPORTING_INTERVAL || 50 // Lower reporting interval to speed up test
3945
default:
4046
return process.env[`INPUT_${param.toUpperCase()}`] || ''
4147
}
@@ -608,6 +614,36 @@ describe('Deployment', () => {
608614
})
609615

610616
describe('#check', () => {
617+
const mockDeploymentStatus = (status, times = 1) => {
618+
mockPool
619+
.intercept({
620+
path: `/repos/${process.env.GITHUB_REPOSITORY}/pages/deployments/${process.env.GITHUB_SHA}`,
621+
method: 'GET'
622+
})
623+
.reply(200, { status }, { headers: { 'content-type': 'application/json' } })
624+
.times(times)
625+
}
626+
627+
const createPendingDeployment = () => {
628+
const deployment = new Deployment()
629+
deployment.deploymentInfo = {
630+
id: process.env.GITHUB_SHA,
631+
pending: true
632+
}
633+
deployment.startTime = Date.now()
634+
return deployment
635+
}
636+
637+
const runWithoutWaiting = async deployment => {
638+
const timeoutSpy = jest.spyOn(global, 'setTimeout').mockImplementation(resolve => {
639+
resolve()
640+
return 0
641+
})
642+
643+
await deployment.check()
644+
return timeoutSpy
645+
}
646+
611647
it('sets output to success when deployment is successful', async () => {
612648
process.env.GITHUB_SHA = 'valid-build-version'
613649

@@ -990,6 +1026,81 @@ describe('Deployment', () => {
9901026
expect(core.info).toHaveBeenLastCalledWith('Reported success!')
9911027
twirpScope.done()
9921028
})
1029+
1030+
it('backs off successful non-terminal status checks', async () => {
1031+
process.env.GITHUB_SHA = 'valid-build-version'
1032+
process.env.INPUT_ERROR_COUNT = '10'
1033+
mockDeploymentStatus('deployment_in_progress', 2)
1034+
mockDeploymentStatus('succeed')
1035+
const randomSpy = jest.spyOn(Math, 'random').mockReturnValue(0.5)
1036+
1037+
const timeoutSpy = await runWithoutWaiting(createPendingDeployment())
1038+
1039+
expect(timeoutSpy).toHaveBeenNthCalledWith(1, expect.any(Function), 50)
1040+
expect(timeoutSpy).toHaveBeenNthCalledWith(2, expect.any(Function), 75)
1041+
expect(timeoutSpy).toHaveBeenNthCalledWith(3, expect.any(Function), 113)
1042+
1043+
timeoutSpy.mockRestore()
1044+
randomSpy.mockRestore()
1045+
delete process.env.INPUT_ERROR_COUNT
1046+
})
1047+
1048+
it('caps the successful status check backoff', async () => {
1049+
process.env.GITHUB_SHA = 'valid-build-version'
1050+
process.env.INPUT_ERROR_COUNT = '10'
1051+
process.env.INPUT_REPORTING_INTERVAL = '20000'
1052+
mockDeploymentStatus('deployment_in_progress')
1053+
mockDeploymentStatus('succeed')
1054+
const randomSpy = jest.spyOn(Math, 'random').mockReturnValue(0.5)
1055+
1056+
const timeoutSpy = await runWithoutWaiting(createPendingDeployment())
1057+
1058+
expect(timeoutSpy.mock.calls.map(([, interval]) => interval)).toEqual([20000, MAX_REPORTING_INTERVAL])
1059+
1060+
timeoutSpy.mockRestore()
1061+
randomSpy.mockRestore()
1062+
delete process.env.INPUT_ERROR_COUNT
1063+
delete process.env.INPUT_REPORTING_INTERVAL
1064+
})
1065+
1066+
it('jitters status check intervals by up to twenty percent', async () => {
1067+
process.env.GITHUB_SHA = 'valid-build-version'
1068+
process.env.INPUT_ERROR_COUNT = '10'
1069+
mockDeploymentStatus('deployment_in_progress')
1070+
mockDeploymentStatus('succeed')
1071+
const randomSpy = jest.spyOn(Math, 'random').mockReturnValueOnce(0).mockReturnValueOnce(1)
1072+
1073+
const timeoutSpy = await runWithoutWaiting(createPendingDeployment())
1074+
1075+
expect(timeoutSpy).toHaveBeenNthCalledWith(1, expect.any(Function), 40)
1076+
expect(timeoutSpy).toHaveBeenNthCalledWith(2, expect.any(Function), 90)
1077+
1078+
timeoutSpy.mockRestore()
1079+
randomSpy.mockRestore()
1080+
delete process.env.INPUT_ERROR_COUNT
1081+
})
1082+
1083+
it('keeps success backoff separate from error backoff', async () => {
1084+
process.env.GITHUB_SHA = 'valid-build-version'
1085+
process.env.INPUT_ERROR_COUNT = '10'
1086+
mockPool
1087+
.intercept({
1088+
path: `/repos/${process.env.GITHUB_REPOSITORY}/pages/deployments/${process.env.GITHUB_SHA}`,
1089+
method: 'GET'
1090+
})
1091+
.reply(500, {}, { headers: { 'content-type': 'application/json' } })
1092+
mockDeploymentStatus('deployment_in_progress')
1093+
mockDeploymentStatus('succeed')
1094+
const randomSpy = jest.spyOn(Math, 'random').mockReturnValue(0.5)
1095+
1096+
const timeoutSpy = await runWithoutWaiting(createPendingDeployment())
1097+
1098+
expect(timeoutSpy.mock.calls.map(([, interval]) => interval)).toEqual([50, 51, 75])
1099+
1100+
timeoutSpy.mockRestore()
1101+
randomSpy.mockRestore()
1102+
delete process.env.INPUT_ERROR_COUNT
1103+
})
9931104
})
9941105

9951106
describe('#cancel', () => {

‎src/internal/deployment.js‎

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,17 @@ const finalErrorStatus = {
2424
}
2525

2626
const MAX_TIMEOUT = 600000
27+
const MAX_REPORTING_INTERVAL = 30000
28+
const REPORTING_BACKOFF_MULTIPLIER = 1.5
29+
const REPORTING_JITTER_FACTOR = 0.2
2730
const ONE_GIGABYTE = 1073741824
2831
const SIZE_LIMIT_DESCRIPTION = '1 GB'
2932

33+
function getJitteredInterval(interval) {
34+
const jitter = interval * REPORTING_JITTER_FACTOR
35+
return Math.round(interval - jitter + Math.random() * jitter * 2)
36+
}
37+
3038
class Deployment {
3139
constructor() {
3240
const context = getContext()
@@ -138,7 +146,7 @@ class Deployment {
138146
}
139147

140148
const deploymentId = this.deploymentInfo.id || this.buildVersion
141-
const reportingInterval = Number(core.getInput('reporting_interval'))
149+
let reportingInterval = Number(core.getInput('reporting_interval'))
142150
const maxErrorCount = Number(core.getInput('error_count'))
143151

144152
let errorCount = 0
@@ -151,7 +159,7 @@ class Deployment {
151159
/*eslint no-constant-condition: ["error", { "checkLoops": false }]*/
152160
while (true) {
153161
// Handle reporting interval
154-
await new Promise(resolve => setTimeout(resolve, reportingInterval + errorReportingInterval))
162+
await new Promise(resolve => setTimeout(resolve, getJitteredInterval(reportingInterval + errorReportingInterval)))
155163

156164
// Check status
157165
try {
@@ -179,6 +187,10 @@ class Deployment {
179187

180188
// reset the error reporting interval once get the proper status back.
181189
errorReportingInterval = 0
190+
reportingInterval = Math.min(
191+
Math.round(reportingInterval * REPORTING_BACKOFF_MULTIPLIER),
192+
MAX_REPORTING_INTERVAL
193+
)
182194
} catch (error) {
183195
core.error(error.stack)
184196

@@ -242,4 +254,10 @@ class Deployment {
242254
}
243255
}
244256

245-
module.exports = { Deployment, MAX_TIMEOUT, ONE_GIGABYTE, SIZE_LIMIT_DESCRIPTION }
257+
module.exports = {
258+
Deployment,
259+
MAX_TIMEOUT,
260+
MAX_REPORTING_INTERVAL,
261+
ONE_GIGABYTE,
262+
SIZE_LIMIT_DESCRIPTION
263+
}

0 commit comments

Comments
 (0)