forked from centrifugal/centrifugo
/
encoding.go
85 lines (73 loc) · 1.63 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
package api
import "sync"
// Encoding determines connection protocol encoding in use.
type Encoding string
const (
// EncodingJSON means JSON protocol.
EncodingJSON Encoding = "json"
// EncodingProtobuf means protobuf protocol.
EncodingProtobuf Encoding = "protobuf"
)
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)
}
protobufReplyEncoderPool.Put(e)
}
// GetCommandDecoder ...
func GetCommandDecoder(enc Encoding, data []byte) CommandDecoder {
if enc == EncodingJSON {
return NewJSONCommandDecoder(data)
}
return NewProtobufCommandDecoder(data)
}
// PutCommandDecoder ...
func PutCommandDecoder(enc Encoding, e CommandDecoder) {
return
}
// GetDecoder ...
func GetDecoder(enc Encoding) Decoder {
if enc == EncodingJSON {
return NewJSONDecoder()
}
return NewProtobufDecoder()
}
// PutDecoder ...
func PutDecoder(enc Encoding, e Decoder) {
return
}
// GetEncoder ...
func GetEncoder(enc Encoding) Encoder {
if enc == EncodingJSON {
return NewJSONEncoder()
}
return NewProtobufEncoder()
}
// PutEncoder ...
func PutEncoder(enc Encoding, e Encoder) {
return
}