File: reply.go

package info (click to toggle)
golang-github-centrifugal-centrifuge 0.15.0%2Bgit20210306.f435ba2-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,612 kB
  • sloc: javascript: 102; makefile: 2
file content (35 lines) | stat: -rw-r--r-- 728 bytes parent folder | download
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
package prepared

import (
	"sync"

	"github.com/centrifugal/protocol"
)

// Reply is structure for encoding reply only once.
type Reply struct {
	ProtoType protocol.Type
	Reply     *protocol.Reply
	data      []byte
	once      sync.Once
}

// NewReply initializes Reply.
func NewReply(reply *protocol.Reply, protoType protocol.Type) *Reply {
	return &Reply{
		Reply:     reply,
		ProtoType: protoType,
	}
}

// Data returns data associated with reply which is only calculated once.
func (r *Reply) Data() []byte {
	r.once.Do(func() {
		encoder := protocol.GetReplyEncoder(r.ProtoType)
		_ = encoder.Encode(r.Reply)
		data := encoder.Finish()
		protocol.PutReplyEncoder(r.ProtoType, encoder)
		r.data = data
	})
	return r.data
}