Use new ctxjoin library to join to room context

This commit is contained in:
zikaeroh 2020-06-13 17:03:47 -07:00
parent 11fb5f52ab
commit 6c81171a5f
4 changed files with 10 additions and 3 deletions

1
go.mod
View File

@ -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

2
go.sum
View File

@ -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=

View File

@ -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) {

View File

@ -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
})
})