-
Notifications
You must be signed in to change notification settings - Fork 0
/
tools.go
54 lines (43 loc) · 912 Bytes
/
tools.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
package castcenter
import (
"fmt"
"sync"
"time"
"github.com/sirupsen/logrus"
)
var (
pool = &sync.Pool{New: func() interface{} {
return make([]byte, datagramReadBufferSize)
}}
)
// Redis .
type Redis interface {
Get(key string) (string, error)
SetNX(key string, value interface{}, expiration time.Duration) (bool, error)
Expire(key string, expiration time.Duration) (bool, error)
ZAdd(key, member string, score float64) (int64, error)
ZRangeByScore(key string, min, max float64) ([]string, error)
}
// GetBytes .
func GetBytes() []byte {
return pool.Get().([]byte)
}
// PutBytes .
func PutBytes(buf []byte) {
pool.Put(buf[:cap(buf)])
}
func recoverLoop(f func()) {
defer func() {
go recoverLoop(f)
e := recover()
if e == nil {
return
}
err, ok := e.(error)
if !ok {
err = fmt.Errorf("%v", e)
}
logrus.WithError(err).Errorf("udp loop fail, recovered")
}()
f()
}