go conventions: context should be first argument.

This commit is contained in:
Erik Brakkee 2024-08-19 19:22:44 +02:00
parent c1ade0408d
commit 6abcf0a7af
3 changed files with 5 additions and 6 deletions

View File

@ -14,7 +14,7 @@ vet: fmt
go vet ./...
test:
go test -v ./...
go test -count=1 -v ./...
build: generate vet
mkdir -p bin

View File

@ -213,13 +213,13 @@ func setupWebSockets(admin *matchmaker.MatchMaker, websessions *ui.WebSessions)
sessionService := websocketutil.WebSocketService{
Handler: func(w http.ResponseWriter, r *http.Request, conn net.Conn) {
ctx, cancel := context.WithCancel(context.Background())
websession := websessions.NewSession(conn, ctx, cancel)
websession := websessions.NewSession(ctx, cancel, conn)
defer websessions.SessionClosed(websession)
location, err := ui.GetUserLocation(r)
if err != nil {
panic(err)
}
websession.WriteNotifications(location, ctx, cancel)
websession.WriteNotifications(ctx, cancel, location)
},
Text: true,
}

View File

@ -69,8 +69,7 @@ func (sessions *WebSessions) notifyWebSessions(notification *models.State) {
}
}
func (sessions *WebSessions) NewSession(wsConnection net.Conn, ctx context.Context,
cancel context.CancelFunc) *WebSession {
func (sessions *WebSessions) NewSession(ctx context.Context, cancel context.CancelFunc, wsConnection net.Conn) *WebSession {
sessions.mutex.Lock()
defer sessions.mutex.Unlock()
session := &WebSession{
@ -108,7 +107,7 @@ func (sessions *WebSessions) NewSession(wsConnection net.Conn, ctx context.Conte
return session
}
func (session *WebSession) WriteNotifications(location *time.Location, ctx context.Context, cancel context.CancelFunc) {
func (session *WebSession) WriteNotifications(ctx context.Context, cancel context.CancelFunc, location *time.Location) {
timer := time.NewTicker(10 * time.Second)
defer timer.Stop()
// if for some reason we cannot send notifications to the web client then the context is canceled.