-
Notifications
You must be signed in to change notification settings - Fork 58
Add latency to lobby listings
#285
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
base: main
Are you sure you want to change the base?
Conversation
`latency` contains the average latency to all the peers in the lobby. This can be useful to filter on or sort by to find lobbies weer peers close to you.
5f07419 to
e6626fa
Compare
|
|
||
| var lobbies []Lobby | ||
| rows, err := s.DB.Query(ctx, ` | ||
| WITH lobbies AS ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NIT: "lobbies" is also the table name, that might be confusing
| latency += Math.round((performance.now() - start) / 2) // Divide by 2 to estimate one-way latency. | ||
| } | ||
|
|
||
| return Math.round(latency / pings) // Average of two measurements. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NIT: two? or multiple
| now := util.NowUTC(ctx) | ||
|
|
||
| if len(latency) == 0 { | ||
| _, err := s.DB.Exec(ctx, ` | ||
| UPDATE peers | ||
| SET | ||
| latency_vector = NULL, | ||
| updated_at = $1 | ||
| WHERE peer = $2 | ||
| `, now, peerID) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| } else { | ||
| _, err := s.DB.Exec(ctx, ` | ||
| UPDATE peers | ||
| SET | ||
| latency_vector = $1, | ||
| updated_at = $2 | ||
| WHERE peer = $3 | ||
| `, pgvector.NewVector(latency), now, peerID) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we combine this?
var latencyVec pgvector.Vector
if len(latency) > 0 {
latencyVec = pgvector.NewVector(latency)
}
_, err := s.DB.Exec(ctx, `
UPDATE peers
SET
latency_vector = $1,
updated_at = $2
WHERE peer = $3
`, latencyVec, now, peerID)
latencycontains the average latency to all the peers in the lobby. This can be useful to filter on or sort by to find lobbies weer peers close to you.