forked from mirror/websocket
Use empty struct to protect writing (#566)
Using empty struct for signaling is more idiomatic compared to booleans because users might wonder what happens on false or true. Empty struct removes this problem. There is also a side benefit of occupying less memory but it should be negligible in this case.
This commit is contained in:
parent
e90f6db575
commit
015e196e21
12
conn.go
12
conn.go
|
@ -244,8 +244,8 @@ type Conn struct {
|
||||||
subprotocol string
|
subprotocol string
|
||||||
|
|
||||||
// Write fields
|
// Write fields
|
||||||
mu chan bool // used as mutex to protect write to conn
|
mu chan struct{} // used as mutex to protect write to conn
|
||||||
writeBuf []byte // frame is constructed in this buffer.
|
writeBuf []byte // frame is constructed in this buffer.
|
||||||
writePool BufferPool
|
writePool BufferPool
|
||||||
writeBufSize int
|
writeBufSize int
|
||||||
writeDeadline time.Time
|
writeDeadline time.Time
|
||||||
|
@ -302,8 +302,8 @@ func newConn(conn net.Conn, isServer bool, readBufferSize, writeBufferSize int,
|
||||||
writeBuf = make([]byte, writeBufferSize)
|
writeBuf = make([]byte, writeBufferSize)
|
||||||
}
|
}
|
||||||
|
|
||||||
mu := make(chan bool, 1)
|
mu := make(chan struct{}, 1)
|
||||||
mu <- true
|
mu <- struct{}{}
|
||||||
c := &Conn{
|
c := &Conn{
|
||||||
isServer: isServer,
|
isServer: isServer,
|
||||||
br: br,
|
br: br,
|
||||||
|
@ -377,7 +377,7 @@ func (c *Conn) read(n int) ([]byte, error) {
|
||||||
|
|
||||||
func (c *Conn) write(frameType int, deadline time.Time, buf0, buf1 []byte) error {
|
func (c *Conn) write(frameType int, deadline time.Time, buf0, buf1 []byte) error {
|
||||||
<-c.mu
|
<-c.mu
|
||||||
defer func() { c.mu <- true }()
|
defer func() { c.mu <- struct{}{} }()
|
||||||
|
|
||||||
c.writeErrMu.Lock()
|
c.writeErrMu.Lock()
|
||||||
err := c.writeErr
|
err := c.writeErr
|
||||||
|
@ -444,7 +444,7 @@ func (c *Conn) WriteControl(messageType int, data []byte, deadline time.Time) er
|
||||||
case <-timer.C:
|
case <-timer.C:
|
||||||
return errWriteTimeout
|
return errWriteTimeout
|
||||||
}
|
}
|
||||||
defer func() { c.mu <- true }()
|
defer func() { c.mu <- struct{}{} }()
|
||||||
|
|
||||||
c.writeErrMu.Lock()
|
c.writeErrMu.Lock()
|
||||||
err := c.writeErr
|
err := c.writeErr
|
||||||
|
|
|
@ -73,8 +73,8 @@ func (pm *PreparedMessage) frame(key prepareKey) (int, []byte, error) {
|
||||||
// Prepare a frame using a 'fake' connection.
|
// Prepare a frame using a 'fake' connection.
|
||||||
// TODO: Refactor code in conn.go to allow more direct construction of
|
// TODO: Refactor code in conn.go to allow more direct construction of
|
||||||
// the frame.
|
// the frame.
|
||||||
mu := make(chan bool, 1)
|
mu := make(chan struct{}, 1)
|
||||||
mu <- true
|
mu <- struct{}{}
|
||||||
var nc prepareConn
|
var nc prepareConn
|
||||||
c := &Conn{
|
c := &Conn{
|
||||||
conn: &nc,
|
conn: &nc,
|
||||||
|
|
Loading…
Reference in New Issue