File size: 658 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package auth

import (
	"context"
	cRand "crypto/rand"
	"encoding/binary"
	"math/rand"
	"time"
)

var failRand *rand.Rand

const (
	delayMax = 10 * time.Millisecond
)

func init() {
	var seed int64
	err := binary.Read(cRand.Reader, binary.BigEndian, &seed)
	if err != nil {
		panic(err)
	}
	failRand = rand.New(rand.NewSource(seed))
}

// Delay will block for a random delay (or until the context is Done).
//
// It is useful in situations where there has been an auth failure.
func Delay(ctx context.Context) {
	dur := time.Duration(failRand.Int63n(int64(delayMax)))
	t := time.NewTicker(dur)
	defer t.Stop()
	select {
	case <-ctx.Done():
	case <-t.C:
	}
}