Skip to content

Commit

Permalink
fix(codec): full codec fix race condition
Browse files Browse the repository at this point in the history
  • Loading branch information
shadowspore committed May 7, 2021
1 parent 49234a0 commit 3bf28ef
Showing 1 changed file with 2 additions and 6 deletions.
8 changes: 2 additions & 6 deletions internal/proto/codec/full.go
Expand Up @@ -36,23 +36,19 @@ func (i *Full) Write(w io.Writer, b *bin.Buffer) error {
return err
}

if err := writeFull(w, int(atomic.LoadInt64(&i.wSeqNo)), b); err != nil {
if err := writeFull(w, int(atomic.AddInt64(&i.wSeqNo, 1)-1), b); err != nil {
return xerrors.Errorf("write full: %w", err)
}

atomic.AddInt64(&i.wSeqNo, 1)

return nil
}

// Read fills buffer with received message.
func (i *Full) Read(r io.Reader, b *bin.Buffer) error {
if err := readFull(r, int(atomic.LoadInt64(&i.rSeqNo)), b); err != nil {
if err := readFull(r, int(atomic.AddInt64(&i.rSeqNo, 1)-1), b); err != nil {
return xerrors.Errorf("read full: %w", err)
}

atomic.AddInt64(&i.rSeqNo, 1)

if err := checkProtocolError(b); err != nil {
return err
}
Expand Down

0 comments on commit 3bf28ef

Please sign in to comment.