Skip to content

Commit

Permalink
Option to send server time in connect result (#379)
Browse files Browse the repository at this point in the history
  • Loading branch information
FZambia committed May 16, 2024
1 parent 82b314f commit a465a9d
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 1 deletion.
5 changes: 4 additions & 1 deletion _examples/recovery_mode_cache/readme.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
Demonstration on using cache recovery mode.
Demonstration of using cache recovery mode.

```
go run main.go
```

Then go to http://localhost:8000

The latest message in channel is delivered to a subscriber immediately after subscription so that client gets an
actual state immediately from publication history.
3 changes: 3 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2331,6 +2331,9 @@ func (c *Client) connectCmd(req *protocol.ConnectRequest, cmd *protocol.Command,
if c.transport.Emulation() {
res.Node = c.node.ID()
}
if c.node.config.ClientConnectIncludeServerTime {
res.Time = time.Now().UnixMilli()
}

// Client successfully connected.
c.mu.Lock()
Expand Down
2 changes: 2 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ func TestClientConnectNoCredentialsNoToken(t *testing.T) {
func TestClientConnectContextCredentials(t *testing.T) {
node := defaultTestNode()
defer func() { _ = node.Shutdown(context.Background()) }()
node.config.ClientConnectIncludeServerTime = true

transport := newTestTransport(func() {})
ctx := context.Background()
Expand All @@ -284,6 +285,7 @@ func TestClientConnectContextCredentials(t *testing.T) {
result := extractConnectReply(rwWrapper.replies)
require.Equal(t, false, result.Expires)
require.Equal(t, uint32(0), result.Ttl)
require.NotZero(t, result.Time)
require.True(t, client.authenticated)
require.Equal(t, "42", client.UserID())
}
Expand Down
4 changes: 4 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ type Config struct {
// aggregation. It's not reasonable to have it less than one second.
// Zero value means 60 * time.Second.
NodeInfoMetricsAggregateInterval time.Duration
// ClientConnectIncludeServerTime tells Centrifuge to append `time` field to Connect result of client protocol.
// This field contains Unix timestamp in milliseconds and represents current server time. By default, server time
// is not included.
ClientConnectIncludeServerTime bool
// ClientPresenceUpdateInterval sets an interval how often connected
// clients update presence information.
// Zero value means 25 * time.Second.
Expand Down

0 comments on commit a465a9d

Please sign in to comment.