You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi there, recently I found this SDK, and it's awesome! However, when I use *Client.SendEvent in iotservice package, I encountered a weird problem, 50 minutes after calling this function, a log comes out as follows:
2021/03/22 11:41:47 ERROR put token error: amqp: session closed
// putTokenContinuously in v0.7.0func (c*Client) putTokenContinuously(ctx context.Context, conn*amqp.Client) error {
const (
tokenUpdateInterval=time.Hour// we need to update tokens before they expire to prevent disconnects// from azure, without interrupting the message flowtokenUpdateSpan=10*time.Minute
)
sess, err:=conn.NewSession()
iferr!=nil {
returnerr
}
defersess.Close(context.Background())
iferr:=c.putToken(ctx, sess, tokenUpdateInterval); err!=nil {
returnerr
}
gofunc() {
ticker:=time.NewTimer(tokenUpdateInterval-tokenUpdateSpan)
deferticker.Stop()
for {
select {
case<-ticker.C:
iferr:=c.putToken(context.Background(), sess, tokenUpdateInterval); err!=nil {
c.logger.Errorf("put token error: %s", err)
return
}
ticker.Reset(tokenUpdateInterval-tokenUpdateSpan)
c.logger.Debugf("token updated")
case<-c.done:
return
}
}
}()
returnnil
}
This function will start a new goroutine to putToken to update session token every 50 minutes (tokenUpdateInterval - tokenUpdateSpan). However, defer sess.Close(context.Background()) means that the session will be closed when putTokenContinuously returns. As a result, putToken called in the goroutine will always fail (which will log: put token error: amqp: session closed). I notice that this problem has been fixed in recent commit (though not released yet), but here's another question:
Each time I call the *Client.SendEvent(), it will start a new session and start a goroutine to putToken every 50 minutes until the Client is closed, which means each time I send an event, a new endless goroutine is created to putToken continuously, but all I need is just sending an event, after that, I don't need to maintain this session anymore (because next time I call *Client.SendEvent(), it will start another new session for me). What's more, as I have to call *Client.SendEvent() frequently, it could lead to a large number of goroutine leak.
My solution to this problem is that the *Client.newSession() called by *Client.SendEvent() don't call putTokenContinuously, it just call putToken and return the new session.
The text was updated successfully, but these errors were encountered:
Hi there, recently I found this SDK, and it's awesome! However, when I use
*Client.SendEvent
iniotservice
package, I encountered a weird problem, 50 minutes after calling this function, a log comes out as follows:And I find a issue explaning this: #38 (comment) . As the comment explains,
*Client.SendEvent
will result in calling*Client.putTokenContinuously
:This function will start a new goroutine to
putToken
to update session token every 50 minutes (tokenUpdateInterval
-tokenUpdateSpan
). However,defer sess.Close(context.Background())
means that the session will be closed whenputTokenContinuously
returns. As a result,putToken
called in the goroutine will always fail (which will log:put token error: amqp: session closed
). I notice that this problem has been fixed in recent commit (though not released yet), but here's another question:Each time I call the
*Client.SendEvent()
, it will start a new session and start a goroutine toputToken
every 50 minutes until theClient
is closed, which means each time I send an event, a new endless goroutine is created toputToken
continuously, but all I need is just sending an event, after that, I don't need to maintain this session anymore (because next time I call*Client.SendEvent()
, it will start another new session for me). What's more, as I have to call*Client.SendEvent()
frequently, it could lead to a large number of goroutine leak.My solution to this problem is that the
*Client.newSession()
called by*Client.SendEvent()
don't callputTokenContinuously
, it just callputToken
and return the new session.The text was updated successfully, but these errors were encountered: