|
5 | 5 | "errors" |
6 | 6 | "fmt" |
7 | 7 | "log" |
| 8 | + "net" |
8 | 9 | gohttp "net/http" |
9 | 10 | "os" |
10 | 11 | "strings" |
@@ -76,6 +77,7 @@ import ( |
76 | 77 | "github.com/IBM-Cloud/bluemix-go/api/schematics" |
77 | 78 | "github.com/IBM-Cloud/bluemix-go/api/usermanagement/usermanagementv2" |
78 | 79 | "github.com/IBM-Cloud/bluemix-go/authentication" |
| 80 | + "github.com/IBM-Cloud/bluemix-go/bmxerror" |
79 | 81 | "github.com/IBM-Cloud/bluemix-go/http" |
80 | 82 | "github.com/IBM-Cloud/bluemix-go/rest" |
81 | 83 | bxsession "github.com/IBM-Cloud/bluemix-go/session" |
@@ -902,14 +904,30 @@ func (c *Config) ClientSession() (interface{}, error) { |
902 | 904 | if sess.BluemixSession.Config.BluemixAPIKey != "" { |
903 | 905 | err = authenticateAPIKey(sess.BluemixSession) |
904 | 906 | 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 | + } |
909 | 919 | } |
910 | 920 | err = authenticateCF(sess.BluemixSession) |
911 | 921 | 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 | + } |
913 | 931 | } |
914 | 932 | } |
915 | 933 |
|
@@ -1762,3 +1780,26 @@ func DefaultTransport() gohttp.RoundTripper { |
1762 | 1780 | } |
1763 | 1781 | return transport |
1764 | 1782 | } |
| 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