Skip to content

Commit

Permalink
Merge pull request #1 from iluozhiqiang/feature/async_read
Browse files Browse the repository at this point in the history
add allowReadControlMessages field, support non-blocking read
  • Loading branch information
iluozhiqiang authored May 13, 2021
2 parents e8629af + da244de commit 879d2c0
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,9 +255,10 @@ type Conn struct {
writeErrMu sync.Mutex
writeErr error

enableWriteCompression bool
compressionLevel int
newCompressionWriter func(io.WriteCloser, int) io.WriteCloser
allowReadControlMessages bool
enableWriteCompression bool
compressionLevel int
newCompressionWriter func(io.WriteCloser, int) io.WriteCloser

// Read fields
reader io.ReadCloser // the current reader returned to the application
Expand Down Expand Up @@ -355,7 +356,6 @@ func (c *Conn) RemoteAddr() net.Addr {
}

// Write methods

func (c *Conn) writeFatal(err error) error {
err = hideTempErr(err)
c.writeErrMu.Lock()
Expand Down Expand Up @@ -991,6 +991,9 @@ func (c *Conn) NextReader() (messageType int, r io.Reader, err error) {
}
return frameType, c.reader, nil
}
if c.allowReadControlMessages {
return frameType, nil, c.readErr
}
}

// Applications that do handle the error returned from this method spin in
Expand Down Expand Up @@ -1077,6 +1080,12 @@ func (c *Conn) SetReadDeadline(t time.Time) error {
return c.conn.SetReadDeadline(t)
}

// AllowReadControlMessages allows the ReadMessage method to return ControlMessage
// without having to wait for TextMessage And BinaryMessage
func (c *Conn) AllowReadControlMessages() {
c.allowReadControlMessages = true
}

// SetReadLimit sets the maximum size in bytes for a message read from the peer. If a
// message exceeds the limit, the connection sends a close message to the peer
// and returns ErrReadLimit to the application.
Expand Down

0 comments on commit 879d2c0

Please sign in to comment.