-
Notifications
You must be signed in to change notification settings - Fork 0
/
serv.go
35 lines (31 loc) · 836 Bytes
/
serv.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
package hub
// A service is a common interface for the last message processor in line.
// It usually is used by wrappers, that handles request parsing and delegate.
type Service interface {
// Serve handles the message and returns the response or nil, or an error.
Serve(*Msg) (*Msg, error)
}
// Services maps message subjects to services.
type Services map[string]Service
// Handle calls the service with m's subject or returns an error.
// If the service returns data and c is not nil, a reply is sent to the sender.
func (s Services) Handle(m *Msg) bool {
f := s[m.Subj]
if f == nil {
return false
}
res, err := f.Serve(m)
if err != nil {
res = m.ReplyErr(err)
}
if res != nil {
m.From.Chan() <- res
}
return true
}
func (s Services) Merge(o Services) Services {
for k, v := range o {
s[k] = v
}
return s
}