-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathrouter.go
53 lines (40 loc) · 973 Bytes
/
router.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package vega
type MemRouteTable map[string]Pusher
func (ht MemRouteTable) Set(name string, st Pusher) error {
ht[name] = st
return nil
}
func (ht MemRouteTable) Remove(name string) error {
delete(ht, name)
return nil
}
func (ht MemRouteTable) Get(name string) (Pusher, bool) {
s, ok := ht[name]
return s, ok
}
type Router struct {
routes RouteTable
}
func NewRouter(rt RouteTable) *Router {
return &Router{rt}
}
func MemRouter() *Router {
return NewRouter(make(MemRouteTable))
}
func (r *Router) Add(name string, reg Pusher) {
r.routes.Set(name, reg)
}
func (r *Router) Remove(name string) error {
return r.routes.Remove(name)
}
func (r *Router) DiscoverEndpoint(name string) (Pusher, bool) {
reg, ok := r.routes.Get(name)
return reg, ok
}
func (r *Router) Push(name string, body *Message) error {
if storage, ok := r.routes.Get(name); ok {
debugf("Routing %s to %#v\n", name, storage)
return storage.Push(name, body)
}
return ENoMailbox
}