Contextless hub

This commit is contained in:
Gregory Eremin 2014-09-10 16:52:18 +04:00
parent c1d828b222
commit 69b94c077f

View File

@ -1,27 +1,47 @@
package hub package hub
import ( import (
"code.google.com/p/go.net/context"
"github.com/KosyanMedia/burlesque/storage" "github.com/KosyanMedia/burlesque/storage"
) )
type ( type (
Hub struct { Hub struct {
storage *storage.Storage storage *storage.Storage
subscribers []*context.Context subscribers []*Subscription
} }
) )
func New() (h *Hub) { func New(st *storage.Storage) *Hub {
h = Hub{} return &Hub{
storage: st,
return subscribers: []*Subscription{},
}
} }
func (h *Hub) Pub(ctx context.Context) context.Context { func (h *Hub) Pub(queue string, msg []byte) bool {
return ctx for _, s := range h.subscribers {
if s.Queue == queue {
select {
case <-s.Done():
continue
default:
}
if ok := s.Send(msg); ok {
return true
}
}
}
err := h.storage.Put(queue, msg)
return (err == nil)
} }
func (h *Hub) Sub(ctx context.Context) context.Context { func (h *Hub) Sub(s *Subscription) {
return ctx if msg, ok := h.storage.Get(s.Queue); ok {
s.Send(msg)
} else {
h.subscribers = append(h.subscribers, s)
}
} }