Cleanup hub pool periodically

This commit is contained in:
Gregory Eremin 2014-09-11 15:11:48 +04:00
parent f14ed3a85e
commit 3fc1601f5d

View File

@ -2,6 +2,7 @@ package hub
import ( import (
"sync" "sync"
"time"
"github.com/KosyanMedia/burlesque/storage" "github.com/KosyanMedia/burlesque/storage"
) )
@ -15,10 +16,14 @@ type (
) )
func New(st *storage.Storage) *Hub { func New(st *storage.Storage) *Hub {
return &Hub{ h := &Hub{
storage: st, storage: st,
subscribers: []*Subscription{}, subscribers: []*Subscription{},
} }
go h.cleanupPeriodically()
return h
} }
func (h *Hub) Pub(queue string, msg []byte) bool { 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) 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() { func (h *Hub) cleanup() {
h.lock.Lock() h.lock.Lock()
defer h.lock.Unlock() defer h.lock.Unlock()