Skip to content

Modify NetConn to take a context as the first argument #131

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

Merged
merged 1 commit into from
Sep 1, 2019
Merged
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
13 changes: 8 additions & 5 deletions netconn.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ import (
// Every Write to the net.Conn will correspond to a message write of
// the given type on *websocket.Conn.
//
// If a message is read that is not of the correct type, an error
// will be thrown.
// The passed ctx bounds the lifetime of the net.Conn. If cancelled,
// all reads and writes on the net.Conn will be cancelled.
//
// If a message is read that is not of the correct type, the connection
// will be closed with StatusUnsupportedData and an error will be returned.
//
// Close will close the *websocket.Conn with StatusNormalClosure.
//
Expand All @@ -35,20 +38,20 @@ import (
//
// A received StatusNormalClosure or StatusGoingAway close frame will be translated to
// io.EOF when reading.
func NetConn(c *Conn, msgType MessageType) net.Conn {
func NetConn(ctx context.Context, c *Conn, msgType MessageType) net.Conn {
nc := &netConn{
c: c,
msgType: msgType,
}

var cancel context.CancelFunc
nc.writeContext, cancel = context.WithCancel(context.Background())
nc.writeContext, cancel = context.WithCancel(ctx)
nc.writeTimer = time.AfterFunc(math.MaxInt64, cancel)
if !nc.writeTimer.Stop() {
<-nc.writeTimer.C
}

nc.readContext, cancel = context.WithCancel(context.Background())
nc.readContext, cancel = context.WithCancel(ctx)
nc.readTimer = time.AfterFunc(math.MaxInt64, cancel)
if !nc.readTimer.Stop() {
<-nc.readTimer.C
Expand Down
8 changes: 4 additions & 4 deletions websocket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ func TestConn(t *testing.T) {
{
name: "netConn",
server: func(ctx context.Context, c *websocket.Conn) error {
nc := websocket.NetConn(c, websocket.MessageBinary)
nc := websocket.NetConn(ctx, c, websocket.MessageBinary)
defer nc.Close()

nc.SetWriteDeadline(time.Time{})
Expand All @@ -290,7 +290,7 @@ func TestConn(t *testing.T) {
return nil
},
client: func(ctx context.Context, c *websocket.Conn) error {
nc := websocket.NetConn(c, websocket.MessageBinary)
nc := websocket.NetConn(ctx, c, websocket.MessageBinary)

nc.SetReadDeadline(time.Time{})
time.Sleep(1)
Expand All @@ -317,7 +317,7 @@ func TestConn(t *testing.T) {
{
name: "netConn/badReadMsgType",
server: func(ctx context.Context, c *websocket.Conn) error {
nc := websocket.NetConn(c, websocket.MessageBinary)
nc := websocket.NetConn(ctx, c, websocket.MessageBinary)

nc.SetDeadline(time.Now().Add(time.Second * 15))

Expand All @@ -337,7 +337,7 @@ func TestConn(t *testing.T) {
{
name: "netConn/badRead",
server: func(ctx context.Context, c *websocket.Conn) error {
nc := websocket.NetConn(c, websocket.MessageBinary)
nc := websocket.NetConn(ctx, c, websocket.MessageBinary)
defer nc.Close()

nc.SetDeadline(time.Now().Add(time.Second * 15))
Expand Down