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