maeqtt/session/expiry.go

61 lines
1.2 KiB
Go
Raw Permalink Normal View History

2021-10-16 23:38:23 +02:00
package session
import (
"log"
2021-10-16 23:38:23 +02:00
"time"
"badat.dev/maeqtt/v2/subscription"
)
type Expiry struct {
ExpiryInterval time.Duration
expireTimer *time.Timer
2021-10-16 23:38:23 +02:00
RemoveSessionChannel
}
func NewExpiry(channel RemoveSessionChannel) Expiry {
expiry := Expiry{}
2021-10-16 23:38:23 +02:00
expiry.RemoveSessionChannel = channel
return expiry
}
// Channel for removing a session from the global state
type RemoveSessionChannel chan string
func (s *Session) expireSession() {
log.Printf("Session: %v expired", *s.ClientID)
2021-10-16 23:38:23 +02:00
subscription.Subscriptions.RemoveSubsForChannel(s.SubscriptionChannel)
s.RemoveSessionChannel <- *s.ClientID
}
// newTime is nullable
func (e *Expiry) SetExpireTimerDuration(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)
}
e.ExpiryInterval = time.Second * time.Duration(expiry)
2021-10-16 23:38:23 +02:00
}
func (e *Expiry) startExpireTimer() {
e.stopExpireTimer()
e.expireTimer = time.NewTimer(e.ExpiryInterval)
}
func (e *Expiry) stopExpireTimer() {
if e.expireTimer != nil {
e.expireTimer.Stop()
}
}
func (e *Expiry) expiryChannel() <- chan time.Time {
if e.expireTimer != nil {
return e.expireTimer.C
} else {
return nil
}
2021-10-16 23:38:23 +02:00
}