Skip to content

Commit

Permalink
rand delay
Browse files Browse the repository at this point in the history
  • Loading branch information
awill1988 committed Oct 30, 2024
1 parent 00bd28e commit 2d0f13e
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions jwk/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import (
"context"
"crypto/ecdsa"
"crypto/ed25519"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/json"
"encoding/pem"
"math/big"
"sync"
"time"

Expand All @@ -29,7 +31,13 @@ import (

var mapLock sync.RWMutex
var locks = map[string]*sync.RWMutex{}
var lockDelay = 3 * time.Millisecond

func lockDelay() (duration time.Duration) {
if n, err := rand.Int(rand.Reader, big.NewInt(3)); err == nil {
duration = time.Duration(n.Int64()) * time.Millisecond
}
return
}

func getLock(set string) *sync.RWMutex {
mapLock.Lock()
Expand All @@ -56,7 +64,7 @@ func GetOrGenerateKeys(ctx context.Context, r InternalRegistry, m Manager, set,
return nil, err
}
} else {
time.Sleep(lockDelay)
time.Sleep(lockDelay())
return GetOrGenerateKeys(ctx, r, m, set, kid, alg)
}
} else if err != nil {
Expand Down Expand Up @@ -84,7 +92,7 @@ func GetOrGenerateKeys(ctx context.Context, r InternalRegistry, m Manager, set,
return privKey, nil
}

time.Sleep(lockDelay)
time.Sleep(lockDelay())
return GetOrGenerateKeys(ctx, r, m, set, kid, alg)
}

Expand Down

0 comments on commit 2d0f13e

Please sign in to comment.