Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix returned EOF error when calling Nodes GC/GcAlloc API #5970

Merged
merged 1 commit into from
Mar 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"compress/gzip"
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"io"
"net"
Expand Down Expand Up @@ -930,8 +931,16 @@ func parseWriteMeta(resp *http.Response, q *WriteMeta) error {

// decodeBody is used to JSON decode a body
func decodeBody(resp *http.Response, out interface{}) error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we always skip decoding body if out == nil? I noticed that we wrap few decodeBody invocations with a nil check already:

nomad/api/api.go

Lines 858 to 862 in d890ddb

if out != nil {
if err := decodeBody(resp, &out); err != nil {
return nil, err
}
}
.

It would simplify caller code a bit, and it doesn't seem like an error if caller explicitly passes nil and ignores response body (though body may need to be consumed... a potential bug in current code).

dec := json.NewDecoder(resp.Body)
return dec.Decode(out)
switch resp.ContentLength {
case 0:
if out == nil {
return nil
}
return errors.New("Got 0 byte response with non-nil decode object")
default:
dec := json.NewDecoder(resp.Body)
return dec.Decode(out)
}
}

// encodeBody is used to encode a request body
Expand Down
6 changes: 2 additions & 4 deletions api/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,17 +413,15 @@ func (n *Nodes) Stats(nodeID string, q *QueryOptions) (*HostStats, error) {
}

func (n *Nodes) GC(nodeID string, q *QueryOptions) error {
var resp struct{}
path := fmt.Sprintf("/v1/client/gc?node_id=%s", nodeID)
_, err := n.client.query(path, &resp, q)
_, err := n.client.query(path, nil, q)
return err
}

// TODO Add tests
func (n *Nodes) GcAlloc(allocID string, q *QueryOptions) error {
var resp struct{}
path := fmt.Sprintf("/v1/client/allocation/%s/gc", allocID)
_, err := n.client.query(path, &resp, q)
_, err := n.client.query(path, nil, q)
return err
}

Expand Down