X-Git-Url: https://gerrit.fd.io/r/gitweb?p=govpp.git;a=blobdiff_plain;f=core%2Fchannel.go;fp=core%2Fchannel.go;h=112c14e28baf6dbba38f9c76af2ea77c9bbd2bbe;hp=1086c3639dbaf887f2a49bfdf8abc6cda671846d;hb=b9aa34d5f77ac5969ad273bfd187ce5f9594b25e;hpb=000215c229d6df2c1a68b50847d8c7abf3842ce5 diff --git a/core/channel.go b/core/channel.go index 1086c36..112c14e 100644 --- a/core/channel.go +++ b/core/channel.go @@ -19,7 +19,6 @@ import ( "fmt" "reflect" "strings" - "sync/atomic" "time" "github.com/sirupsen/logrus" @@ -110,11 +109,9 @@ type Channel struct { receiveReplyTimeout time.Duration // maximum time that we wait for receiver to consume reply } -func (c *Connection) newChannel(reqChanBufSize, replyChanBufSize int) *Channel { +func (c *Connection) newChannel(reqChanBufSize, replyChanBufSize int) (*Channel, error) { // create new channel - chID := uint16(atomic.AddUint32(&c.maxChannelID, 1) & 0x7fff) channel := &Channel{ - id: chID, conn: c, msgCodec: c.codec, msgIdentifier: c, @@ -126,10 +123,22 @@ func (c *Connection) newChannel(reqChanBufSize, replyChanBufSize int) *Channel { // store API channel within the client c.channelsLock.Lock() - c.channels[chID] = channel + if len(c.channels) >= 0x7fff { + return nil, errors.New("all channel IDs are used") + } + for { + c.nextChannelID++ + chID := c.nextChannelID & 0x7fff + _, ok := c.channels[chID] + if !ok { + channel.id = chID + c.channels[chID] = channel + break + } + } c.channelsLock.Unlock() - return channel + return channel, nil } func (ch *Channel) GetID() uint16 {