This repository has been archived by the owner on Mar 29, 2022. It is now read-only.
/
exchange.go
145 lines (121 loc) · 2.83 KB
/
exchange.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package server
import (
"fmt"
"sync"
)
type Exchange interface {
route(route string, d *Delivery) error
addBinding(route string, b *BindingsMap)
delBinding(route string)
}
type BindingsMap struct {
queue *Queue
headers map[string]string
}
type TopicExchange struct {
name string
bindings map[string]*BindingsMap
mu *sync.RWMutex
}
func NewTopicExchange(name string) *TopicExchange {
return &TopicExchange{
name: name,
bindings: make(map[string]*BindingsMap),
mu: &sync.RWMutex{},
}
}
func (t *TopicExchange) addBinding(route string, b *BindingsMap) {
t.mu.Lock()
defer t.mu.Unlock()
t.bindings[route] = b
}
func (t *TopicExchange) delBinding(route string) {
t.mu.Lock()
defer t.mu.Unlock()
delete(t.bindings, route)
}
func (t *TopicExchange) route(route string, d *Delivery) error {
t.mu.RLock()
defer t.mu.RUnlock()
for bname, bindings := range t.bindings {
if topicMatch(bname, route) {
bindings.queue.data <- d
return nil
}
}
// The route doesnt match any binding, then will be discarded
return nil
}
type DirectExchange struct {
name string
bindings map[string]*BindingsMap
mu *sync.RWMutex
}
func NewDirectExchange(name string) *DirectExchange {
return &DirectExchange{
name: name,
bindings: make(map[string]*BindingsMap),
mu: &sync.RWMutex{},
}
}
func (d *DirectExchange) addBinding(route string, b *BindingsMap) {
d.mu.Lock()
defer d.mu.Unlock()
if d.bindings == nil {
d.bindings = make(map[string]*BindingsMap)
}
d.bindings[route] = b
}
func (d *DirectExchange) delBinding(route string) {
d.mu.Lock()
defer d.mu.Unlock()
delete(d.bindings, route)
}
func (d *DirectExchange) route(route string, delivery *Delivery) error {
d.mu.RLock()
defer d.mu.RUnlock()
if bindings, ok := d.bindings[route]; ok {
bindings.queue.data <- delivery
return nil
}
return fmt.Errorf("no bindings to route: %s", route)
}
type HeadersExchange struct {
name string
bindings map[string]*BindingsMap
mu *sync.RWMutex
}
func NewHeadersExchange(name string) *HeadersExchange {
return &HeadersExchange{
name: name,
bindings: make(map[string]*BindingsMap),
mu: &sync.RWMutex{},
}
}
func (t *HeadersExchange) addBinding(route string, b *BindingsMap) {
t.mu.Lock()
defer t.mu.Unlock()
bindingKey := route
if bindingKey == "" {
bindingKey = b.queue.name
}
t.bindings[bindingKey] = b
}
func (t *HeadersExchange) delBinding(route string) {
t.mu.Lock()
defer t.mu.Unlock()
delete(t.bindings, route)
}
func (t *HeadersExchange) route(route string, d *Delivery) error {
t.mu.RLock()
defer t.mu.RUnlock()
for _, bindings := range t.bindings {
if match, err := headersMatch(bindings, d); match {
bindings.queue.data <- d
} else if err != nil {
return err
}
}
// The headers doesnt match any attribute, then will be discarded
return nil
}