From 2b65b72066acc7211064dce37ebc1bf95b836274 Mon Sep 17 00:00:00 2001 From: FZambia Date: Fri, 10 May 2024 11:24:25 +0300 Subject: [PATCH] option to send server time in connect result --- _examples/go.mod | 4 ++-- _examples/go.sum | 12 ++---------- client.go | 3 +++ client_test.go | 2 ++ config.go | 3 +++ go.mod | 2 +- go.sum | 4 ++-- 7 files changed, 15 insertions(+), 15 deletions(-) diff --git a/_examples/go.mod b/_examples/go.mod index 8077ec19..b1ee08f8 100644 --- a/_examples/go.mod +++ b/_examples/go.mod @@ -7,7 +7,7 @@ replace github.com/centrifugal/centrifuge => ../ require ( github.com/FZambia/tarantool v0.2.2 github.com/centrifugal/centrifuge v0.8.2 - github.com/centrifugal/protocol v0.12.1 + github.com/centrifugal/protocol v0.12.2-0.20240510082212-95d0d6be417a github.com/cristalhq/jwt/v5 v5.4.0 github.com/dchest/uniuri v1.2.0 github.com/gin-contrib/sessions v0.0.3 @@ -30,7 +30,6 @@ require ( ) require ( - cloud.google.com/go/compute v1.24.0 // indirect cloud.google.com/go/compute/metadata v0.3.0 // indirect github.com/FZambia/eagle v0.1.0 // indirect github.com/Yiling-J/theine-go v0.3.2 // indirect @@ -42,6 +41,7 @@ require ( github.com/gabriel-vasile/mimetype v1.4.2 // indirect github.com/gammazero/deque v0.2.1 // indirect github.com/gin-contrib/sse v0.1.0 // indirect + github.com/go-logr/logr v1.4.1 // indirect github.com/go-playground/locales v0.14.1 // indirect github.com/go-playground/universal-translator v0.18.1 // indirect github.com/go-playground/validator/v10 v10.14.0 // indirect diff --git a/_examples/go.sum b/_examples/go.sum index 80f8aa8e..12d49d1a 100644 --- a/_examples/go.sum +++ b/_examples/go.sum @@ -1,7 +1,3 @@ -cloud.google.com/go/compute v1.24.0 h1:phWcR2eWzRJaL/kOiJwfFsPs4BaKq1j6vnpZrc1YlVg= -cloud.google.com/go/compute v1.24.0/go.mod h1:kw1/T+h/+tK2LJK0wiPPx1intgdAM3j/g3hFDlscY40= -cloud.google.com/go/compute/metadata v0.2.3 h1:mg4jlk7mCAj6xXp9UJ4fjI9VUI5rubuGBW5aJ7UnBMY= -cloud.google.com/go/compute/metadata v0.2.3/go.mod h1:VAV5nSsACxMJvgaAuX6Pk2AawlZn8kiOGuCv6gTkwuA= cloud.google.com/go/compute/metadata v0.3.0 h1:Tz+eQXMEqDIKRsmY3cHTL6FVaynIjX2QxYC4trgAKZc= cloud.google.com/go/compute/metadata v0.3.0/go.mod h1:zFmK7XCadkQkj6TtorcaGlCW1hT1fIilQDwofLpJ20k= github.com/FZambia/eagle v0.1.0 h1:9gyX6x+xjoIfglgyPTcYm7dvY7FJ93us1QY5De4CyXA= @@ -18,8 +14,8 @@ github.com/bradleypeabody/gorilla-sessions-memcache v0.0.0-20181103040241-659414 github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM= github.com/bytedance/sonic v1.9.1 h1:6iJ6NqdoxCDr6mbY8h18oSO+cShGSMRGCEo7F2h0x8s= github.com/bytedance/sonic v1.9.1/go.mod h1:i736AoUSYt75HyZLoJW9ERYxcy6eaN6h4BZXU064P/U= -github.com/centrifugal/protocol v0.12.1 h1:hGbIl9Y0UbVsESgLcsqgZ7duwEnrZebFUYdu5Opwzgo= -github.com/centrifugal/protocol v0.12.1/go.mod h1:5Z0SuNdXEt83Fkoi34BCyY23p1P8+zQakQS6/BfJHak= +github.com/centrifugal/protocol v0.12.2-0.20240510082212-95d0d6be417a h1:22cS8zoI28nYvewmoXnBvgzM5imqwzHcSfKltJ2f+ws= +github.com/centrifugal/protocol v0.12.2-0.20240510082212-95d0d6be417a/go.mod h1:lM54PGU/u5WupYSb755Zv6tZ2ju1SqNKCp6A4s0DeG4= github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44= github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY= @@ -200,8 +196,6 @@ golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc= golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/net v0.24.0 h1:1PcaxkF854Fu3+lvBIx5SYn9wRlBzzcnHZSiaFFAb0w= golang.org/x/net v0.24.0/go.mod h1:2Q7sJY5mzlzWjKtYUEXSlBWCdyaioyXzRB2RtU8KVE8= -golang.org/x/oauth2 v0.19.0 h1:9+E/EZBCbTLNrbN35fHv/a/d/mOBatymz1zbtQrXpIg= -golang.org/x/oauth2 v0.19.0/go.mod h1:vYi7skDa1x015PmRRYZ7+s1cWyPgrPiSYRe4rnsexc8= golang.org/x/oauth2 v0.20.0 h1:4mQdhULixXKP1rwYBW0vAijoXnkTG0BLCDRzfe1idMo= golang.org/x/oauth2 v0.20.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI= golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M= @@ -219,8 +213,6 @@ golang.org/x/tools v0.12.0 h1:YW6HUoUmYBpwSgyaGaZq1fHjrBjX1rlpZ54T6mu2kss= golang.org/x/tools v0.12.0/go.mod h1:Sc0INKfu04TlqNoRA1hgpFZbhYXHPr4V5DzpSBTPqQM= google.golang.org/genproto/googleapis/rpc v0.0.0-20240227224415-6ceb2ff114de h1:cZGRis4/ot9uVm639a+rHCUaG0JJHEsdyzSQTMX+suY= google.golang.org/genproto/googleapis/rpc v0.0.0-20240227224415-6ceb2ff114de/go.mod h1:H4O17MA/PE9BsGx3w+a+W2VOLLD1Qf7oJneAoU6WktY= -google.golang.org/grpc v1.63.0 h1:WjKe+dnvABXyPJMD7KDNLxtoGk5tgk+YFWN6cBWjZE8= -google.golang.org/grpc v1.63.0/go.mod h1:WAX/8DgncnokcFUldAxq7GeB5DXHDbMF+lLvDomNkRA= google.golang.org/grpc v1.63.2 h1:MUeiw1B2maTVZthpU5xvASfTh3LDbxHd6IJ6QQVU+xM= google.golang.org/grpc v1.63.2/go.mod h1:WAX/8DgncnokcFUldAxq7GeB5DXHDbMF+lLvDomNkRA= google.golang.org/protobuf v1.34.0 h1:Qo/qEd2RZPCf2nKuorzksSknv0d3ERwp1vFG38gSmH4= diff --git a/client.go b/client.go index 954fb277..2f95ccef 100644 --- a/client.go +++ b/client.go @@ -2323,6 +2323,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() diff --git a/client_test.go b/client_test.go index 4abded52..c83b3f7c 100644 --- a/client_test.go +++ b/client_test.go @@ -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() @@ -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.TimeMs) require.True(t, client.authenticated) require.Equal(t, "42", client.UserID()) } diff --git a/config.go b/config.go index 5de49aab..7f707f4a 100644 --- a/config.go +++ b/config.go @@ -20,6 +20,9 @@ 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_ms to Connect result of client protocol. + // 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. diff --git a/go.mod b/go.mod index cf15a0db..bfe46216 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.21 require ( github.com/FZambia/eagle v0.1.0 github.com/Yiling-J/theine-go v0.3.2 - github.com/centrifugal/protocol v0.12.1 + github.com/centrifugal/protocol v0.12.2-0.20240510082212-95d0d6be417a github.com/google/uuid v1.6.0 github.com/prometheus/client_golang v1.19.0 github.com/redis/rueidis v1.0.36 diff --git a/go.sum b/go.sum index 791254a4..577029a3 100644 --- a/go.sum +++ b/go.sum @@ -4,8 +4,8 @@ github.com/Yiling-J/theine-go v0.3.2 h1:XcSdMPV9DwBD9gqqSxbBfVJnP8CCiqNSqp3C6Ypm github.com/Yiling-J/theine-go v0.3.2/go.mod h1:ygLXqrWPZT/a+PzK5hQ0+a6gu0lpAY5IudTcgnPleqI= github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= -github.com/centrifugal/protocol v0.12.1 h1:hGbIl9Y0UbVsESgLcsqgZ7duwEnrZebFUYdu5Opwzgo= -github.com/centrifugal/protocol v0.12.1/go.mod h1:5Z0SuNdXEt83Fkoi34BCyY23p1P8+zQakQS6/BfJHak= +github.com/centrifugal/protocol v0.12.2-0.20240510082212-95d0d6be417a h1:22cS8zoI28nYvewmoXnBvgzM5imqwzHcSfKltJ2f+ws= +github.com/centrifugal/protocol v0.12.2-0.20240510082212-95d0d6be417a/go.mod h1:lM54PGU/u5WupYSb755Zv6tZ2ju1SqNKCp6A4s0DeG4= github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44= github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=