Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
sethvargo committed Dec 14, 2022
1 parent 9eb639b commit c2e7428
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 172 deletions.
23 changes: 0 additions & 23 deletions .github/workflows/lock.yml

This file was deleted.

28 changes: 0 additions & 28 deletions .github/workflows/stale.yml

This file was deleted.

31 changes: 13 additions & 18 deletions .github/workflows/test.yml
@@ -1,35 +1,30 @@
name: Test
name: 'Test'

on:
push:
branches:
- main
- 'main'
tags:
- '*'
pull_request:
branches:
- main
- 'main'
workflow_dispatch:

concurrency:
group: '${{ github.workflow }}-${{ github.head_ref || github.ref }}'
cancel-in-progress: true

jobs:
test:
runs-on: ubuntu-latest
runs-on: 'ubuntu-latest'

steps:
- uses: actions/checkout@v2
- uses: 'actions/checkout@v3'

- uses: actions/setup-go@v2
- uses: actions/setup-go@v3
with:
go-version: '1.16'

- uses: actions/cache@v2
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- name: Lint
run: make fmtcheck staticcheck spellcheck

- name: Test
run: make test-acc
- name: 'Test'
run: 'make test'
39 changes: 1 addition & 38 deletions Makefile
@@ -1,45 +1,8 @@
VETTERS = "asmdecl,assign,atomic,bools,buildtag,cgocall,composites,copylocks,errorsas,httpresponse,loopclosure,lostcancel,nilfunc,printf,shift,stdmethods,structtag,tests,unmarshal,unreachable,unsafeptr,unusedresult"
GOFMT_FILES = $(shell go list -f '{{.Dir}}' ./...)

fmtcheck:
@command -v goimports > /dev/null 2>&1 || (cd tools && go get golang.org/x/tools/cmd/goimports)
@CHANGES="$$(goimports -d $(GOFMT_FILES))"; \
if [ -n "$${CHANGES}" ]; then \
echo "Unformatted (run goimports -w .):\n\n$${CHANGES}\n\n"; \
exit 1; \
fi
@# Annoyingly, goimports does not support the simplify flag.
@CHANGES="$$(gofmt -s -d $(GOFMT_FILES))"; \
if [ -n "$${CHANGES}" ]; then \
echo "Unformatted (run gofmt -s -w .):\n\n$${CHANGES}\n\n"; \
exit 1; \
fi
.PHONY: fmtcheck

spellcheck:
@command -v misspell > /dev/null 2>&1 || (cd tools && go get github.com/client9/misspell/cmd/misspell)
@misspell -locale="US" -error -source="text" **/*
.PHONY: spellcheck

staticcheck:
@command -v staticcheck > /dev/null 2>&1 || (cd tools && go get honnef.co/go/tools/cmd/staticcheck)
@staticcheck -checks="all" -tests $(GOFMT_FILES)
.PHONY: staticcheck

test:
@go test \
-count=1 \
-race \
-short \
-timeout=5m \
-vet="${VETTERS}" \
./...
.PHONY: test

test-acc:
@go test \
-count=1 \
-race \
-timeout=10m \
-vet="${VETTERS}" \
./...
.PHONY: test-acc
7 changes: 4 additions & 3 deletions backoff.go
@@ -1,7 +1,6 @@
package retry

import (
"math/rand"
"sync"
"time"
)
Expand All @@ -27,7 +26,8 @@ func (b BackoffFunc) Next() (time.Duration, bool) {
// returned 20s, the value could be between 15 and 25 seconds. The value can
// never be less than 0.
func WithJitter(j time.Duration, next Backoff) Backoff {
r := &lockedSource{src: rand.New(rand.NewSource(time.Now().UnixNano()))}
r := newLockedRandom(time.Now().UnixNano())

return BackoffFunc(func() (time.Duration, bool) {
val, stop := next.Next()
if stop {
Expand All @@ -48,7 +48,8 @@ func WithJitter(j time.Duration, next Backoff) Backoff {
// the backoff returned 20s, the value could be between 19 and 21 seconds. The
// value can never be less than 0 or greater than 100.
func WithJitterPercent(j uint64, next Backoff) Backoff {
r := &lockedSource{src: rand.New(rand.NewSource(time.Now().UnixNano()))}
r := newLockedRandom(time.Now().UnixNano())

return BackoffFunc(func() (time.Duration, bool) {
val, stop := next.Next()
if stop {
Expand Down
18 changes: 11 additions & 7 deletions rand.go
Expand Up @@ -7,29 +7,33 @@ import (

type lockedSource struct {
src *rand.Rand
lk sync.Mutex
mu sync.Mutex
}

var _ rand.Source64 = (*lockedSource)(nil)

func newLockedRandom(seed int64) *lockedSource {
return &lockedSource{src: rand.New(rand.NewSource(seed))}
}

// Int63 mimics math/rand.(*Rand).Int63 with mutex locked.
func (r *lockedSource) Int63() int64 {
r.lk.Lock()
defer r.lk.Unlock()
r.mu.Lock()
defer r.mu.Unlock()
return r.src.Int63()
}

// Seed mimics math/rand.(*Rand).Seed with mutex locked.
func (r *lockedSource) Seed(seed int64) {
r.lk.Lock()
defer r.lk.Unlock()
r.mu.Lock()
defer r.mu.Unlock()
r.src.Seed(seed)
}

// Uint64 mimics math/rand.(*Rand).Uint64 with mutex locked.
func (r *lockedSource) Uint64() uint64 {
r.lk.Lock()
defer r.lk.Unlock()
r.mu.Lock()
defer r.mu.Unlock()
return r.src.Uint64()
}

Expand Down
9 changes: 0 additions & 9 deletions tools/go.mod

This file was deleted.

37 changes: 0 additions & 37 deletions tools/go.sum

This file was deleted.

9 changes: 0 additions & 9 deletions tools/tools.go

This file was deleted.

0 comments on commit c2e7428

Please sign in to comment.