-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
295 lines (243 loc) · 6.76 KB
/
client.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package ga
import (
"bytes"
"context"
"fmt"
"io/ioutil"
"net/http"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/pkg/errors"
)
// Client reports Events to GA.
type Client struct {
// How long the client waits before reporting an Event to GA.
// The default is 15 seconds.
BatchWait time.Duration
// HTTP is the http.Client used to make POST calls to GA.
HTTP *http.Client
// The time to wait for batch sends to complete.
// If the timeout is exceeded the remaining items will be reported later.
// Client.Shutdown will wait in steps of SendTimeout until all Events have been submitted.
// The defaults to http.Client.Timeout if this is zero it will default to 5 seconds.
SendTimeout time.Duration
// The GA ID for Events.
// This is only used by Client.DefaultHTTPHandler.
TID string
doneChan chan struct{}
errHandler ErrHandler // useful for logging errors occurring on ga go routines
eventChan chan event
eventCounter int32 // accessed atomically
events events
inShutdown int32 // accessed atomically (non-zero means we're in Shutdown).
mu sync.Mutex
started int32 // accessed atomically (non-zero means we've Started).
urlStr string // set to httptest.NewServer().URL during tests.
}
func (c *Client) getDoneChan() <-chan struct{} {
c.mu.Lock()
defer c.mu.Unlock()
return c.getDoneChanLocked()
}
func (c *Client) getDoneChanLocked() chan struct{} {
if c.doneChan == nil {
c.doneChan = make(chan struct{})
}
return c.doneChan
}
func (c *Client) closeDoneChanLocked() {
ch := c.getDoneChanLocked()
select {
case <-ch:
// Already closed. Don't close again.
default:
// Safe to close here. We're the only closer, guarded
// by c.mu.
close(ch)
}
}
func (c *Client) closeEventChanLocked() {
close(c.eventChan)
}
// Shutdown the Client.
// This will block until all Events reported before calling Shutdown have been submitted.
func (c *Client) Shutdown(ctx context.Context) error {
atomic.AddInt32(&c.inShutdown, 1)
defer atomic.AddInt32(&c.inShutdown, -1)
c.mu.Lock()
c.closeDoneChanLocked()
c.mu.Unlock()
for c.sending() {
time.Sleep(c.HTTP.Timeout)
}
c.closeEventChanLocked()
return nil
}
func (c *Client) sending() bool {
if atomic.LoadInt32(&c.eventCounter) > 0 {
return true
}
return false
}
// Start makes the Client receive Events and submit them to GA.
// This call will block until Client.Shutdown is called.
func (c *Client) Start() error {
if atomic.LoadInt32(&c.started) > 0 {
return ErrAlreadyStarted
}
atomic.AddInt32(&c.started, 1)
defer atomic.AddInt32(&c.started, -1)
c.events = make([]event, 0, 256)
c.eventChan = make(chan event)
if c.HTTP == nil {
c.HTTP = http.DefaultClient
}
if c.BatchWait == 0 {
c.BatchWait = time.Second * 15
}
if c.SendTimeout == 0 {
c.SendTimeout = c.HTTP.Timeout
}
if c.SendTimeout == 0 {
c.SendTimeout = time.Second * 5
}
if c.urlStr == "" {
c.urlStr = "https://www.google-analytics.com/batch"
}
if c.errHandler == nil {
c.HandleErr(ErrHandlerFunc(func(e []Event, err error) {}))
}
ticker := time.NewTicker(c.BatchWait)
defer ticker.Stop()
for {
select {
case e := <-c.eventChan:
atomic.AddInt32(&c.eventCounter, 1)
c.events = append(c.events, e)
if len(c.events) >= 20 {
n, _ := c.send(c.events)
c.events = c.events[:len(c.events)-int(n)]
atomic.AddInt32(&c.eventCounter, -n)
}
case <-ticker.C:
if len(c.events) > 0 {
n, _ := c.send(c.events)
c.events = c.events[:len(c.events)-int(n)]
atomic.AddInt32(&c.eventCounter, -n)
}
case <-c.getDoneChan():
if len(c.events) > 0 {
n, _ := c.send(c.events)
c.events = c.events[:len(c.events)-int(n)]
atomic.AddInt32(&c.eventCounter, -n)
}
return ErrClientClosed
}
}
}
// Report is used to submit an Event to GA.
// This can be safely called by multiple go routines.
func (c *Client) Report(e Event) error {
select {
case <-c.getDoneChan():
return ErrClientClosed
default:
}
x := event{
reportedAt: time.Now(),
e: e,
}
c.eventChan <- x
return nil
}
// ErrHandler is used to handle errors that occur while submitting to GA.
type ErrHandler interface {
// Err receives the Events that erred and the corresponding error.
// This is useful for logging and retry submitting Events.
Err([]Event, error)
}
// The ErrHandlerFunc type is an adapter to allow the use of ordinary functions as ErrHandlers.
// If f is a function with the appropriate signature, ErrHandlerFunc(f) is a ErrHandler that calls f.
type ErrHandlerFunc func(e []Event, err error)
// Err receives the Events that erred and the corresponding error.
// This is useful for logging and retry submitting Events.
func (f ErrHandlerFunc) Err(e []Event, err error) {
f(e, err)
}
// HandleErr sets the ErrHandler to be used by the Client.
// Multiple calls will override.
func (c *Client) HandleErr(h ErrHandler) {
c.mu.Lock()
defer c.mu.Unlock()
if c.errHandler != nil {
return
}
c.errHandler = h
}
func (c *Client) send(events events) (int32, error) {
ctx, cancel := context.WithTimeout(context.Background(), c.SendTimeout)
defer cancel()
var sumN int32
for len(events) >= 20 {
n, err := c.sendBatch(ctx, events[:20])
sumN += n
if err != nil {
return sumN, err
}
events = events[20:]
}
if len(events) > 0 {
n, err := c.sendBatch(ctx, events)
sumN += n
if err != nil {
return sumN, err
}
}
return sumN, nil
}
func (c *Client) sendBatch(ctx context.Context, events events) (int32, error) {
select {
case <-ctx.Done():
return 0, ctx.Err()
default:
// execute
}
for _, e := range events {
if e.reportedAt.IsZero() {
continue
}
qt := fmt.Sprint(time.Since(e.reportedAt).Nanoseconds() / 1e6)
if qt != "0" {
e.e.Set("qt", qt)
}
}
buf := bytes.NewBuffer(nil)
_, err := events.WriteTo(buf)
if err != nil {
c.errHandler.Err(events.cleanEvents(), err)
return int32(len(events)), nil // can't recover this
}
resp, err := c.HTTP.Post(c.urlStr, "application/x-www-form-urlencoded", buf)
if err != nil && strings.Contains(err.Error(), "net/http: request canceled") || err != nil && strings.Contains(err.Error(), "net/http: timeout") {
return 0, err
}
if err != nil {
c.errHandler.Err(events.cleanEvents(), err)
return int32(len(events)), nil // can't recover this
}
if resp.StatusCode != 200 {
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
err = errors.Wrap(err, ErrGoogleAnalytics.Error())
c.errHandler.Err(events.cleanEvents(), err)
return int32(len(events)), nil // can't recover this
}
err = errors.Wrap(fmt.Errorf("code: %d message: %s", resp.StatusCode, strings.TrimSuffix(string(b), "\n")), ErrGoogleAnalytics.Error())
c.errHandler.Err(events.cleanEvents(), err)
return int32(len(events)), nil // can't recover this
}
return int32(len(events)), nil
}