Skip to content

Commit

Permalink
rhp: fix data race
Browse files Browse the repository at this point in the history
  • Loading branch information
n8maninger committed Aug 10, 2023
1 parent a762e78 commit ec3718e
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions rhp/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package rhp
import (
"context"
"net"
"sync/atomic"

"golang.org/x/time/rate"
)
Expand All @@ -26,13 +27,15 @@ type (

// Usage returns the amount of data read and written by the connection.
func (c *Conn) Usage() (read, written uint64) {
return c.r, c.w
read = atomic.LoadUint64(&c.r)
written = atomic.LoadUint64(&c.w)
return
}

// Read implements io.Reader
func (c *Conn) Read(b []byte) (int, error) {
n, err := c.Conn.Read(b)
c.r += uint64(n)
atomic.AddUint64(&c.r, uint64(n))
c.monitor.ReadBytes(n)
if err := c.rl.WaitN(context.Background(), n); err != nil {
return n, err
Expand All @@ -43,7 +46,7 @@ func (c *Conn) Read(b []byte) (int, error) {
// Write implements io.Writer
func (c *Conn) Write(b []byte) (int, error) {
n, err := c.Conn.Write(b)
c.w += uint64(n)
atomic.AddUint64(&c.w, uint64(n))
c.monitor.WriteBytes(n)
if err := c.wl.WaitN(context.Background(), n); err != nil {
return n, err
Expand Down

0 comments on commit ec3718e

Please sign in to comment.