21
21
22
22
## Description
23
23
24
- This is a framework to use [ RabbitMQ] as a client/server RPC setup togheter with
25
- the [ Go amqp] implementation. The framework can manage a fully funcitonal
24
+ This is a framework to use [ RabbitMQ] as a client/server RPC setup together with
25
+ the [ Go amqp] implementation. The framework can manage a fully functional
26
26
message queue setup with reconnects, disconnects, graceful shutdown and other
27
27
stability mechanisms. By providing this RabbitMQ can be used as transport and
28
28
service discovery to quickly get up and running with a micro service
@@ -35,7 +35,7 @@ nomenclature is unique for RabbitMQ some prior experience is preferred.
35
35
36
36
## Project status
37
37
38
- This project has been used in production since october 2018 handling millions of
38
+ This project has been used in production since October 2018 handling millions of
39
39
requests both as server and client.
40
40
41
41
## Server
@@ -44,7 +44,7 @@ The server is inspired by the HTTP library where the user maps a [RabbitMQ
44
44
binding] to a handler function. A response writer is passed to the handler which
45
45
may be used as an ` io.Writer ` to write the response.
46
46
47
- This is an an example of how to get up and running with a server responding to
47
+ This is an example of how to get up and running with a server responding to
48
48
all messages published to the given routing key.
49
49
50
50
``` go
67
67
68
68
``` go
69
69
server.Bind (DirectBinding (" routing_key" , handleFunc))
70
- server.Bind (FanoutBinding (" fanout-exchange" , handleFunc))
71
70
server.Bind (TopicBinding (" queue-name" , " routing_key.#" , handleFunc))
72
71
server.Bind (HeadersBinding (" queue-name" , amqp.Table {" x-match" : " all" , " foo" : " bar" }, handleFunc))
73
72
```
@@ -76,14 +75,9 @@ If the default variables doesn't result in the desired result you can setup the
76
75
binding with the type manually.
77
76
78
77
``` go
79
- customBinding := HandlerBinding {
80
- QueueName : " oh-sweet-queue" ,
81
- ExchangeName : " my-exchange" ,
82
- ExchangeType : ExchangeDirect ,
83
- RoutingKey : " my-key" ,
84
- BindHeaders : amqp.Table {},
85
- Handler : handleFunc,
86
- }
78
+ customBinding := CreateBinding (" oh-sweet-queue" , DefaultExchangeNameDirect , handleFunc).
79
+ WithPrefetchCount (100 ).
80
+ WithAutoAck (false )
87
81
88
82
server.Bind (customBinding)
89
83
```
@@ -94,18 +88,13 @@ can be changed by calling chainable methods.
94
88
95
89
``` go
96
90
server := NewServer (" amqp://guest:guest@localhost:5672" ).
97
- WithConsumeSettings (ConsumeSettings{}).
98
- WithQueueDeclareSettings (QueueDeclareSettings{}).
99
- WithExchangeDeclareSettings (ExchangeDeclareSettings{}).
100
91
WithDebugLogger (log.Printf ).
101
92
WithErrorLogger (log.Printf ).
102
- WithDialConfig (amqp.Config {}).
103
93
WithTLS (&tls.Config {})
104
94
```
105
95
106
- QoS is by default set to a prefetch count of ` 10 ` and a prefetch size of ` 0 ` (no
107
- limit). If you want to change this you can use the
108
- ` WithConsumeSettings(settings) ` function.
96
+ QoS is by default set to a prefetch count of ` 10 ` . If you want to change this
97
+ you can modify the binding by setting the ` PrefetchCount ` to something else.
109
98
110
99
## Client
111
100
@@ -142,7 +131,7 @@ methods.
142
131
client := NewClient(" amqp:// guest:guest@localhost:5672").
143
132
WithTimeout (5000 * time.Milliseconds )
144
133
145
- // Will not connect and may be changed untill this call.
134
+ // Will not connect and may be changed until this call.
146
135
client.Send (NewRequest ().WithRoutingKey (" routing_key" ))
147
136
```
148
137
@@ -154,10 +143,8 @@ client := NewClient("amqp://guest:guest@localhost:5672").
154
143
WithErrorLogger (log.Printf ).
155
144
WithDialConfig (amqp.Config {}).
156
145
WithTLS (&tls.Config {}).
157
- WithQueueDeclareSettings (QueueDeclareSettings{}).
158
- WithConsumeSettings (ConsumeSettings{}).
159
- WithPublishSettings (PublishSettings{}).
160
- WithConfirmMode (true ),
146
+ WithReplyToConsumerArgs (amqp.Table {}).
147
+ WithConfirmMode (false ),
161
148
WithTimeout (10 * Time .Second )
162
149
```
163
150
@@ -170,14 +157,12 @@ can read more here](https://www.rabbitmq.com/confirms.html#publisher-confirms)
170
157
171
158
The client is set in confirm mode by default.
172
159
173
- You can use ` WithPublishSettings ` or ` WithConfirmMode ` to control this setting.
160
+ You can use ` WithConfirmMode ` to control this setting. It defaults to ` true ` .
174
161
175
162
``` go
176
163
client := NewClient (" amqp://guest:guest@localhost:5672" ).
177
164
WithConfirmMode (true )
178
165
179
- client := NewClient (" amqp://guest:guest@localhost:5672" ).
180
- WithPublishSettings (true )
181
166
```
182
167
183
168
### Request
@@ -195,8 +180,8 @@ request := NewRequest().
195
180
WithExchange (" custom.exchange" ).
196
181
WithRoutingKey (" routing_key" ).
197
182
WithHeaders (amqp.Headers {}).
198
- WithCorrelationID (" custom-correlation-id" ).
199
183
WithTimeout (5 * time.Second ).
184
+ WithMandatory (true ).
200
185
WithResponse (true )
201
186
```
202
187
@@ -219,10 +204,6 @@ if err != nil {
219
204
}
220
205
```
221
206
222
- ** Note** : If you request a response when sending to a fanout exchange the
223
- response will be the first one responded from any of the subscribers. There's
224
- currently no way to stream multiple responses for the same request.
225
-
226
207
### Sender
227
208
228
209
The client invokes a default ` SendFunc ` while calling ` Send() ` where all the
@@ -352,7 +333,7 @@ func myMiddleware(next SendFunc) SendFunc {
352
333
client := NewClient (" amqp://guest:guest@localhost:5672" ).
353
334
AddMiddleware (myMiddleware)
354
335
355
- // Add the middleware to a singlerequest
336
+ // Add the middleware to a single request
356
337
reuqest := NewRequest ().
357
338
WithRoutingKey (" routing_key" ).
358
339
AddMiddleware (myMiddleware)
@@ -371,38 +352,25 @@ For more examples of client middlewares, see [examples/middleware].
371
352
You often want to know when a connection has been established and when it comes
372
353
to RabbitMQ also perform some post connection setup. This is enabled by the fact
373
354
that both the server and the client holds a list of ` OnStarted ` . The function
374
- receives the incomming connection, outgoing connection, incomming channel and
355
+ receives the incoming connection, outgoing connection, incoming channel and
375
356
outgoing channel.
376
357
377
358
``` go
378
359
type OnStartedFunc func (inputConn, outputConn *amqp.Connection , inputChannel, outputChannel *amqp.Channel )
379
360
```
380
361
381
- As an example this is a great place to do some initial QoS setup.
382
-
383
362
``` go
384
- server := NewServer (" amqp://guest:guest@localhost:5672" )
385
-
386
- setupQoS (_, _ *amqp.Connection , inChan, _ *amqp.Channel ) {
387
- err := inChan.Qos (
388
- 10 , // Prefetch count
389
- 1024 , // Prefetch size
390
- true , // Global
391
- )
392
-
393
- if err != nil {
394
- panic (err.Error ())
395
- }
363
+ server := NewServer (" amqp://guest:guest@localhost:5672" ).
364
+ OnStarted (func (inConn, outConn *amqp.Connection , inChan, outChan *amqp.Channel ) {
365
+ // Do something after connection here...
366
+ })
396
367
}
397
368
398
- // Setup QoS when the connection is established.
399
- server.OnStarted (setupQoS)
400
-
401
369
server.ListenAndServe ()
402
370
```
403
371
404
372
Both the server and the client follow the recommendations for [ RabbitMQ
405
- connections] which means separate connections for incomming and outgoing traffic
373
+ connections] which means separate connections for incoming and outgoing traffic
406
374
and separate channels for consuming and publishing messages. Because of this the
407
375
signature looks the same way for both the server and the client.
408
376
@@ -431,7 +399,7 @@ server.ListenAndServe()
431
399
432
400
## Logging
433
401
434
- You can specifiy two optional loggers for debugging and errors or unexpected
402
+ You can specify two optional loggers for debugging and errors or unexpected
435
403
behaviour. By default only error logging is turned on and is logged via the log
436
404
package's standard logging.
437
405
0 commit comments