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

go/clientConnector: fix signal handling, non-2XX HTTP responses, redundant body reading #16

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

odeke-em
Copy link

@odeke-em odeke-em commented Dec 5, 2022

This change adds proper signal handling by passing in a buffered signal channel to signal.Notify as it mandates at https://pkg.go.dev/os/signal#Notify It also handles non-2XX codes and reports this while printing the content and returning.
While here also added resp.Body.Close() to avoid leaking HTTP responses. Also removes redundant bytes.Buffer.ReadFrom, then .String() and string(newMsg) by simply using io/ioutil.ReadAll(resp.Body) given we aren't sure which version of Go being used but really in >=Go1.16, we can use io.ReadAll

Fixes #13
Fixes #14
Fixes #15

…ndant body reading

This change adds proper signal handling by passing in a buffered
signal channel to signal.Notify as it mandates at https://pkg.go.dev/os/signal#Notify
It also handles non-2XX codes and reports this while printing the
content and returning.
While here also added resp.Body.Close() to avoid leaking HTTP responses.
Also removes redundant bytes.Buffer.ReadFrom, then .String() and string(newMsg)
by simply using io/ioutil.ReadAll(resp.Body) given we aren't sure which
version of Go being used but really in >=Go1.16, we can use io.ReadAll

Fixes danielgross#13
Fixes danielgross#14
Fixes danielgross#15

Signed-off-by: Emmanuel T Odeke <[email protected]>
@odeke-em odeke-em force-pushed the fix-connetor-HTTP.Response+signal.Notify branch from 44b43be to e200266 Compare December 5, 2022 04:45
buf.ReadFrom(resp.Body)
newMsg := buf.String()
newMsg, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()

Choose a reason for hiding this comment

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

This should be deferred... no?

Copy link
Author

Choose a reason for hiding this comment

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

No need to, this code closes it immediately after use. A defer will keep it open until the function returns or panics and then that's when the close will happen.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants