1
0
Fork 0

Contextless hub

This commit is contained in:
Gregory Eremin 2014-09-10 16:52:18 +04:00
parent c1d828b222
commit 69b94c077f
1 changed files with 30 additions and 10 deletions

View File

@ -1,27 +1,47 @@
package hub
import (
"code.google.com/p/go.net/context"
"github.com/KosyanMedia/burlesque/storage"
)
type (
Hub struct {
storage *storage.Storage
subscribers []*context.Context
subscribers []*Subscription
}
)
func New() (h *Hub) {
h = Hub{}
return
func New(st *storage.Storage) *Hub {
return &Hub{
storage: st,
subscribers: []*Subscription{},
}
}
func (h *Hub) Pub(ctx context.Context) context.Context {
return ctx
func (h *Hub) Pub(queue string, msg []byte) bool {
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 {
return ctx
func (h *Hub) Sub(s *Subscription) {
if msg, ok := h.storage.Get(s.Queue); ok {
s.Send(msg)
} else {
h.subscribers = append(h.subscribers, s)
}
}