-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrequest.go
107 lines (92 loc) · 2.42 KB
/
request.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
96
97
98
99
100
101
102
103
104
105
106
107
package exposed
import (
"bufio"
"fmt"
"sync"
)
// request is a TLV request.
type request struct {
payload []byte
operation uint64
opBuf [8]byte
sizeBuf [4]byte
}
// Reset resets the given request.
func (req *request) Reset() {
req.operation = 0
req.payload = req.payload[:0]
}
//SetOperation sets request operation.
func (req *request) SetOperation(op uint64) {
req.operation = op
}
// Operation returns request operation.
//
// The returned payload is valid until the next request method call
// or until releaseRequest is called.
func (req *request) Operation() uint64 {
return req.operation
}
// Write appends p to the request payload.
//
// It implements io.Writer.
func (req *request) Write(p []byte) (int, error) {
req.Append(p)
return len(p), nil
}
// AppendPayload appends p to the request payload.
func (req *request) Append(p []byte) {
req.payload = append(req.payload, p...)
}
// SwapPayload swaps the given payload with the request's payload.
//
// It is forbidden accessing the swapped payload after the call.
func (req *request) SwapPayload(value []byte) {
req.payload = value
return
}
// Payload returns request payload.
//
// The returned payload is valid until the next request method call.
// or until releaseRequest is called.
func (req *request) Payload() []byte {
return req.payload
}
// WriteRequest writes the request to bw.
//
// It implements exposed.RequestWriter
func (req *request) WriteRequest(bw *bufio.Writer) error {
if err := writeOperation(bw, req.operation, req.opBuf[:]); err != nil {
return fmt.Errorf("cannot write request operation: %s", err)
}
if err := writeBytes(bw, req.payload, req.sizeBuf[:]); err != nil {
return fmt.Errorf("cannot write request payload: %s", err)
}
return nil
}
// readRequest reads the request from br.
func (req *request) ReadRequest(br *bufio.Reader) (err error) {
req.operation, err = readOperation(br, req.opBuf[:])
if err != nil {
return fmt.Errorf("cannot read request operation: %s", err)
}
req.payload, err = readBytes(br, req.payload[:0], req.sizeBuf[:])
if err != nil {
return fmt.Errorf("cannot read request payload: %s", err)
}
return nil
}
// acquireRequest acquires new request.
func acquireRequest() *request {
v := requestPool.Get()
if v == nil {
v = &request{}
}
return v.(*request)
}
// releaseRequest releases the given request.
func releaseRequest(req *request) {
req.Reset()
requestPool.Put(req)
}
var requestPool sync.Pool