forked from alexjlockwood/gcm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sender.go
211 lines (190 loc) · 6.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
// Copyright 2013 Alex Lockwood. All rights reserved.
// 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.
GcmSendEndpoint = "https://android.googleapis.com/gcm/send"
// Initial delay before first retry, without jitter.
backoffInitialDelay = 1000
// Maximum delay before a retry.
maxBackoffDelay = 1024000
)
// 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 (i.e. new(http.Client)). 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: apiKey, 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()
if resp.StatusCode != http.StatusOK {
return nil, 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
var regIds = msg.RegistrationIDs
var allResults = make(map[string]Result, len(regIds))
var 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 {
return nil, err
}
}
// Bring the message back to its original state
msg.RegistrationIDs = regIds
// Create a Response containing the overall results
var success, failure, canonicalIds int
var finalResults = make([]Result, len(regIds))
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 {
var 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
}