Skip to content

Commit 8764b58

Browse files
authored
Merge pull request #2 from hkantare/retry/autehntication
Retry on authentication
2 parents 0a57446 + 269955b commit 8764b58

File tree

1 file changed

+46
-5
lines changed

1 file changed

+46
-5
lines changed

ibm/config.go

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"errors"
66
"fmt"
77
"log"
8+
"net"
89
gohttp "net/http"
910
"os"
1011
"strings"
@@ -76,6 +77,7 @@ import (
7677
"github.com/IBM-Cloud/bluemix-go/api/schematics"
7778
"github.com/IBM-Cloud/bluemix-go/api/usermanagement/usermanagementv2"
7879
"github.com/IBM-Cloud/bluemix-go/authentication"
80+
"github.com/IBM-Cloud/bluemix-go/bmxerror"
7981
"github.com/IBM-Cloud/bluemix-go/http"
8082
"github.com/IBM-Cloud/bluemix-go/rest"
8183
bxsession "github.com/IBM-Cloud/bluemix-go/session"
@@ -902,14 +904,30 @@ func (c *Config) ClientSession() (interface{}, error) {
902904
if sess.BluemixSession.Config.BluemixAPIKey != "" {
903905
err = authenticateAPIKey(sess.BluemixSession)
904906
if err != nil {
905-
session.bmxUserFetchErr = fmt.Errorf("Error occured while fetching auth key for account user details: %q", err)
906-
session.functionConfigErr = fmt.Errorf("Error occured while fetching auth key for function: %q", err)
907-
session.powerConfigErr = fmt.Errorf("Error occured while fetching the auth key for power iaas: %q", err)
908-
session.ibmpiConfigErr = fmt.Errorf("Error occured while fetching the auth key for power iaas: %q", err)
907+
for count := c.RetryCount; count >= 0; count-- {
908+
if err == nil || !isRetryable(err) {
909+
break
910+
}
911+
err = authenticateAPIKey(sess.BluemixSession)
912+
}
913+
if err != nil {
914+
session.bmxUserFetchErr = fmt.Errorf("Error occured while fetching auth key for account user details: %q", err)
915+
session.functionConfigErr = fmt.Errorf("Error occured while fetching auth key for function: %q", err)
916+
session.powerConfigErr = fmt.Errorf("Error occured while fetching the auth key for power iaas: %q", err)
917+
session.ibmpiConfigErr = fmt.Errorf("Error occured while fetching the auth key for power iaas: %q", err)
918+
}
909919
}
910920
err = authenticateCF(sess.BluemixSession)
911921
if err != nil {
912-
session.functionConfigErr = fmt.Errorf("Error occured while fetching auth key for function: %q", err)
922+
for count := c.RetryCount; count >= 0; count-- {
923+
if err == nil || !isRetryable(err) {
924+
break
925+
}
926+
err = authenticateCF(sess.BluemixSession)
927+
}
928+
if err != nil {
929+
session.functionConfigErr = fmt.Errorf("Error occured while fetching auth key for function: %q", err)
930+
}
913931
}
914932
}
915933

@@ -1762,3 +1780,26 @@ func DefaultTransport() gohttp.RoundTripper {
17621780
}
17631781
return transport
17641782
}
1783+
1784+
func isRetryable(err error) bool {
1785+
if bmErr, ok := err.(bmxerror.RequestFailure); ok {
1786+
switch bmErr.StatusCode() {
1787+
case 408, 504, 599, 429, 500, 502, 520, 503:
1788+
return true
1789+
}
1790+
}
1791+
1792+
if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
1793+
return true
1794+
}
1795+
1796+
if netErr, ok := err.(*net.OpError); ok && netErr.Timeout() {
1797+
return true
1798+
}
1799+
1800+
if netErr, ok := err.(net.UnknownNetworkError); ok && netErr.Timeout() {
1801+
return true
1802+
}
1803+
1804+
return false
1805+
}

0 commit comments

Comments
 (0)