1
0
Fork 0

Cleanup hub pool periodically

This commit is contained in:
Gregory Eremin 2014-09-11 15:11:48 +04:00
parent f14ed3a85e
commit 3fc1601f5d
1 changed files with 17 additions and 1 deletions

View File

@ -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()