-
Notifications
You must be signed in to change notification settings - Fork 23
/
sync_transport.go
66 lines (59 loc) · 1.69 KB
/
sync_transport.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
package rollbar
import (
"time"
)
// SyncTransport is a concrete implementation of the Transport type which communicates with the
// Rollbar API synchronously.
type SyncTransport struct {
baseTransport
}
// NewSyncTransport builds a synchronous transport which sends data to the Rollbar API at the
// specified endpoint using the given access token.
func NewSyncTransport(token, endpoint string) *SyncTransport {
return &SyncTransport{
baseTransport{
Token: token,
Endpoint: endpoint,
RetryAttempts: DefaultRetryAttempts,
PrintPayloadOnError: true,
ItemsPerMinute: 0,
perMinCounter: 0,
startTime: time.Now(),
},
}
}
// Send the body to Rollbar.
// Returns errors associated with the http request if any.
// If the access token has not been set or is empty then this will
// not send anything and will return nil.
func (t *SyncTransport) Send(body map[string]interface{}) error {
return t.doSend(body, t.RetryAttempts)
}
func (t *SyncTransport) doSend(body map[string]interface{}, retriesLeft int) error {
elapsedTime := time.Now().Sub(t.startTime).Seconds()
if elapsedTime < 0 || elapsedTime >= 60 {
t.startTime = time.Now()
t.perMinCounter = 0
}
if t.shouldSend() {
canRetry, err := t.post(body)
if err != nil {
if !canRetry || retriesLeft <= 0 {
if t.PrintPayloadOnError {
writePayloadToStderr(t.Logger, body)
}
return err
}
return t.doSend(body, retriesLeft-1)
} else {
t.perMinCounter++
}
}
return nil
}
// Wait is a no-op for the synchronous transport.
func (t *SyncTransport) Wait() {}
// Close is a no-op for the synchronous transport.
func (t *SyncTransport) Close() error {
return nil
}