Skip to content

Commit 015e196

Browse files
authored
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.
1 parent e90f6db commit 015e196

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

conn.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -244,8 +244,8 @@ type Conn struct {
244244
subprotocol string
245245

246246
// Write fields
247-
mu chan bool // used as mutex to protect write to conn
248-
writeBuf []byte // frame is constructed in this buffer.
247+
mu chan struct{} // used as mutex to protect write to conn
248+
writeBuf []byte // frame is constructed in this buffer.
249249
writePool BufferPool
250250
writeBufSize int
251251
writeDeadline time.Time
@@ -302,8 +302,8 @@ func newConn(conn net.Conn, isServer bool, readBufferSize, writeBufferSize int,
302302
writeBuf = make([]byte, writeBufferSize)
303303
}
304304

305-
mu := make(chan bool, 1)
306-
mu <- true
305+
mu := make(chan struct{}, 1)
306+
mu <- struct{}{}
307307
c := &Conn{
308308
isServer: isServer,
309309
br: br,
@@ -377,7 +377,7 @@ func (c *Conn) read(n int) ([]byte, error) {
377377

378378
func (c *Conn) write(frameType int, deadline time.Time, buf0, buf1 []byte) error {
379379
<-c.mu
380-
defer func() { c.mu <- true }()
380+
defer func() { c.mu <- struct{}{} }()
381381

382382
c.writeErrMu.Lock()
383383
err := c.writeErr
@@ -444,7 +444,7 @@ func (c *Conn) WriteControl(messageType int, data []byte, deadline time.Time) er
444444
case <-timer.C:
445445
return errWriteTimeout
446446
}
447-
defer func() { c.mu <- true }()
447+
defer func() { c.mu <- struct{}{} }()
448448

449449
c.writeErrMu.Lock()
450450
err := c.writeErr

prepared.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ func (pm *PreparedMessage) frame(key prepareKey) (int, []byte, error) {
7373
// Prepare a frame using a 'fake' connection.
7474
// TODO: Refactor code in conn.go to allow more direct construction of
7575
// the frame.
76-
mu := make(chan bool, 1)
77-
mu <- true
76+
mu := make(chan struct{}, 1)
77+
mu <- struct{}{}
7878
var nc prepareConn
7979
c := &Conn{
8080
conn: &nc,

0 commit comments

Comments
 (0)