Skip to content

Commit

Permalink
fix: remove streamPool to avoid memory leak
Browse files Browse the repository at this point in the history
  • Loading branch information
ethan256 committed May 20, 2024
1 parent fd36b8c commit 08a5726
Show file tree
Hide file tree
Showing 6 changed files with 7 additions and 29 deletions.
17 changes: 0 additions & 17 deletions ua/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,12 @@ import (
"fmt"
"math"
"reflect"
"sync"
"time"

"github.com/gopcua/opcua/debug"
"github.com/gopcua/opcua/errors"
)

var streamPool sync.Pool = sync.Pool{
New: func() interface{} {
return NewStream(DefaultBufSize)
},
}

func BorrowStream() *Stream {
v := streamPool.Get().(*Stream)
v.Reset()
return v
}

func ReturnStream(s *Stream) {
streamPool.Put(s)
}

// debugCodec enables printing of debug messages in the opcua codec.
var debugCodec = debug.FlagSet("codec")

Expand Down
3 changes: 1 addition & 2 deletions ua/extension_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,7 @@ func (e *ExtensionObject) Encode(s *Stream) {
return
}

body := BorrowStream()
defer ReturnStream(body)
body := NewStream(DefaultBufSize)
body.WriteAny(e.Value)
if body.Error() != nil {
s.WrapError(body.Error())
Expand Down
1 change: 1 addition & 0 deletions ua/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func (s *Stream) Len() int {
func (s *Stream) Reset() {
s.buf = s.buf[:0]
s.pos = 0
s.err = nil
}

func (s *Stream) Bytes() []byte {
Expand Down
6 changes: 2 additions & 4 deletions uacp/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,8 +396,7 @@ func (c *Conn) Send(typ string, msg interface{}) error {
return errors.Errorf("invalid msg type: %s", typ)
}

bodyStream := ua.BorrowStream()
defer ua.ReturnStream(bodyStream)
bodyStream := ua.NewStream(ua.DefaultBufSize)
bodyStream.WriteAny(msg)
if bodyStream.Error() != nil {
return errors.Errorf("encode msg failed: %s", bodyStream.Error())
Expand All @@ -413,8 +412,7 @@ func (c *Conn) Send(typ string, msg interface{}) error {
return errors.Errorf("send packet too large: %d > %d bytes", h.MessageSize, c.ack.SendBufSize)
}

headerStream := ua.BorrowStream()
defer ua.ReturnStream(headerStream)
headerStream := ua.NewStream(ua.DefaultBufSize)
h.Encode(headerStream)
if headerStream.Error() != nil {
return errors.Errorf("encode hdr failed: %s", headerStream.Error())
Expand Down
3 changes: 1 addition & 2 deletions uasc/codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ func RunCodecTest(t *testing.T, cases []CodecTestCase) {
})

t.Run("encode", func(t *testing.T) {
s := ua.BorrowStream()
defer ua.ReturnStream(s)
s := ua.NewStream(ua.DefaultBufSize)
s.WriteAny(c.Struct)
if s.Error() != nil {
t.Fatal(s.Error())
Expand Down
6 changes: 2 additions & 4 deletions uasc/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,7 @@ func (m *Message) Encode(s *ua.Stream) {
}

func (m *Message) EncodeChunks(maxBodySize uint32) ([][]byte, error) {
dataBody := ua.BorrowStream()
defer ua.ReturnStream(dataBody)
dataBody := ua.NewStream(ua.DefaultBufSize)
dataBody.WriteAny(m.TypeID)
dataBody.WriteAny(m.Service)
if dataBody.Error() != nil {
Expand All @@ -132,8 +131,7 @@ func (m *Message) EncodeChunks(maxBodySize uint32) ([][]byte, error) {

switch m.Header.MessageType {
case "OPN":
partialHeader := ua.BorrowStream()
defer ua.ReturnStream(dataBody)
partialHeader := ua.NewStream(ua.DefaultBufSize)
partialHeader.WriteAny(m.AsymmetricSecurityHeader)
partialHeader.WriteAny(m.SequenceHeader)
if partialHeader.Error() != nil {
Expand Down

0 comments on commit 08a5726

Please sign in to comment.