forked from alexjlockwood/gcm
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sender.go
236 lines (213 loc) · 7.25 KB
/
sender.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// Google Cloud Messaging for application servers implemented using the
// Go programming language.
package gcm
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"math/rand"
"net/http"
"time"
)
const (
// GcmSendEndpoint is the endpoint for sending messages to the GCM server.
// DT: Replaced GCM endpoint with FCM. Al other references remain the same
GcmSendEndpoint = "https://fcm.googleapis.com/fcm/send"
// Initial delay before first retry, without jitter.
backoffInitialDelay = 1000
// Maximum delay before a retry.
maxBackoffDelay = 1024000
)
// Declared as a mutable variable for testing purposes.
var gcmSendEndpoint = GcmSendEndpoint
// Errors
type JSONParseError struct{ error }
type UnauthorizedError struct{ error }
type UnknownError struct{ error }
const (
ResponseErrorMissingRegistration = "MissingRegistration"
ResponseErrorInvalidRegistration = "InvalidRegistration"
ResponseErrorMismatchSenderID = "MismatchSenderId"
ResponseErrorNotRegistered = "NotRegistered"
ResponseErrorMessageTooBig = "MessageTooBig"
ResponseErrorInvalidDataKey = "InvalidDataKey"
ResponseErrorInvalidTTL = "InvalidTtl"
ResponseErrorUnavailable = "Unavailable"
ResponseErrorInternalServerError = "InternalServerError"
ResponseErrorInvalidPackageName = "InvalidPackageName"
)
// Sender abstracts the interaction between the application server and the
// GCM server. The developer must obtain an API key from the Google APIs
// Console page and pass it to the Sender so that it can perform authorized
// requests on the application server's behalf. To send a message to one or
// more devices use the Sender's Send or SendNoRetry methods.
//
// If the Http field is nil, a zeroed http.Client will be allocated and used
// to send messages. If your application server runs on Google AppEngine,
// you must use the "appengine/urlfetch" package to create the *http.Client
// as follows:
//
// func handler(w http.ResponseWriter, r *http.Request) {
// c := appengine.NewContext(r)
// client := urlfetch.Client(c)
// sender := &gcm.Sender{ApiKey: key, Http: client}
//
// /* ... */
// }
type Sender struct {
ApiKey string
Http *http.Client
}
// SendNoRetry sends a message to the GCM server without retrying in case of
// service unavailability. A non-nil error is returned if a non-recoverable
// error occurs (i.e. if the response status is not "200 OK").
func (s *Sender) SendNoRetry(msg *Message) (*Response, error) {
if err := checkSender(s); err != nil {
return nil, err
} else if err := checkMessage(msg); err != nil {
return nil, err
}
data, err := json.Marshal(msg)
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", gcmSendEndpoint, bytes.NewBuffer(data))
if err != nil {
return nil, err
}
req.Header.Add("Authorization", fmt.Sprintf("key=%s", s.ApiKey))
req.Header.Add("Content-Type", "application/json")
resp, err := s.Http.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
switch resp.StatusCode {
case http.StatusBadRequest:
return nil, JSONParseError{fmt.Errorf("%d error: %s", resp.StatusCode, resp.Status)}
case http.StatusUnauthorized:
return nil, UnauthorizedError{fmt.Errorf("%d error: %s", resp.StatusCode, resp.Status)}
case http.StatusOK:
default:
return nil, UnknownError{fmt.Errorf("%d error: %s", resp.StatusCode, resp.Status)}
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
response := new(Response)
err = json.Unmarshal(body, response)
return response, err
}
// Send sends a message to the GCM server, retrying in case of service
// unavailability. A non-nil error is returned if a non-recoverable
// error occurs (i.e. if the response status is not "200 OK").
//
// Note that messages are retried using exponential backoff, and as a
// result, this method may block for several seconds.
func (s *Sender) Send(msg *Message, retries int) (*Response, error) {
if err := checkSender(s); err != nil {
return nil, err
} else if err := checkMessage(msg); err != nil {
return nil, err
} else if retries < 0 {
return nil, errors.New("'retries' must not be negative.")
}
// Send the message for the first time.
resp, err := s.SendNoRetry(msg)
if err != nil {
return nil, err
} else if resp.Failure == 0 || retries == 0 {
return resp, nil
}
// One or more messages failed to send.
regIDs := msg.RegistrationIDs
allResults := make(map[string]Result, len(regIDs))
backoff := backoffInitialDelay
for i := 0; updateStatus(msg, resp, allResults) > 0 && i < retries; i++ {
sleepTime := backoff/2 + rand.Intn(backoff)
time.Sleep(time.Duration(sleepTime) * time.Millisecond)
backoff = min(2*backoff, maxBackoffDelay)
if resp, err = s.SendNoRetry(msg); err != nil {
msg.RegistrationIDs = regIDs
return nil, err
}
}
// Bring the message back to its original state.
msg.RegistrationIDs = regIDs
// Create a Response containing the overall results.
finalResults := make([]Result, len(regIDs))
var success, failure, canonicalIDs int
for i := 0; i < len(regIDs); i++ {
result, _ := allResults[regIDs[i]]
finalResults[i] = result
if result.MessageID != "" {
if result.RegistrationID != "" {
canonicalIDs++
}
success++
} else {
failure++
}
}
return &Response{
// Return the most recent multicast id.
MulticastID: resp.MulticastID,
Success: success,
Failure: failure,
CanonicalIDs: canonicalIDs,
Results: finalResults,
}, nil
}
// updateStatus updates the status of the messages sent to devices and
// returns the number of recoverable errors that could be retried.
func updateStatus(msg *Message, resp *Response, allResults map[string]Result) int {
unsentRegIDs := make([]string, 0, resp.Failure)
for i := 0; i < len(resp.Results); i++ {
regID := msg.RegistrationIDs[i]
allResults[regID] = resp.Results[i]
if resp.Results[i].Error == "Unavailable" {
unsentRegIDs = append(unsentRegIDs, regID)
}
}
msg.RegistrationIDs = unsentRegIDs
return len(unsentRegIDs)
}
// min returns the smaller of two integers. For exciting religious wars
// about why this wasn't included in the "math" package, see this thread:
// https://groups.google.com/d/topic/golang-nuts/dbyqx_LGUxM/discussion
func min(a, b int) int {
if a < b {
return a
}
return b
}
// checkSender returns an error if the sender is not well-formed and
// initializes a zeroed http.Client if one has not been provided.
func checkSender(sender *Sender) error {
if sender.ApiKey == "" {
return errors.New("the sender's API key must not be empty")
}
if sender.Http == nil {
sender.Http = new(http.Client)
}
return nil
}
// checkMessage returns an error if the message is not well-formed.
func checkMessage(msg *Message) error {
if msg == nil {
return errors.New("the message must not be nil")
} else if msg.RegistrationIDs == nil {
return errors.New("the message's RegistrationIDs field must not be nil")
} else if len(msg.RegistrationIDs) == 0 {
return errors.New("the message must specify at least one registration ID")
} else if len(msg.RegistrationIDs) > 1000 {
return errors.New("the message may specify at most 1000 registration IDs")
} else if msg.TimeToLive < 0 || 2419200 < msg.TimeToLive {
return errors.New("the message's TimeToLive field must be an integer " +
"between 0 and 2419200 (4 weeks)")
}
return nil
}