X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/516685e09227ab64c2d4f7fd04d4b60a75fc5d0f..397981dadc145225c691c8643b10527c9710f1fb:/sdk/go/keepclient/keepclient.go diff --git a/sdk/go/keepclient/keepclient.go b/sdk/go/keepclient/keepclient.go index 6a3e83796e..d600322e6a 100644 --- a/sdk/go/keepclient/keepclient.go +++ b/sdk/go/keepclient/keepclient.go @@ -2,7 +2,8 @@ // // SPDX-License-Identifier: Apache-2.0 -/* Provides low-level Get/Put primitives for accessing Arvados Keep blocks. */ +// Package keepclient provides low-level Get/Put primitives for accessing +// Arvados Keep blocks. package keepclient import ( @@ -20,12 +21,12 @@ import ( "sync" "time" - "git.curoverse.com/arvados.git/sdk/go/arvadosclient" - "git.curoverse.com/arvados.git/sdk/go/asyncbuf" - "git.curoverse.com/arvados.git/sdk/go/httpserver" + "git.arvados.org/arvados.git/sdk/go/arvadosclient" + "git.arvados.org/arvados.git/sdk/go/asyncbuf" + "git.arvados.org/arvados.git/sdk/go/httpserver" ) -// A Keep "block" is 64MB. +// BLOCKSIZE defines the length of a Keep "block", which is 64MB. const BLOCKSIZE = 64 * 1024 * 1024 var ( @@ -82,8 +83,8 @@ var ErrNoSuchKeepServer = errors.New("No keep server matching the given UUID is // ErrIncompleteIndex is returned when the Index response does not end with a new empty line var ErrIncompleteIndex = errors.New("Got incomplete index") -const X_Keep_Desired_Replicas = "X-Keep-Desired-Replicas" -const X_Keep_Replicas_Stored = "X-Keep-Replicas-Stored" +const XKeepDesiredReplicas = "X-Keep-Desired-Replicas" +const XKeepReplicasStored = "X-Keep-Replicas-Stored" type HTTPClient interface { Do(*http.Request) (*http.Response, error) @@ -139,7 +140,7 @@ func New(arv *arvadosclient.ArvadosClient) *KeepClient { } } -// Put a block given the block hash, a reader, and the number of bytes +// PutHR puts a block given the block hash, a reader, and the number of bytes // to read from the reader (which must be between 0 and BLOCKSIZE). // // Returns the locator for the written block, the number of replicas @@ -191,11 +192,11 @@ func (kc *KeepClient) PutB(buffer []byte) (string, int, error) { // // If the block hash and data size are known, PutHR is more efficient. func (kc *KeepClient) PutR(r io.Reader) (locator string, replicas int, err error) { - if buffer, err := ioutil.ReadAll(r); err != nil { + buffer, err := ioutil.ReadAll(r) + if err != nil { return "", 0, err - } else { - return kc.PutB(buffer) } + return kc.PutB(buffer) } func (kc *KeepClient) getOrHead(method string, locator string, header http.Header) (io.ReadCloser, int64, string, http.Header, error) { @@ -216,7 +217,7 @@ func (kc *KeepClient) getOrHead(method string, locator string, header http.Heade var errs []string - tries_remaining := 1 + kc.Retries + triesRemaining := 1 + kc.Retries serversToTry := kc.getSortedRoots(locator) @@ -225,8 +226,8 @@ func (kc *KeepClient) getOrHead(method string, locator string, header http.Heade var retryList []string - for tries_remaining > 0 { - tries_remaining -= 1 + for triesRemaining > 0 { + triesRemaining -= 1 retryList = nil for _, host := range serversToTry { @@ -290,10 +291,9 @@ func (kc *KeepClient) getOrHead(method string, locator string, header http.Heade Hash: md5.New(), Check: locator[0:32], }, expectLength, url, resp.Header, nil - } else { - resp.Body.Close() - return nil, expectLength, url, resp.Header, nil } + resp.Body.Close() + return nil, expectLength, url, resp.Header, nil } serversToTry = retryList } @@ -493,9 +493,8 @@ func (kc *KeepClient) getSortedRoots(locator string) []string { func (kc *KeepClient) cache() *BlockCache { if kc.BlockCache != nil { return kc.BlockCache - } else { - return DefaultBlockCache } + return DefaultBlockCache } func (kc *KeepClient) ClearBlockCache() { @@ -546,31 +545,26 @@ func (kc *KeepClient) httpClient() HTTPClient { keepAlive = DefaultKeepAlive } - transport, ok := http.DefaultTransport.(*http.Transport) - if ok { - copy := *transport - transport = © - } else { - // Evidently the application has replaced - // http.DefaultTransport with a different type, so we - // need to build our own from scratch using the Go 1.8 - // defaults. - transport = &http.Transport{ + c := &http.Client{ + Timeout: requestTimeout, + // It's not safe to copy *http.DefaultTransport + // because it has a mutex (which might be locked) + // protecting a private map (which might not be nil). + // So we build our own, using the Go 1.12 default + // values, ignoring any changes the application has + // made to http.DefaultTransport. + Transport: &http.Transport{ + DialContext: (&net.Dialer{ + Timeout: connectTimeout, + KeepAlive: keepAlive, + DualStack: true, + }).DialContext, MaxIdleConns: 100, IdleConnTimeout: 90 * time.Second, - ExpectContinueTimeout: time.Second, - } - } - transport.DialContext = (&net.Dialer{ - Timeout: connectTimeout, - KeepAlive: keepAlive, - DualStack: true, - }).DialContext - transport.TLSHandshakeTimeout = tlsTimeout - transport.TLSClientConfig = arvadosclient.MakeTLSConfig(kc.Arvados.ApiInsecure) - c := &http.Client{ - Timeout: requestTimeout, - Transport: transport, + TLSHandshakeTimeout: tlsTimeout, + ExpectContinueTimeout: 1 * time.Second, + TLSClientConfig: arvadosclient.MakeTLSConfig(kc.Arvados.ApiInsecure), + }, } defaultClient[kc.Arvados.ApiInsecure][kc.foundNonDiskSvc] = c return c @@ -581,9 +575,8 @@ var reqIDGen = httpserver.IDGenerator{Prefix: "req-"} func (kc *KeepClient) getRequestID() string { if kc.RequestID != "" { return kc.RequestID - } else { - return reqIDGen.Next() } + return reqIDGen.Next() } type Locator struct {