diff options
author | mhalaj1 <matus.halaj@pantheon.tech> | 2021-08-26 20:15:08 +0200 |
---|---|---|
committer | mhalaj1 <matus.halaj@pantheon.tech> | 2021-09-07 15:24:53 +0200 |
commit | c09ee3241377aae2530a73d48c4e20641d76d0ee (patch) | |
tree | 219fd1f7d9ba595f0f4c8dac9796bc76ce3556fc /core/channel.go | |
parent | debc52dea8a81417bb08ca5bb934c7876b6d65e0 (diff) |
Refactoring and fixes
* refactor creation of new channel
* add missing closing of created streams
* correct documentation regarding thread safety of stream
Signed-off-by: mhalaj1 <matus.halaj@pantheon.tech>
Change-Id: Ic601efff298fcbdecaafab83fa236253af69de21
Diffstat (limited to 'core/channel.go')
-rw-r--r-- | core/channel.go | 26 |
1 files changed, 18 insertions, 8 deletions
diff --git a/core/channel.go b/core/channel.go index 4cb5761..1086c36 100644 --- a/core/channel.go +++ b/core/channel.go @@ -19,6 +19,7 @@ import ( "fmt" "reflect" "strings" + "sync/atomic" "time" "github.com/sirupsen/logrus" @@ -109,17 +110,26 @@ type Channel struct { receiveReplyTimeout time.Duration // maximum time that we wait for receiver to consume reply } -func newChannel(id uint16, conn *Connection, codec MessageCodec, identifier MessageIdentifier, reqSize, replySize int) *Channel { - return &Channel{ - id: id, - conn: conn, - msgCodec: codec, - msgIdentifier: identifier, - reqChan: make(chan *vppRequest, reqSize), - replyChan: make(chan *vppReply, replySize), +func (c *Connection) newChannel(reqChanBufSize, replyChanBufSize int) *Channel { + // create new channel + chID := uint16(atomic.AddUint32(&c.maxChannelID, 1) & 0x7fff) + channel := &Channel{ + id: chID, + conn: c, + msgCodec: c.codec, + msgIdentifier: c, + reqChan: make(chan *vppRequest, reqChanBufSize), + replyChan: make(chan *vppReply, replyChanBufSize), replyTimeout: DefaultReplyTimeout, receiveReplyTimeout: ReplyChannelTimeout, } + + // store API channel within the client + c.channelsLock.Lock() + c.channels[chID] = channel + c.channelsLock.Unlock() + + return channel } func (ch *Channel) GetID() uint16 { |