Skip to content

Commit 2ea151a

Browse files
committed
return close sent for all writes, remove sentinel error
1 parent e70e060 commit 2ea151a

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

Diff for: write.go

+9-7
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@ import (
2020
"github.com/coder/websocket/internal/util"
2121
)
2222

23-
// ErrAlreadyClosed is returned when a close frame has already been sent and another is attempted.
24-
var ErrAlreadyClosed = errors.New("close frame already sent")
25-
2623
// Writer returns a writer bounded by the context that will write
2724
// a WebSocket message of type dataType to the connection.
2825
//
@@ -252,6 +249,15 @@ func (c *Conn) writeFrame(ctx context.Context, fin bool, flate bool, opcode opco
252249
}
253250
defer c.writeFrameMu.unlock()
254251

252+
if c.closeSent {
253+
select {
254+
case <-c.closed:
255+
return 0, net.ErrClosed
256+
default:
257+
}
258+
return 0, errors.New("close sent")
259+
}
260+
255261
select {
256262
case <-c.closed:
257263
return 0, net.ErrClosed
@@ -271,10 +277,6 @@ func (c *Conn) writeFrame(ctx context.Context, fin bool, flate bool, opcode opco
271277
}
272278
}()
273279

274-
if opcode == opClose && c.closeSent {
275-
return 0, ErrAlreadyClosed
276-
}
277-
278280
c.writeHeader.fin = fin
279281
c.writeHeader.opcode = opcode
280282
c.writeHeader.payloadLength = int64(len(p))

0 commit comments

Comments
 (0)