Cleanup hub pool periodically
This commit is contained in:
parent
f14ed3a85e
commit
3fc1601f5d
18
hub/hub.go
18
hub/hub.go
|
@ -2,6 +2,7 @@ package hub
|
|||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/KosyanMedia/burlesque/storage"
|
||||
)
|
||||
|
@ -15,10 +16,14 @@ type (
|
|||
)
|
||||
|
||||
func New(st *storage.Storage) *Hub {
|
||||
return &Hub{
|
||||
h := &Hub{
|
||||
storage: st,
|
||||
subscribers: []*Subscription{},
|
||||
}
|
||||
|
||||
go h.cleanupPeriodically()
|
||||
|
||||
return h
|
||||
}
|
||||
|
||||
func (h *Hub) Pub(queue string, msg []byte) bool {
|
||||
|
@ -52,6 +57,17 @@ func (h *Hub) Sub(s *Subscription) {
|
|||
h.subscribers = append(h.subscribers, s)
|
||||
}
|
||||
|
||||
func (h *Hub) cleanupPeriodically() {
|
||||
t := time.NewTicker(1 * time.Second)
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-t.C:
|
||||
h.cleanup()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Hub) cleanup() {
|
||||
h.lock.Lock()
|
||||
defer h.lock.Unlock()
|
||||
|
|
Loading…
Reference in New Issue