diff --git a/go.mod b/go.mod index 62ff58a..a253d7d 100644 --- a/go.mod +++ b/go.mod @@ -12,6 +12,7 @@ require ( github.com/prometheus/client_golang v1.6.0 github.com/speps/go-hashids v2.0.0+incompatible github.com/tomwright/queryparam/v4 v4.1.0 + github.com/zikaeroh/ctxjoin v0.0.0-20200613235025-e3d47af29310 go.uber.org/atomic v1.6.0 golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a gotest.tools/v3 v3.0.2 diff --git a/go.sum b/go.sum index 624dd1d..8640201 100644 --- a/go.sum +++ b/go.sum @@ -133,6 +133,8 @@ github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo= github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw= github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs= github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY= +github.com/zikaeroh/ctxjoin v0.0.0-20200613235025-e3d47af29310 h1:nzMukvhYHxWxiSNaa0J7E5Wx9XPEh5K3GtVjcF3yWdM= +github.com/zikaeroh/ctxjoin v0.0.0-20200613235025-e3d47af29310/go.mod h1:bR1HcUSJKqE19Z24xgoSc1nMAbWr+P0nuhzx2kYJv8M= github.com/zikaeroh/pkger v0.17.1-0.20200604025301-dceb832975ba h1:t0aP/yT6220rbPXYRf4ASwgWkynUvy+SGQqR6jNzutc= github.com/zikaeroh/pkger v0.17.1-0.20200604025301-dceb832975ba/go.mod h1:0JoVlrol20BSywW79rN3kdFFsE5xYM+rSCQDXbLhiuI= go.uber.org/atomic v1.6.0 h1:Ezj3JGmsOnG1MoRWQkPBsKLe9DwWD9QeXzTRzzldNVk= diff --git a/internal/server/server.go b/internal/server/server.go index 585b281..7ee7e3f 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -12,6 +12,7 @@ import ( "github.com/speps/go-hashids" "github.com/zikaeroh/codies/internal/game" "github.com/zikaeroh/codies/internal/protocol" + "github.com/zikaeroh/ctxjoin" "go.uber.org/atomic" "golang.org/x/sync/errgroup" "nhooyr.io/websocket" @@ -222,7 +223,10 @@ type Room struct { type noteSender func(protocol.ServerNote) -func (r *Room) HandleConn(playerID uuid.UUID, nickname string, c *websocket.Conn) { +func (r *Room) HandleConn(ctx context.Context, playerID uuid.UUID, nickname string, c *websocket.Conn) { + ctx, cancel := ctxjoin.AddCancel(ctx, r.ctx) + defer cancel() + metricClients.Inc() defer metricClients.Dec() @@ -235,7 +239,7 @@ func (r *Room) HandleConn(playerID uuid.UUID, nickname string, c *websocket.Conn }() defer c.Close(websocket.StatusGoingAway, "going away") - g, ctx := errgroup.WithContext(r.ctx) + g, ctx := errgroup.WithContext(ctx) r.mu.Lock() r.players[playerID] = func(s protocol.ServerNote) { diff --git a/main.go b/main.go index b487ea5..ab6277d 100644 --- a/main.go +++ b/main.go @@ -210,7 +210,7 @@ func main() { } g.Go(func() error { - room.HandleConn(query.PlayerID, query.Nickname, c) + room.HandleConn(ctx, query.PlayerID, query.Nickname, c) return nil }) })