|
32 | 32 | // HTTPClientTimeout specifies a time limit for requests made by the |
33 | 33 | // HTTPClient. The timeout includes connection time, any redirects, |
34 | 34 | // and reading the response body. |
35 | | - HTTPClientTimeout = 30 * time.Second |
| 35 | + HTTPClientTimeout = 60 * time.Second |
36 | 36 | ) |
37 | 37 |
|
38 | 38 | // Client represents a connection with the APNs |
@@ -93,17 +93,32 @@ func (c *Client) Production() *Client { |
93 | 93 | // transparently before sending the notification. It will return a Response |
94 | 94 | // indicating whether the notification was accepted or rejected by the APNs |
95 | 95 | // gateway, or an error if something goes wrong. |
| 96 | +// |
| 97 | +// Use PushWithContext if you need better cancelation and timeout control. |
96 | 98 | func (c *Client) Push(n *Notification) (*Response, error) { |
97 | | - payload, err := json.Marshal(n) |
| 99 | + return c.PushWithContext(nil, n) |
| 100 | +} |
98 | 101 |
|
| 102 | +// PushWithContext sends a Notification to the APNs gateway. Context carries a |
| 103 | +// deadline and a cancelation signal and allows you to close long running |
| 104 | +// requests when the context timeout is exceeded. Context can be nil, for |
| 105 | +// backwards compatibility. |
| 106 | +// |
| 107 | +// If the underlying http.Client is not currently connected, this method will |
| 108 | +// attempt to reconnect transparently before sending the notification. It will |
| 109 | +// return a Response indicating whether the notification was accepted or |
| 110 | +// rejected by the APNs gateway, or an error if something goes wrong. |
| 111 | +func (c *Client) PushWithContext(ctx Context, n *Notification) (*Response, error) { |
| 112 | + payload, err := json.Marshal(n) |
99 | 113 | if err != nil { |
100 | 114 | return nil, err |
101 | 115 | } |
102 | 116 |
|
103 | 117 | url := fmt.Sprintf("%v/3/device/%v", c.Host, n.DeviceToken) |
104 | 118 | req, _ := http.NewRequest("POST", url, bytes.NewBuffer(payload)) |
105 | 119 | setHeaders(req, n) |
106 | | - httpRes, err := c.HTTPClient.Do(req) |
| 120 | + |
| 121 | + httpRes, err := c.requestWithContext(ctx, req) |
107 | 122 | if err != nil { |
108 | 123 | return nil, err |
109 | 124 | } |
|
0 commit comments