2021-10-16 23:38:23 +02:00
|
|
|
package session
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"badat.dev/maeqtt/v2/subscription"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Expiry struct {
|
|
|
|
ExpiryInterval time.Duration
|
|
|
|
expireTimer time.Timer
|
|
|
|
RemoveSessionChannel
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewExpiry(channel RemoveSessionChannel) Expiry {
|
|
|
|
expiry := Expiry {}
|
|
|
|
|
|
|
|
expiry.RemoveSessionChannel = channel
|
|
|
|
expiry.expireTimer = *time.NewTimer(time.Hour*9999)
|
|
|
|
expiry.expireTimer.Stop()
|
|
|
|
return expiry
|
|
|
|
}
|
|
|
|
|
|
|
|
// Channel for removing a session from the global state
|
|
|
|
type RemoveSessionChannel chan string
|
|
|
|
|
|
|
|
func (s *Session) expireSession() {
|
|
|
|
subscription.Subscriptions.RemoveSubsForChannel(s.SubscriptionChannel)
|
|
|
|
s.RemoveSessionChannel <- *s.ClientID
|
|
|
|
}
|
|
|
|
|
|
|
|
// newTime is nullable
|
|
|
|
func (s *Session) SetExpireTimer(newTime *uint32) {
|
2021-10-17 20:58:16 +02:00
|
|
|
expiry := uint32(0)
|
2021-10-16 23:38:23 +02:00
|
|
|
if newTime != nil {
|
|
|
|
expiry = *newTime
|
|
|
|
} else {
|
|
|
|
expiry = uint32(0)
|
|
|
|
}
|
|
|
|
s.ExpiryInterval = time.Duration(expiry) * time.Second
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Session) resetExpireTimer() {
|
|
|
|
if s.ExpiryInterval == 0 {
|
|
|
|
s.expireTimer.Stop()
|
|
|
|
} else {
|
|
|
|
s.expireTimer.Reset(s.ExpiryInterval)
|
|
|
|
}
|
|
|
|
}
|