[ARVADOS] updated: ffdfd0ac6e501514c5327c2d6b5f8507bb6353bc

git at public.curoverse.com git at public.curoverse.com
Thu Nov 5 20:15:40 EST 2015


Summary of changes:
 sdk/go/arvadosclient/arvadosclient.go      | 56 ++++++++++++++----------------
 sdk/go/arvadosclient/arvadosclient_test.go |  2 ++
 2 files changed, 28 insertions(+), 30 deletions(-)

       via  ffdfd0ac6e501514c5327c2d6b5f8507bb6353bc (commit)
      from  4197567e7b1fe768896ea6af9c4160b43e8521dd (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.


commit ffdfd0ac6e501514c5327c2d6b5f8507bb6353bc
Author: radhika <radhika at curoverse.com>
Date:   Thu Nov 5 20:13:32 2015 -0500

    5538: code improvements; use switch statement instead of if statement with several status code checks, sleep between retries.

diff --git a/sdk/go/arvadosclient/arvadosclient.go b/sdk/go/arvadosclient/arvadosclient.go
index a5d5a02..68c0632 100644
--- a/sdk/go/arvadosclient/arvadosclient.go
+++ b/sdk/go/arvadosclient/arvadosclient.go
@@ -32,6 +32,8 @@ var ErrInvalidArgument = errors.New("Invalid argument")
 // such failures by always using a new or recently active socket.
 var MaxIdleConnectionDuration = 30 * time.Second
 
+var RetryDelay = 2 * time.Second
+
 // Indicates an error that was returned by the API server.
 type APIServerError struct {
 	// Address of server returning error, of the form "host:port".
@@ -161,14 +163,26 @@ func (c ArvadosClient) CallRaw(method string, resourceType string, uuid string,
 		}
 	}
 
+	retryable := false
+	switch method {
+	case "GET", "HEAD", "PUT", "OPTIONS", "POST", "DELETE":
+		retryable = true
+	}
+
+	// POST and DELETE are not safe to retry automatically, so we minimize
+	// such failures by always using a new or recently active socket
+	if method == "POST" || method == "DELETE" {
+		if time.Since(c.lastClosedIdlesAt) > MaxIdleConnectionDuration {
+			c.lastClosedIdlesAt = time.Now()
+			c.Client.Transport.(*http.Transport).CloseIdleConnections()
+		}
+	}
+
 	// Make the request
-	remainingTries := 1 + c.Retries
 	var req *http.Request
 	var resp *http.Response
-	var errs []string
-	var badResp bool
 
-	for remainingTries > 0 {
+	for attempt := 0; attempt <= c.Retries; attempt++ {
 		if method == "GET" || method == "HEAD" {
 			u.RawQuery = vals.Encode()
 			if req, err = http.NewRequest(method, u.String(), nil); err != nil {
@@ -187,21 +201,10 @@ func (c ArvadosClient) CallRaw(method string, resourceType string, uuid string,
 			req.Header.Add("X-External-Client", "1")
 		}
 
-		// POST and DELETE are not safe to retry automatically, so we minimize
-		// such failures by always using a new or recently active socket
-		if method == "POST" || method == "DELETE" {
-			if time.Since(c.lastClosedIdlesAt) > MaxIdleConnectionDuration {
-				c.lastClosedIdlesAt = time.Now()
-				c.Client.Transport.(*http.Transport).CloseIdleConnections()
-			}
-		}
-
 		resp, err = c.Client.Do(req)
 		if err != nil {
-			if method == "GET" || method == "HEAD" || method == "PUT" {
-				errs = append(errs, err.Error())
-				badResp = false
-				remainingTries -= 1
+			if retryable {
+				time.Sleep(RetryDelay)
 				continue
 			} else {
 				return nil, err
@@ -214,26 +217,19 @@ func (c ArvadosClient) CallRaw(method string, resourceType string, uuid string,
 
 		defer resp.Body.Close()
 
-		if resp.StatusCode == 408 ||
-			resp.StatusCode == 409 ||
-			resp.StatusCode == 422 ||
-			resp.StatusCode == 423 ||
-			resp.StatusCode == 500 ||
-			resp.StatusCode == 502 ||
-			resp.StatusCode == 503 ||
-			resp.StatusCode == 504 {
-			badResp = true
-			remainingTries -= 1
+		switch resp.StatusCode {
+		case 408, 409, 422, 423, 500, 502, 503, 504:
+			time.Sleep(RetryDelay)
 			continue
-		} else {
+		default:
 			return nil, newAPIServerError(c.ApiServer, resp)
 		}
 	}
 
-	if badResp {
+	if resp != nil {
 		return nil, newAPIServerError(c.ApiServer, resp)
 	} else {
-		return nil, fmt.Errorf("%v", errs)
+		return nil, err
 	}
 }
 
diff --git a/sdk/go/arvadosclient/arvadosclient_test.go b/sdk/go/arvadosclient/arvadosclient_test.go
index c026a88..20cac55 100644
--- a/sdk/go/arvadosclient/arvadosclient_test.go
+++ b/sdk/go/arvadosclient/arvadosclient_test.go
@@ -26,6 +26,7 @@ type ServerRequiredSuite struct{}
 func (s *ServerRequiredSuite) SetUpSuite(c *C) {
 	arvadostest.StartAPI()
 	arvadostest.StartKeep(2, false)
+	RetryDelay = 0
 }
 
 func (s *ServerRequiredSuite) SetUpTest(c *C) {
@@ -226,6 +227,7 @@ func (s *UnitSuite) TestPDHMatch(c *C) {
 type MockArvadosServerSuite struct{}
 
 func (s *MockArvadosServerSuite) SetUpSuite(c *C) {
+	RetryDelay = 0
 }
 
 func (s *MockArvadosServerSuite) SetUpTest(c *C) {

-----------------------------------------------------------------------


hooks/post-receive
-- 




More information about the arvados-commits mailing list