Skip to content

Commit

Permalink
OPTIM: Improvements to operation of alive handler.
Browse files Browse the repository at this point in the history
  • Loading branch information
jwrosewell committed Sep 1, 2021
1 parent 929772a commit 8c56872
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 17 deletions.
32 changes: 21 additions & 11 deletions aliveService.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
}

Expand All @@ -126,5 +126,6 @@ func decompress(b []byte) ([]byte, error) {
if err != nil {
return nil, err
}
defer z.Close()
return ioutil.ReadAll(z)
}
13 changes: 11 additions & 2 deletions handlerAlive.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 8c56872

Please sign in to comment.