forked from centrifugal/centrifuge-go
/
encoding.go
95 lines (83 loc) · 1.93 KB
/
encoding.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
package proto
import "sync"
// Encoding determines connection protocol encoding in use.
type Encoding int
const (
// EncodingJSON means JSON protocol.
EncodingJSON Encoding = 0
// EncodingProtobuf means protobuf protocol.
EncodingProtobuf Encoding = 1
)
// NewPushEncoder ...
func NewPushEncoder(enc Encoding) PushEncoder {
if enc == EncodingJSON {
return NewJSONPushEncoder()
}
return NewProtobufPushEncoder()
}
// NewPushDecoder ...
func NewPushDecoder(enc Encoding) PushDecoder {
if enc == EncodingJSON {
return NewJSONPushDecoder()
}
return NewProtobufPushDecoder()
}
var (
jsonReplyEncoderPool sync.Pool
protobufReplyEncoderPool sync.Pool
)
// GetReplyEncoder ...
func GetReplyEncoder(enc Encoding) ReplyEncoder {
if enc == EncodingJSON {
e := jsonReplyEncoderPool.Get()
if e == nil {
return NewJSONReplyEncoder()
}
encoder := e.(ReplyEncoder)
encoder.Reset()
return encoder
}
e := protobufReplyEncoderPool.Get()
if e == nil {
return NewProtobufReplyEncoder()
}
encoder := e.(ReplyEncoder)
encoder.Reset()
return encoder
}
// PutReplyEncoder ...
func PutReplyEncoder(enc Encoding, e ReplyEncoder) {
if enc == EncodingJSON {
jsonReplyEncoderPool.Put(e)
return
}
protobufReplyEncoderPool.Put(e)
}
// NewReplyDecoder ...
func NewReplyDecoder(enc Encoding, data []byte) ReplyDecoder {
if enc == EncodingJSON {
return NewJSONReplyDecoder(data)
}
return NewProtobufReplyDecoder(data)
}
// NewResultDecoder ...
func NewResultDecoder(enc Encoding) ResultDecoder {
if enc == EncodingJSON {
return NewJSONResultDecoder()
}
return NewProtobufResultDecoder()
}
// NewParamsEncoder ...
func NewParamsEncoder(enc Encoding) ParamsEncoder {
if enc == EncodingJSON {
return NewJSONParamsEncoder()
}
return NewProtobufParamsEncoder()
}
// NewCommandEncoder ...
func NewCommandEncoder(enc Encoding) CommandEncoder {
if enc == EncodingJSON {
return NewJSONCommandEncoder()
}
return NewProtobufCommandEncoder()
}