Debian Codegraph @

internal/prepared/reply.go

raw
1 package prepared
2
3 import (
4 "sync"
5
6 "github.com/centrifugal/protocol"
7 )
8
9 // Reply is structure for encoding reply only once.
10 type Reply struct {
11 ProtoType protocol.Type
12 Reply *protocol.Reply
13 data []byte
14 once sync.Once
15 }
16
17 // NewReply initializes Reply.
18 func NewReply(reply *protocol.Reply, protoType protocol.Type) *Reply {
19 return &Reply{
20 Reply: reply,
21 ProtoType: protoType,
22 }
23 }
24
25 // Data returns data associated with reply which is only calculated once.
26 func (r *Reply) Data() []byte {
27 r.once.Do(func() {
28 encoder := protocol.GetReplyEncoder(r.ProtoType)
29 _ = encoder.Encode(r.Reply)
30 data := encoder.Finish()
31 protocol.PutReplyEncoder(r.ProtoType, encoder)
32 r.data = data
33 })
34 return r.data
35 }