summaryrefslogtreecommitdiff
path: root/internal
diff options
context:
space:
mode:
authorValery Piashchynski <[email protected]>2021-06-13 17:03:49 +0300
committerValery Piashchynski <[email protected]>2021-06-13 17:03:49 +0300
commit128d71cad43ffcaab60cb60939584df0941f37be (patch)
treea18f26274ffce32a0b29c479a3692dc1fe822415 /internal
parent9fd01b07ecae6e68dd0ed8be6be5f8c0228771cb (diff)
- Use pool for the FetchPID operation
Signed-off-by: Valery Piashchynski <[email protected]>
Diffstat (limited to 'internal')
-rwxr-xr-xinternal/protocol.go34
1 files changed, 27 insertions, 7 deletions
diff --git a/internal/protocol.go b/internal/protocol.go
index a344a3c4..7487b4f3 100755
--- a/internal/protocol.go
+++ b/internal/protocol.go
@@ -2,6 +2,7 @@ package internal
import (
"os"
+ "sync"
j "github.com/json-iterator/go"
"github.com/spiral/errors"
@@ -19,9 +20,25 @@ type pidCommand struct {
Pid int `json:"pid"`
}
+var fPool = sync.Pool{New: func() interface{} {
+ return frame.NewFrame()
+}}
+
+func getFrame() *frame.Frame {
+ return fPool.Get().(*frame.Frame)
+}
+
+func putFrame(f *frame.Frame) {
+ f.Reset()
+ fPool.Put(f)
+}
+
func SendControl(rl relay.Relay, payload interface{}) error {
const op = errors.Op("send_control")
- fr := frame.NewFrame()
+
+ fr := getFrame()
+ defer putFrame(fr)
+
fr.WriteVersion(frame.VERSION_1)
fr.WriteFlags(frame.CONTROL)
@@ -51,6 +68,8 @@ func SendControl(rl relay.Relay, payload interface{}) error {
fr.WritePayload(data)
fr.WriteCRC()
+ // hold a pointer to a frame
+ // Do we need a copy here????
err = rl.Send(fr)
if err != nil {
return errors.E(op, err)
@@ -66,27 +85,28 @@ func FetchPID(rl relay.Relay) (int64, error) {
return 0, errors.E(op, err)
}
- frameR := frame.NewFrame()
+ fr := getFrame()
+ defer putFrame(fr)
- err = rl.Receive(frameR)
- if !frameR.VerifyCRC() {
+ err = rl.Receive(fr)
+ if !fr.VerifyCRC() {
return 0, errors.E(op, errors.Str("CRC mismatch"))
}
if err != nil {
return 0, errors.E(op, err)
}
- if frameR == nil {
+ if fr == nil {
return 0, errors.E(op, errors.Str("nil frame received"))
}
- flags := frameR.ReadFlags()
+ flags := fr.ReadFlags()
if flags&frame.CONTROL == 0 {
return 0, errors.E(op, errors.Str("unexpected response, header is missing, no CONTROL flag"))
}
link := &pidCommand{}
- err = json.Unmarshal(frameR.Payload(), link)
+ err = json.Unmarshal(fr.Payload(), link)
if err != nil {
return 0, errors.E(op, err)
}