Skip to content

Commit afe94af

Browse files
committed
Use new atomic types from Go 1.19
This is a cleaner solution for the fix in #438 thanks to the fact that Go 1.19 now is the default and the atomic.Int64 types are automatically aligned correctly on 32 bit systems. Using this also means that xsync.Int64 can be removed. The new atomic.Int64 type solves the issue and should be quite a lot faster as it avoids the interface conversion.
1 parent e87d61a commit afe94af

File tree

5 files changed

+25
-51
lines changed

5 files changed

+25
-51
lines changed

conn.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ type Conn struct {
7777
closeMu sync.Mutex
7878
closing bool
7979

80-
pingCounter int32
80+
pingCounter atomic.Int32
8181
activePingsMu sync.Mutex
8282
activePings map[string]chan<- struct{}
8383
}
@@ -200,7 +200,7 @@ func (c *Conn) flate() bool {
200200
//
201201
// TCP Keepalives should suffice for most use cases.
202202
func (c *Conn) Ping(ctx context.Context) error {
203-
p := atomic.AddInt32(&c.pingCounter, 1)
203+
p := c.pingCounter.Add(1)
204204

205205
err := c.ping(ctx, strconv.Itoa(int(p)))
206206
if err != nil {

internal/xsync/int64.go

-23
This file was deleted.

netconn.go

+19-22
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func NetConn(ctx context.Context, c *Conn, msgType MessageType) net.Conn {
6868
defer nc.writeMu.unlock()
6969

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

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

9696
type netConn struct {
97-
// These must be first to be aligned on 32 bit platforms.
98-
// https://github.com/nhooyr/websocket/pull/438
99-
readExpired int64
100-
writeExpired int64
101-
10297
c *Conn
10398
msgType MessageType
10499

105-
writeTimer *time.Timer
106-
writeMu *mu
107-
writeCtx context.Context
108-
writeCancel context.CancelFunc
109-
110-
readTimer *time.Timer
111-
readMu *mu
112-
readCtx context.Context
113-
readCancel context.CancelFunc
114-
readEOFed bool
115-
reader io.Reader
100+
writeTimer *time.Timer
101+
writeMu *mu
102+
writeExpired atomic.Int64
103+
writeCtx context.Context
104+
writeCancel context.CancelFunc
105+
106+
readTimer *time.Timer
107+
readMu *mu
108+
readExpired atomic.Int64
109+
readCtx context.Context
110+
readCancel context.CancelFunc
111+
readEOFed bool
112+
reader io.Reader
116113
}
117114

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

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

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

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

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

211208
func (nc *netConn) SetWriteDeadline(t time.Time) error {
212-
atomic.StoreInt64(&nc.writeExpired, 0)
209+
nc.writeExpired.Store(0)
213210
if t.IsZero() {
214211
nc.writeTimer.Stop()
215212
} else {
@@ -223,7 +220,7 @@ func (nc *netConn) SetWriteDeadline(t time.Time) error {
223220
}
224221

225222
func (nc *netConn) SetReadDeadline(t time.Time) error {
226-
atomic.StoreInt64(&nc.readExpired, 0)
223+
nc.readExpired.Store(0)
227224
if t.IsZero() {
228225
nc.readTimer.Stop()
229226
} else {

read.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ import (
1111
"io"
1212
"net"
1313
"strings"
14+
"sync/atomic"
1415
"time"
1516

1617
"nhooyr.io/websocket/internal/errd"
1718
"nhooyr.io/websocket/internal/util"
18-
"nhooyr.io/websocket/internal/xsync"
1919
)
2020

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

ws_js.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ import (
1212
"runtime"
1313
"strings"
1414
"sync"
15+
"sync/atomic"
1516
"syscall/js"
1617

1718
"nhooyr.io/websocket/internal/bpool"
1819
"nhooyr.io/websocket/internal/wsjs"
19-
"nhooyr.io/websocket/internal/xsync"
2020
)
2121

2222
// opcode represents a WebSocket opcode.
@@ -45,7 +45,7 @@ type Conn struct {
4545
ws wsjs.WebSocket
4646

4747
// read limit for a message in bytes.
48-
msgReadLimit xsync.Int64
48+
msgReadLimit atomic.Int64
4949

5050
closeReadMu sync.Mutex
5151
closeReadCtx context.Context

0 commit comments

Comments
 (0)