Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ require (
github.com/rs/xid v1.5.0
github.com/stretchr/testify v1.9.0
github.com/syndtr/goleveldb v1.0.0
go.uber.org/goleak v1.2.0
go.uber.org/multierr v1.11.0
go.uber.org/ratelimit v0.3.0
go.uber.org/zap v1.25.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,8 @@ golang.org/x/crypto v0.31.0 h1:ihbySMvVjLAeSH1IbfcRTkD/iNscyz8rGzjF/E5hV6U=
golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk=
golang.org/x/exp v0.0.0-20230315142452-642cacee5cc0 h1:pVgRXcIictcr+lBQIFeiwuwtDIs4eL21OuM9nyAADmo=
golang.org/x/exp v0.0.0-20230315142452-642cacee5cc0/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
golang.org/x/lint v0.0.0-20190930215403-16217165b5de h1:5hukYrvBGR8/eNkX5mdUezrA6JiaEZDtJb9Ei+1LlBs=
golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA=
Expand Down
70 changes: 41 additions & 29 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ type Client struct {
pubKey *rsa.PublicKey
quitChan chan struct{}
quitKeepAliveChan chan struct{}
keepAliveInterval time.Duration
disableHTTPFallback bool
token string
correlationIdLength int
Expand Down Expand Up @@ -157,6 +158,7 @@ func New(options *Options) (*Client, error) {
httpClient: httpclient,
token: token,
disableHTTPFallback: options.DisableHTTPFallback,
keepAliveInterval: options.KeepAliveInterval,
correlationIdLength: options.CorrelationIdLength,
CorrelationIdNonceLength: options.CorrelationIdNonceLength,
}
Expand Down Expand Up @@ -192,39 +194,41 @@ func New(options *Options) (*Client, error) {
}
}

// start a keep alive routine
client.quitKeepAliveChan = make(chan struct{})
if options.KeepAliveInterval > 0 {
ticker := time.NewTicker(options.KeepAliveInterval)
go func() {
for {
// exit if the client is closed
if client.State.Load() == Closed {
return client, nil
}

// startKeepAlive starts the keepalive goroutine if configured
func (c *Client) startKeepAlive() {
if c.keepAliveInterval <= 0 {
return
}
if c.quitKeepAliveChan != nil {
return
}
c.quitKeepAliveChan = make(chan struct{})
ticker := time.NewTicker(c.keepAliveInterval)
go func() {
for {
if c.State.Load() == Closed {
return
}
select {
case <-ticker.C:
pubKeyData, err := encodePublicKey(c.pubKey)
if err != nil {
return
}
select {
case <-ticker.C:
// todo: internal logic needs a complete redesign
pubKeyData, err := encodePublicKey(client.pubKey)
if err != nil {
return
}
// attempts to re-register - server will reject is already existing
registrationRequest, err := encodeRegistrationRequest(pubKeyData, client.secretKey, client.correlationID)
if err != nil {
return
}
// silently fails to re-register if the session is still alive
_ = client.performRegistration(client.serverURL.String(), registrationRequest)
case <-client.quitKeepAliveChan:
ticker.Stop()
registrationRequest, err := encodeRegistrationRequest(pubKeyData, c.secretKey, c.correlationID)
if err != nil {
return
}
_ = c.performRegistration(c.serverURL.String(), registrationRequest)
case <-c.quitKeepAliveChan:
ticker.Stop()
return
}
}()
}

return client, nil
}
}()
}

// initializeRSAKeys does the one-time initialization for RSA crypto mechanism
Expand Down Expand Up @@ -367,6 +371,7 @@ func (c *Client) StartPolling(duration time.Duration, callback InteractionCallba
}

c.State.Store(Polling)
c.startKeepAlive()

ticker := time.NewTicker(duration)
c.quitChan = make(chan struct{})
Expand Down Expand Up @@ -518,6 +523,10 @@ func (c *Client) StopPolling() error {
return errors.New("client is not polling")
}
close(c.quitChan)
if c.quitKeepAliveChan != nil {
close(c.quitKeepAliveChan)
c.quitKeepAliveChan = nil
}

c.State.Store(Idle)

Expand All @@ -537,7 +546,10 @@ func (c *Client) Close() error {
return errors.New("client is already closed")
}

close(c.quitKeepAliveChan)
if c.quitKeepAliveChan != nil {
close(c.quitKeepAliveChan)
c.quitKeepAliveChan = nil
}

register := server.DeregisterRequest{
CorrelationID: c.correlationID,
Expand Down
Loading
Loading