50 lines
1,018 B
Go
50 lines
1,018 B
Go
|
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) {
|
||
|
var expiry = uint32(0)
|
||
|
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)
|
||
|
}
|
||
|
}
|