-
Notifications
You must be signed in to change notification settings - Fork 335
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
request with context #57
Conversation
3176f9f
to
9593f63
Compare
…olang version and context package
b2dabee
to
16b818e
Compare
/cc @sideshow |
@xjewer this looks really good, Thanks for the PR |
@@ -0,0 +1,50 @@ | |||
// +build go1.7 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's better to use !go1.6 here for Go >=1.8 compatibility. Anyway Go < 1.6 is not supported by this lib
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is okay. "from Go version 1.7 onward" https://golang.org/pkg/go/build/
Though I don't know if it's needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it makes sense to support context. @sideshow do you agree?
Maybe this could be done in a cleaner way though (see below)
// gateway, or an error if something goes wrong. | ||
// | ||
// Context with deadline allows to close long request when context timeout exceed. It can be nil, for back compatibility | ||
func (c *Client) PushWithCtx(n *Notification, ctx context.Context) (*Response, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There should be a way to minimize the duplication between the Go 1.6 and Go 1.7+ versions of this.
context.Context
is an interface with the same definition in both packages, so they are interchangeable.
https://golang.org/pkg/context/#Context
https://godoc.org/golang.org/x/net/context#Context
I'm not sure the best way to do this, but even redefining the Context
interface locally should allow either one to be passed in.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure, that it costs the time to try to reduce a duplication in this part of code
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem with the duplication is that future changes need to made in both places. If they are not, the code could diverge in behaviour between Go versions. If there isn't any other option, good tests and continuous integration on multiple Go versions can help.
I await a verdict/suggestions from @sideshow.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
client_test.go
Outdated
@@ -16,6 +16,7 @@ import ( | |||
apns "github.com/sideshow/apns2" | |||
"github.com/sideshow/apns2/certificate" | |||
"github.com/stretchr/testify/assert" | |||
"golang.org/x/net/context" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be good to see two versions of the context tests in separate files with appropriate build tags, just to ensure that it works with either package.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
@@ -93,31 +90,10 @@ func (c *Client) Production() *Client { | |||
// transparently before sending the notification. It will return a Response | |||
// indicating whether the notification was accepted or rejected by the APNs | |||
// gateway, or an error if something goes wrong. | |||
// | |||
// It wraps PushWithCtx for back compatibility. | |||
func (c *Client) Push(n *Notification) (*Response, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sideshow How important is API stability for this project? Do you want to keep the no-context version around?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think its pretty important to keep this old version around @nathany just so we don't break other projects that depend on it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// gateway, or an error if something goes wrong. | ||
// | ||
// Context with deadline allows to close long request when context timeout exceed. It can be nil, for back compatibility | ||
func (c *Client) PushWithCtx(n *Notification, ctx context.Context) (*Response, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"golang.org/x/net/context" | ||
) | ||
|
||
func TestClient_PushWithCtx_WithTimeout(t *testing.T) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any chance we could change this to TestClientPushWithCtxWithTimeout just for consistency
@@ -93,31 +90,10 @@ func (c *Client) Production() *Client { | |||
// transparently before sending the notification. It will return a Response | |||
// indicating whether the notification was accepted or rejected by the APNs | |||
// gateway, or an error if something goes wrong. | |||
// | |||
// It wraps PushWithCtx for back compatibility. | |||
func (c *Client) Push(n *Notification) (*Response, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think its pretty important to keep this old version around @nathany just so we don't break other projects that depend on it
With context you can to cancel request when it goes longer than necessary https://godoc.org/context#WithTimeout
import "context"
works only >= go1.7, so i divided PushWithCtx method along with build tags!go1.7
andgo1.7
into the distinct files with "net/context" and "context" respectively