forked from gree/futurama
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.go
70 lines (55 loc) · 1.36 KB
/
helper.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
package futurama
import (
"bytes"
"encoding/json"
"math"
"math/rand"
"sync"
"time"
)
func backoff(attempt int) time.Time {
const RETRY_BACKOFF_CAP = 10 * time.Minute
basePart := int(math.Pow(2, float64(attempt-2)) * 1000)
randPart := rand.Intn(basePart)
du := time.Duration(basePart+randPart) * time.Millisecond
if du > RETRY_BACKOFF_CAP {
du = RETRY_BACKOFF_CAP
}
return time.Now().Add(du)
}
type reusableEncoder struct {
buf *bytes.Buffer
encoder *json.Encoder
}
func newReusableEncoder() (encoder *reusableEncoder) {
encoder = new(reusableEncoder)
encoder.buf = bytes.NewBuffer(nil)
encoder.encoder = json.NewEncoder(encoder.buf)
return
}
func (self *reusableEncoder) Marshal(v interface{}) ([]byte, error) {
defer self.buf.Reset()
if err := self.encoder.Encode(v); err != nil {
return nil, err
}
marshalled := self.buf.Bytes()
buf := make([]byte, len(marshalled))
copy(buf, marshalled)
return buf, nil
}
type EncoderPool struct {
pool sync.Pool
}
func newEncoderPool() *EncoderPool {
return_value := EncoderPool{}
return_value.pool = sync.Pool{
New: func() interface{} { return newReusableEncoder() },
}
return &return_value
}
func (self *EncoderPool) Marshal(v interface{}) ([]byte, error) {
encoder := self.pool.Get().(*reusableEncoder)
defer self.pool.Put(encoder)
return encoder.Marshal(v)
}
var Encoder = newEncoderPool()