Skip to content

Commit

Permalink
Merge pull request #471 from coder/dev
Browse files Browse the repository at this point in the history
Sunset the dev branch
  • Loading branch information
mafredri authored Aug 15, 2024
2 parents b366270 + 0978a2f commit e46e020
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 52 deletions.
6 changes: 3 additions & 3 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ type Conn struct {
closeMu sync.Mutex
closing bool

pingCounter int32
pingCounter atomic.Int64
activePingsMu sync.Mutex
activePings map[string]chan<- struct{}
}
Expand Down Expand Up @@ -200,9 +200,9 @@ func (c *Conn) flate() bool {
//
// TCP Keepalives should suffice for most use cases.
func (c *Conn) Ping(ctx context.Context) error {
p := atomic.AddInt32(&c.pingCounter, 1)
p := c.pingCounter.Add(1)

err := c.ping(ctx, strconv.Itoa(int(p)))
err := c.ping(ctx, strconv.FormatInt(p, 10))
if err != nil {
return fmt.Errorf("failed to ping: %w", err)
}
Expand Down
23 changes: 0 additions & 23 deletions internal/xsync/int64.go

This file was deleted.

41 changes: 19 additions & 22 deletions netconn.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func NetConn(ctx context.Context, c *Conn, msgType MessageType) net.Conn {
defer nc.writeMu.unlock()

// Prevents future writes from writing until the deadline is reset.
atomic.StoreInt64(&nc.writeExpired, 1)
nc.writeExpired.Store(1)
})
if !nc.writeTimer.Stop() {
<-nc.writeTimer.C
Expand All @@ -84,7 +84,7 @@ func NetConn(ctx context.Context, c *Conn, msgType MessageType) net.Conn {
defer nc.readMu.unlock()

// Prevents future reads from reading until the deadline is reset.
atomic.StoreInt64(&nc.readExpired, 1)
nc.readExpired.Store(1)
})
if !nc.readTimer.Stop() {
<-nc.readTimer.C
Expand All @@ -94,25 +94,22 @@ func NetConn(ctx context.Context, c *Conn, msgType MessageType) net.Conn {
}

type netConn struct {
// These must be first to be aligned on 32 bit platforms.
// https://github.com/nhooyr/websocket/pull/438
readExpired int64
writeExpired int64

c *Conn
msgType MessageType

writeTimer *time.Timer
writeMu *mu
writeCtx context.Context
writeCancel context.CancelFunc

readTimer *time.Timer
readMu *mu
readCtx context.Context
readCancel context.CancelFunc
readEOFed bool
reader io.Reader
writeTimer *time.Timer
writeMu *mu
writeExpired atomic.Int64
writeCtx context.Context
writeCancel context.CancelFunc

readTimer *time.Timer
readMu *mu
readExpired atomic.Int64
readCtx context.Context
readCancel context.CancelFunc
readEOFed bool
reader io.Reader
}

var _ net.Conn = &netConn{}
Expand All @@ -129,7 +126,7 @@ func (nc *netConn) Write(p []byte) (int, error) {
nc.writeMu.forceLock()
defer nc.writeMu.unlock()

if atomic.LoadInt64(&nc.writeExpired) == 1 {
if nc.writeExpired.Load() == 1 {
return 0, fmt.Errorf("failed to write: %w", context.DeadlineExceeded)
}

Expand Down Expand Up @@ -157,7 +154,7 @@ func (nc *netConn) Read(p []byte) (int, error) {
}

func (nc *netConn) read(p []byte) (int, error) {
if atomic.LoadInt64(&nc.readExpired) == 1 {
if nc.readExpired.Load() == 1 {
return 0, fmt.Errorf("failed to read: %w", context.DeadlineExceeded)
}

Expand Down Expand Up @@ -209,7 +206,7 @@ func (nc *netConn) SetDeadline(t time.Time) error {
}

func (nc *netConn) SetWriteDeadline(t time.Time) error {
atomic.StoreInt64(&nc.writeExpired, 0)
nc.writeExpired.Store(0)
if t.IsZero() {
nc.writeTimer.Stop()
} else {
Expand All @@ -223,7 +220,7 @@ func (nc *netConn) SetWriteDeadline(t time.Time) error {
}

func (nc *netConn) SetReadDeadline(t time.Time) error {
atomic.StoreInt64(&nc.readExpired, 0)
nc.readExpired.Store(0)
if t.IsZero() {
nc.readTimer.Stop()
} else {
Expand Down
4 changes: 2 additions & 2 deletions read.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import (
"io"
"net"
"strings"
"sync/atomic"
"time"

"github.com/coder/websocket/internal/errd"
"github.com/coder/websocket/internal/util"
"github.com/coder/websocket/internal/xsync"
)

// Reader reads from the connection until there is a WebSocket
Expand Down Expand Up @@ -465,7 +465,7 @@ func (mr *msgReader) read(p []byte) (int, error) {
type limitReader struct {
c *Conn
r io.Reader
limit xsync.Int64
limit atomic.Int64
n int64
}

Expand Down
4 changes: 2 additions & 2 deletions ws_js.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import (
"runtime"
"strings"
"sync"
"sync/atomic"
"syscall/js"

"github.com/coder/websocket/internal/bpool"
"github.com/coder/websocket/internal/wsjs"
"github.com/coder/websocket/internal/xsync"
)

// opcode represents a WebSocket opcode.
Expand Down Expand Up @@ -45,7 +45,7 @@ type Conn struct {
ws wsjs.WebSocket

// read limit for a message in bytes.
msgReadLimit xsync.Int64
msgReadLimit atomic.Int64

closeReadMu sync.Mutex
closeReadCtx context.Context
Expand Down

0 comments on commit e46e020

Please sign in to comment.