Skip to content

Commit

Permalink
Always work with UTC time (#97)
Browse files Browse the repository at this point in the history
Otherwise there are issues when running the test locally on a machine
that isn't in UTC. CURRENT_TIMESTAMP in the postgress container will
return a time in UTC, but time.Now() will return it in the local time
which messes up the TimeoutManager.

I found this when running tests with the TimeoutManager, and noticed it
was timing out peers immediately instead of after the minute that is
configured.

Since we can't test the database in tests we can't write a feature test
for this yet. We'll have to write this after
#65
  • Loading branch information
erikdubbelboer authored Jun 18, 2024
1 parent 491acee commit 3f7c2f7
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 9 deletions.
2 changes: 1 addition & 1 deletion internal/metrics/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (c *Client) Record(ctx context.Context, category, action, game, peerID, lob

func (c *Client) RecordEvent(ctx context.Context, params EventParams) {
logger := logging.GetLogger(ctx)
now := util.Now(ctx)
now := util.NowUTC(ctx)
remoteAddr, _ := ctx.Value(remoteAddrKey).(string)
userAgent, _ := ctx.Value(userAgentKey).(string)

Expand Down
2 changes: 1 addition & 1 deletion internal/signaling/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func Handler(ctx context.Context, store stores.Store, cloudflare *cloudflare.Cre
select {
case <-ticker.C:
logger.Info("cleaning empty lobbies")
if err := store.CleanEmptyLobbies(ctx, time.Now().Add(-LobbyCleanThreshold)); err != nil {
if err := store.CleanEmptyLobbies(ctx, util.NowUTC(ctx).Add(-LobbyCleanThreshold)); err != nil {
logger.Error("failed to clean empty lobbies", zap.Error(err))
}
case <-ctx.Done():
Expand Down
10 changes: 5 additions & 5 deletions internal/signaling/stores/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func (s *PostgresStore) CreateLobby(ctx context.Context, game, lobbyCode, peerID
logger.Warn("peer id too long", zap.String("peerID", peerID))
return ErrInvalidPeerID
}
now := util.Now(ctx)
now := util.NowUTC(ctx)
res, err := s.DB.Exec(ctx, `
INSERT INTO lobbies (code, game, public, meta, created_at, updated_at)
VALUES ($1, $2, $3, $4, $5, $5)
Expand All @@ -175,7 +175,7 @@ func (s *PostgresStore) JoinLobby(ctx context.Context, game, lobbyCode, peerID s
return nil, ErrInvalidPeerID
}

now := util.Now(ctx)
now := util.NowUTC(ctx)

tx, err := s.DB.Begin(ctx)
if err != nil {
Expand Down Expand Up @@ -240,7 +240,7 @@ func (s *PostgresStore) IsPeerInLobby(ctx context.Context, game, lobbyCode, peer
}

func (s *PostgresStore) LeaveLobby(ctx context.Context, game, lobbyCode, peerID string) ([]string, error) {
now := util.Now(ctx)
now := util.NowUTC(ctx)

var peerlist []string
err := s.DB.QueryRow(ctx, `
Expand Down Expand Up @@ -347,7 +347,7 @@ func (s *PostgresStore) TimeoutPeer(ctx context.Context, peerID, secret, gameID
}
}

now := util.Now(ctx)
now := util.NowUTC(ctx)
_, err := s.DB.Exec(ctx, `
INSERT INTO timeouts (peer, secret, game, lobbies, created_at, updated_at)
VALUES ($1, $2, $3, $4, $5, $5)
Expand Down Expand Up @@ -387,7 +387,7 @@ func (s *PostgresStore) ReconnectPeer(ctx context.Context, peerID, secret, gameI
}

func (s *PostgresStore) ClaimNextTimedOutPeer(ctx context.Context, threshold time.Duration, callback func(peerID, gameID string, lobbies []string) error) (more bool, err error) {
now := util.Now(ctx)
now := util.NowUTC(ctx)

tx, err := s.DB.Begin(ctx)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions internal/util/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ import (
"time"
)

func Now(ctx context.Context) time.Time {
return time.Now()
func NowUTC(ctx context.Context) time.Time {
return time.Now().UTC()
}

0 comments on commit 3f7c2f7

Please sign in to comment.