From 8c56872760413aa6ae36589fe1cadcdceb79b9e4 Mon Sep 17 00:00:00 2001 From: James Rosewell Date: Wed, 1 Sep 2021 14:06:43 +0100 Subject: [PATCH] OPTIM: Improvements to operation of alive handler. --- aliveService.go | 32 +++++++++++++++++++++----------- crypto.go | 7 ++++--- handlerAlive.go | 13 +++++++++++-- node.go | 2 +- 4 files changed, 37 insertions(+), 17 deletions(-) diff --git a/aliveService.go b/aliveService.go index 460c0fb..6e9b998 100644 --- a/aliveService.go +++ b/aliveService.go @@ -88,11 +88,20 @@ func (a *aliveService) aliveLoop() { a.ticker = time.NewTicker(a.pollingInterval) for _ = range a.ticker.C { a.ticker.Stop() - for _, n := range a.store.nodes { + a.pollNodes(c) + a.ticker.Reset(a.pollingInterval) + } +} + +// pollNodes gets the latest copy of all the nodes and polls each one if it's +// last accessed time is older than the polling interval. +func (a *aliveService) pollNodes(c *http.Client) { + ns, err := a.store.getAllNodes() + if err == nil { + for _, n := range ns { a.pollNode(n, c) } c.CloseIdleConnections() - a.ticker.Reset(a.pollingInterval) } } @@ -165,29 +174,30 @@ func (a *aliveService) callAlive( c *http.Client, d []byte) ([]byte, error) { + // Construct the URL for the alive service endpoint. url := url.URL{ Scheme: a.config.Scheme, Host: n.domain, Path: "/swift/api/v1/alive", } - req, err := http.NewRequest("POST", url.String(), bytes.NewBuffer(d)) + // Use the client provided to post the byte array. + r, err := c.Post( + url.String(), + "application/octet-stream", + bytes.NewBuffer(d)) if err != nil { return nil, err } - r, err := c.Do(req) + // Read the response and return it. + b, err := ioutil.ReadAll(r.Body) if err != nil { return nil, err } - defer r.Body.Close() + r.Body.Close() - body, err := ioutil.ReadAll(r.Body) - if err != nil { - return nil, err - } - - return body, nil + return b, nil } // nonce returns a new nonce generated using crpyto/rand diff --git a/crypto.go b/crypto.go index 7aed039..7448d4f 100644 --- a/crypto.go +++ b/crypto.go @@ -51,7 +51,7 @@ func (x *crypto) decrypt(b []byte) ([]byte, error) { nonceSize := x.gcm.NonceSize() if len(b) < nonceSize { return nil, fmt.Errorf( - "Data length '%d' shorter than nonce '%d'", + "data length '%d' shorter than nonce '%d'", len(b), nonceSize) } @@ -110,13 +110,13 @@ func compress(b []byte) ([]byte, error) { if err != nil { return nil, err } + z.Close() if i != len(b) { return nil, fmt.Errorf( - "Byte written '%d' does not match length '%d", + "byte written '%d' does not match length '%d", i, len(b)) } - z.Close() return o.Bytes(), nil } @@ -126,5 +126,6 @@ func decompress(b []byte) ([]byte, error) { if err != nil { return nil, err } + defer z.Close() return ioutil.ReadAll(z) } diff --git a/handlerAlive.go b/handlerAlive.go index 179ffc4..5c48f8a 100644 --- a/handlerAlive.go +++ b/handlerAlive.go @@ -30,17 +30,25 @@ func handlerAlive(s *Services) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { // Get the body bytes from the request. - body, err := ioutil.ReadAll(r.Body) + b, err := ioutil.ReadAll(r.Body) if err != nil { returnAPIError(s, w, err, http.StatusInternalServerError) return } + r.Body.Close() // Get the node associated with the request. n := s.store.getNode(r.Host) + if n == nil { + returnAPIError( + s, + w, + fmt.Errorf("no node for '%s'", r.Host), + http.StatusBadRequest) + } // Decode the body to form the decrypted byte array. - decrypted, err := n.Decrypt(body) + decrypted, err := n.Decrypt(b) if err != nil { returnAPIError(s, w, err, http.StatusBadRequest) return @@ -49,6 +57,7 @@ func handlerAlive(s *Services) http.HandlerFunc { // Return the decrypted information uncompressed. w.Header().Set("Content-Type", "application/octet-stream") w.Header().Set("Cache-Control", "no-cache") + w.Header().Set("Cache-Length", fmt.Sprintf("%d", len(decrypted))) l, err := w.Write(decrypted) if err != nil { returnAPIError(s, w, err, http.StatusInternalServerError) diff --git a/node.go b/node.go index 7e8a552..ae7d418 100644 --- a/node.go +++ b/node.go @@ -164,7 +164,7 @@ func (n *node) DecryptAndDecode(d []byte) (*Results, error) { return nil, err } if b == nil { - return nil, fmt.Errorf("Could not decrypt byte array") + return nil, fmt.Errorf("could not decrypt byte array") } // Decode the byte array to become a results array.