Contextless hub
This commit is contained in:
		
							parent
							
								
									c1d828b222
								
							
						
					
					
						commit
						69b94c077f
					
				
							
								
								
									
										40
									
								
								hub/hub.go
									
									
									
									
									
								
							
							
						
						
									
										40
									
								
								hub/hub.go
									
									
									
									
									
								
							| @ -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: | ||||||
| 			} | 			} | ||||||
| 
 | 
 | ||||||
| func (h *Hub) Sub(ctx context.Context) context.Context { | 			if ok := s.Send(msg); ok { | ||||||
| 	return ctx | 				return true | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	err := h.storage.Put(queue, msg) | ||||||
|  | 
 | ||||||
|  | 	return (err == nil) | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | 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) | ||||||
|  | 	} | ||||||
| } | } | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user