60 lines
1.2 KiB
Go
60 lines
1.2 KiB
Go
package session
|
|
|
|
import (
|
|
"log"
|
|
"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
|
|
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)
|
|
subscription.Subscriptions.RemoveSubsForChannel(s.SubscriptionChannel)
|
|
s.RemoveSessionChannel <- *s.ClientID
|
|
}
|
|
|
|
// newTime is nullable
|
|
func (e *Expiry) SetExpireTimerDuration(newTime *uint32) {
|
|
expiry := uint32(0)
|
|
if newTime != nil {
|
|
expiry = *newTime
|
|
} else {
|
|
expiry = uint32(0)
|
|
}
|
|
e.ExpiryInterval = time.Second * time.Duration(expiry)
|
|
}
|
|
|
|
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
|
|
}
|
|
}
|