Skip to content

Small code changes #150

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ import (
"time"
)

func init() {
rand.Seed(time.Now().UnixNano())
}

// Conn represents a WebSocket connection.
// All methods may be called concurrently except for Reader, Read
// and SetReadLimit.
Expand Down Expand Up @@ -70,7 +74,7 @@ type Conn struct {
activeReader *messageReader
// readFrameLock is acquired to read from bw.
readFrameLock chan struct{}
readClosed int64
isReadClosed int64
readHeaderBuf []byte
controlPayloadBuf []byte

Expand Down Expand Up @@ -341,7 +345,7 @@ func (c *Conn) handleControl(ctx context.Context, h header) error {
// See https://github.com/nhooyr/websocket/issues/87#issue-451703332
// Most users should not need this.
func (c *Conn) Reader(ctx context.Context) (MessageType, io.Reader, error) {
if atomic.LoadInt64(&c.readClosed) == 1 {
if atomic.LoadInt64(&c.isReadClosed) == 1 {
return 0, nil, fmt.Errorf("websocket connection read closed")
}

Expand Down Expand Up @@ -685,9 +689,8 @@ func (c *Conn) writeFrame(ctx context.Context, fin bool, opcode opcode, p []byte
case <-c.closed:
return n, c.closeErr
case c.setWriteTimeout <- context.Background():
return n, nil
}

return n, nil
}

func (c *Conn) realWriteFrame(ctx context.Context, h header, p []byte) (n int, err error) {
Expand Down Expand Up @@ -838,10 +841,6 @@ func (c *Conn) writeClose(p []byte, cerr error) error {
return nil
}

func init() {
rand.Seed(time.Now().UnixNano())
}

// Ping sends a ping to the peer and waits for a pong.
// Use this to measure latency or ensure the peer is responsive.
// Ping must be called concurrently with Reader as it does
Expand Down
2 changes: 1 addition & 1 deletion conn_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func (c *netConn) SetReadDeadline(t time.Time) error {
// Use this when you do not want to read data messages from the connection anymore but will
// want to write messages to it.
func (c *Conn) CloseRead(ctx context.Context) context.Context {
atomic.StoreInt64(&c.readClosed, 1)
atomic.StoreInt64(&c.isReadClosed, 1)

ctx, cancel := context.WithCancel(ctx)
go func() {
Expand Down