This repository has been archived by the owner on Nov 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
subscriberRegistry.go
61 lines (49 loc) · 1.63 KB
/
subscriberRegistry.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
54
55
56
57
58
59
60
61
package franklin
import (
"fmt"
"reflect"
)
// SubscriberRegistry blabla
type SubscriberRegistry struct {
handlers map[string]MessageHandler
queues map[string]string
keys map[reflect.Type]string
}
// Consumers returns the subscriber registrations
func (r *SubscriberRegistry) Consumers() map[string]MessageHandler {
return r.handlers
}
// KeyForMessage returns the key for a message
func (r *SubscriberRegistry) KeyForMessage(message Message) string {
messageType := reflect.TypeOf(message)
return r.keys[messageType]
}
// QueueForKey returns the queue for a key
func (r *SubscriberRegistry) QueueForKey(key string) string {
return r.queues[key]
}
// Register adds a handler registration to the subscriber registry
func (r *SubscriberRegistry) Register(key string, queue string, handler MessageHandler) error {
if _, exists := r.keys[handler.MessageType()]; exists {
return fmt.Errorf("Registration for key %s already exists", key)
}
r.handlers[key] = handler
r.queues[key] = queue
r.keys[handler.MessageType()] = key
return nil
}
// RegisterExternal adds an external handler registration to the subscriber registry
func (r *SubscriberRegistry) RegisterExternal(key string, messageType reflect.Type) error {
if _, exists := r.keys[messageType]; exists {
return fmt.Errorf("Registration for key %s already exists", key)
}
r.keys[messageType] = key
return nil
}
// NewSubscriberRegistry creates an empty subscriber registry
func NewSubscriberRegistry() *SubscriberRegistry {
return &SubscriberRegistry{
handlers: make(map[string]MessageHandler),
queues: make(map[string]string),
keys: make(map[reflect.Type]string)}
}