-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathfeature_client.go
191 lines (147 loc) · 3.31 KB
/
feature_client.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package vega
import (
"fmt"
"strings"
"sync"
"time"
)
func Dial(addr string) (*FeatureClient, error) {
client, err := NewClient(addr)
if err != nil {
return nil, err
}
return &FeatureClient{
Client: client,
}, nil
}
func Local() (*FeatureClient, error) {
client, err := NewInsecureClient(fmt.Sprintf("127.0.0.1:%d", DefaultPort))
if err != nil {
return nil, err
}
return &FeatureClient{
Client: client,
}, nil
}
// Create a new FeatureClient wrapping a explicit Client
func NewFeatureClient(c *Client) *FeatureClient {
return &FeatureClient{Client: c}
}
type Handler interface {
HandleMessage(*Message) *Message
}
type wrappedHandlerFunc struct {
f func(*Message) *Message
}
func (w *wrappedHandlerFunc) HandleMessage(m *Message) *Message {
return w.f(m)
}
func HandlerFunc(h func(*Message) *Message) Handler {
return &wrappedHandlerFunc{h}
}
// type Handler func(*Message) *Message
// Wraps Client to provide highlevel behaviors that build on the basics
// of the distributed mailboxes. Should only be used by one goroutine
// at a time.
type FeatureClient struct {
*Client
localMailbox string
lock sync.Mutex
}
// Create a new FeatureClient that wraps the same Client as
// this one. Useful for creating a new instance to use in a new
// goroutine
func (fc *FeatureClient) Clone() *FeatureClient {
return &FeatureClient{Client: fc.Client}
}
// Return the name of a ephemeral mailbox only for this instance
func (fc *FeatureClient) LocalMailbox() string {
fc.lock.Lock()
defer fc.lock.Unlock()
if fc.localMailbox != "" {
return fc.localMailbox
}
r := RandomMailbox()
err := fc.EphemeralDeclare(r)
if err != nil {
panic(err)
}
fc.localMailbox = r
return r
}
const cEphemeral = "#ephemeral"
func (fc *FeatureClient) Declare(name string) error {
if strings.HasSuffix(name, cEphemeral) {
return fc.Client.EphemeralDeclare(name)
}
return fc.Client.Declare(name)
}
func (fc *FeatureClient) HandleRequests(name string, h Handler) error {
for {
del, err := fc.LongPoll(name, 1*time.Minute)
if err != nil {
return err
}
if del == nil {
continue
}
msg := del.Message
ret := h.HandleMessage(msg)
del.Ack()
fc.Push(msg.ReplyTo, ret)
}
}
func (fc *FeatureClient) Request(name string, msg *Message) (*Delivery, error) {
msg.ReplyTo = fc.LocalMailbox()
err := fc.Push(name, msg)
if err != nil {
return nil, err
}
for {
resp, err := fc.LongPoll(msg.ReplyTo, 1*time.Minute)
if err != nil {
return nil, err
}
if resp == nil {
continue
}
return resp, nil
}
}
type Receiver struct {
// channel that messages are sent to
Channel <-chan *Delivery
// Any error detected while receiving
Error error
shutdown chan struct{}
}
func (rec *Receiver) Close() error {
close(rec.shutdown)
return nil
}
func (fc *FeatureClient) Receive(name string) *Receiver {
c := make(chan *Delivery)
rec := &Receiver{c, nil, make(chan struct{})}
go func() {
for {
select {
case <-rec.shutdown:
close(c)
return
default:
// We don't cancel this action if Receive is told to Close. Instead
// we let it timeout and then detect the shutdown request and exit.
msg, err := fc.Client.LongPoll(name, 1*time.Minute)
if err != nil {
close(c)
return
}
if msg == nil {
continue
}
c <- msg
}
}
}()
return rec
}