retry get nonce few times before return error

This commit is contained in:
Rekby 2016-04-11 07:22:00 +03:00
parent f32c8a55e7
commit 3a426a1382

View file

@ -11,8 +11,12 @@ import (
"gopkg.in/square/go-jose.v1"
"errors"
"time"
)
const TRY_COUNT = 10
const RETRY_PAUSE = time.Second
type jws struct {
directoryURL string
privKey crypto.PrivateKey
@ -97,14 +101,21 @@ func (j *jws) getNonce() error {
func (j *jws) Nonce() (string, error) {
nonce := ""
if len(j.nonces) == 0 {
err := j.getNonce()
if err != nil {
return nonce, err
}
if len(j.nonces) == 0 {
return "", errors.New("Can't get nonce")
for i := 0; i < TRY_COUNT; i++ {
err := j.getNonce()
if err != nil {
return nonce, err
}
if len(j.nonces) != 0 {
// get nonce ok and can continue
break
}
time.Sleep(RETRY_PAUSE);
}
}
if len(j.nonces) == 0 {
return "", errors.New("Can't get nonce")
}
nonce, j.nonces = j.nonces[len(j.nonces)-1], j.nonces[:len(j.nonces)-1]
return nonce, nil