Skip to content

Commit

Permalink
feat: add retry for preparing data
Browse files Browse the repository at this point in the history
Signed-off-by: He Weiwei <[email protected]>
  • Loading branch information
heww committed Aug 20, 2021
1 parent 39f6d05 commit be76655
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
4 changes: 2 additions & 2 deletions scripts/data/04-artifact.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import harbor from 'k6/x/harbor'
import { ContentStore } from 'k6/x/harbor'

import { Settings } from '../config.js'
import { getProjectName, getRepositoryName, getArtifactTag } from '../helpers.js'
import { getProjectName, getRepositoryName, getArtifactTag, retry } from '../helpers.js'

const settings = Settings()

Expand Down Expand Up @@ -80,7 +80,7 @@ export default function () {
}

try {
harbor.push({ ref, store, blobs })
retry(() => harbor.push({ ref, store, blobs }))
successRate.add(true)
} catch (e) {
successRate.add(false)
Expand Down
4 changes: 2 additions & 2 deletions scripts/data/05-artifact-tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import counter from 'k6/x/counter'
import harbor from 'k6/x/harbor'

import { Settings } from '../config.js'
import { getProjectName, getRepositoryName, getArtifactTag, getArtifactNewTag } from '../helpers.js'
import { getProjectName, getRepositoryName, getArtifactTag, getArtifactNewTag, retry } from '../helpers.js'

const settings = Settings()

Expand Down Expand Up @@ -74,7 +74,7 @@ export default function () {
const t = newTags[i]

try {
harbor.createArtifactTag(t.projectName, t.repositoryName, t.tag, t.newTag)
retry(() => harbor.createArtifactTag(t.projectName, t.repositoryName, t.tag, t.newTag))
successRate.add(true)
} catch (e) {
successRate.add(false)
Expand Down
4 changes: 2 additions & 2 deletions scripts/data/06-audit-log.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import counter from 'k6/x/counter'
import harbor from 'k6/x/harbor'

import { Settings } from '../config.js'
import { randomItem, getProjectName, getRepositoryName, getArtifactTag } from '../helpers.js'
import { getProjectName, getRepositoryName, getArtifactTag, randomItem, retry } from '../helpers.js'

const settings = Settings()

Expand Down Expand Up @@ -63,7 +63,7 @@ export default function () {
const ref = `${a.projectName}/${a.repositoryName}:${a.tag}`

try {
harbor.getManifest(ref)
retry(() => harbor.getManifest(ref))
successRate.add(true)
} catch (e) {
successRate.add(false)
Expand Down
27 changes: 27 additions & 0 deletions scripts/helpers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

import harbor from 'k6/x/harbor'
import { sleep } from 'k6'

export function fetchAritfacts(projectName, repositoryName, count=-1) {
let page = 1
Expand Down Expand Up @@ -178,3 +179,29 @@ export function getArtifactTag(settings, i) {
export function getArtifactNewTag(settings, tagIndex, newTagIndex) {
return `${getArtifactTag(settings, tagIndex)}p${numberToPadString(newTagIndex+1, settings.ArtifactTagsCountPerArtifact)}`
}

export function retry(f, opts = {}) {
var DEFAULT_TIMES = 5

var options = {
times: +opts.times || DEFAULT_TIMES,
intervalFunc: typeof opts.interval === 'function' ? opts.interval : () => (+t.interval || randomIntBetween(1, 5))
}

var attempt = 1;

while (attempt++ < options.times) {
try {
return f()
} catch (e) {
const delay = options.intervalFunc(attempt)
if (delay > 0) {
sleep(d)
}

if (attempts >= options.times) {
throw e
}
}
}
}

0 comments on commit be76655

Please sign in to comment.