Debian Codegraph @

amqptest/server/exchange.go

raw
1 package server
2
3 import (
4 "fmt"
5 "sync"
6 )
7
8 type Exchange interface {
9 route(route string, d *Delivery) error
10 addBinding(route string, b *BindingsMap)
11 delBinding(route string)
12 }
13
14 type BindingsMap struct {
15 queue *Queue
16 headers map[string]string
17 }
18
19 type TopicExchange struct {
20 name string
21 bindings map[string]*BindingsMap
22 mu *sync.RWMutex
23 }
24
25 func NewTopicExchange(name string) *TopicExchange {
26 return &TopicExchange{
27 name: name,
28 bindings: make(map[string]*BindingsMap),
29 mu: &sync.RWMutex{},
30 }
31 }
32
33 func (t *TopicExchange) addBinding(route string, b *BindingsMap) {
34 t.mu.Lock()
35 defer t.mu.Unlock()
36 t.bindings[route] = b
37 }
38
39 func (t *TopicExchange) delBinding(route string) {
40 t.mu.Lock()
41 defer t.mu.Unlock()
42 delete(t.bindings, route)
43 }
44
45 func (t *TopicExchange) route(route string, d *Delivery) error {
46 t.mu.RLock()
47 defer t.mu.RUnlock()
48 for bname, bindings := range t.bindings {
49 if topicMatch(bname, route) {
50 bindings.queue.data <- d
51 return nil
52 }
53 }
54
55 // The route doesnt match any binding, then will be discarded
56 return nil
57 }
58
59 type DirectExchange struct {
60 name string
61 bindings map[string]*BindingsMap
62 mu *sync.RWMutex
63 }
64
65 func NewDirectExchange(name string) *DirectExchange {
66 return &DirectExchange{
67 name: name,
68 bindings: make(map[string]*BindingsMap),
69 mu: &sync.RWMutex{},
70 }
71 }
72
73 func (d *DirectExchange) addBinding(route string, b *BindingsMap) {
74 d.mu.Lock()
75 defer d.mu.Unlock()
76 if d.bindings == nil {
77 d.bindings = make(map[string]*BindingsMap)
78 }
79
80 d.bindings[route] = b
81 }
82
83 func (d *DirectExchange) delBinding(route string) {
84 d.mu.Lock()
85 defer d.mu.Unlock()
86 delete(d.bindings, route)
87 }
88
89 func (d *DirectExchange) route(route string, delivery *Delivery) error {
90 d.mu.RLock()
91 defer d.mu.RUnlock()
92 if bindings, ok := d.bindings[route]; ok {
93 bindings.queue.data <- delivery
94 return nil
95 }
96
97 return fmt.Errorf("no bindings to route: %s", route)
98
99 }
100
101 type HeadersExchange struct {
102 name string
103 bindings map[string]*BindingsMap
104 mu *sync.RWMutex
105 }
106
107 func NewHeadersExchange(name string) *HeadersExchange {
108 return &HeadersExchange{
109 name: name,
110 bindings: make(map[string]*BindingsMap),
111 mu: &sync.RWMutex{},
112 }
113 }
114
115 func (t *HeadersExchange) addBinding(route string, b *BindingsMap) {
116 t.mu.Lock()
117 defer t.mu.Unlock()
118 bindingKey := route
119 if bindingKey == "" {
120 bindingKey = b.queue.name
121 }
122
123 t.bindings[bindingKey] = b
124 }
125
126 func (t *HeadersExchange) delBinding(route string) {
127 t.mu.Lock()
128 defer t.mu.Unlock()
129 delete(t.bindings, route)
130 }
131
132 func (t *HeadersExchange) route(route string, d *Delivery) error {
133 t.mu.RLock()
134 defer t.mu.RUnlock()
135 for _, bindings := range t.bindings {
136 if match, err := headersMatch(bindings, d); match {
137 bindings.queue.data <- d
138 } else if err != nil {
139 return err
140 }
141 }
142
143 // The headers doesnt match any attribute, then will be discarded
144 return nil
145 }