-
Notifications
You must be signed in to change notification settings - Fork 21
/
protocol.jl
1431 lines (1225 loc) · 57.8 KB
/
protocol.jl
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# default client timeout to use with blocking methods after which they throw an error
# Julia Timer converts seconds to milliseconds and adds 1 to it before passing it to libuv, hence the magic numbers to prevent overflow
const DEFAULT_TIMEOUT = round(Int, typemax(Int)/1000) - 1
const DEFAULT_CONNECT_TIMEOUT = round(Int, typemax(Int)/1000) - 1
# ----------------------------------------
# IO for types begin
# ----------------------------------------
function read(io::IO, ::Type{TAMQPBit})
TAMQPBit(ntoh(read(io, UInt8)))
end
function write(io::IO, b::TAMQPBit)
write(io, hton(b.val))
end
function read(io::IO, ::Type{TAMQPFrameProperties})
TAMQPFrameProperties(
ntoh(read(io, fieldtype(TAMQPFrameProperties, :channel))),
ntoh(read(io, fieldtype(TAMQPFrameProperties, :payloadsize))),
)
end
write(io::IO, p::TAMQPFrameProperties) = write(io, hton(p.channel), hton(p.payloadsize))
function read!(io::IO, b::TAMQPBodyPayload)
read!(io, b.data)
b
end
write(io::IO, b::TAMQPBodyPayload) = write(io, b.data)
function read(io::IO, ::Type{TAMQPShortStr})
len = ntoh(read(io, TAMQPOctet))
TAMQPShortStr(len, read!(io, Vector{UInt8}(undef, len)))
end
function read(io::IO, ::Type{TAMQPLongStr})
len = ntoh(read(io, TAMQPLongUInt))
TAMQPLongStr(len, read!(io, Vector{UInt8}(undef, len)))
end
function read(io::IO, ::Type{TAMQPByteArray})
len = ntoh(read(io, TAMQPLongUInt))
TAMQPByteArray(len, read!(io, Vector{UInt8}(undef, len)))
end
write(io::IO, s::T) where {T<:Union{TAMQPShortStr,TAMQPLongStr,TAMQPByteArray}} = write(io, hton(s.len), s.data)
function read(io::IO, ::Type{TAMQPFieldValue})
c = read(io, Char)
v = read(io, FieldValueIndicatorMap[c])
T = FieldValueIndicatorMap[c]
if T <: Integer
v = ntoh(v)
end
TAMQPFieldValue{T}(c, v)
end
function write(io::IO, fv::TAMQPFieldValue)
v = isa(fv.fld, Integer) ? hton(fv.fld) : fv.fld
write(io, fv.typ, v)
end
read(io::IO, ::Type{TAMQPFieldValuePair}) = TAMQPFieldValuePair(read(io, TAMQPFieldName), read(io, TAMQPFieldValue))
write(io::IO, fv::TAMQPFieldValuePair) = write(io, fv.name, fv.val)
function read(io::IO, ::Type{TAMQPFieldTable})
len = ntoh(read(io, fieldtype(TAMQPFieldTable, :len)))
@debug("read fieldtable", len)
buff = read!(io, Vector{UInt8}(undef, len))
data = TAMQPFieldValuePair[]
iob = IOBuffer(buff)
while !eof(iob)
push!(data, read(iob, TAMQPFieldValuePair))
end
TAMQPFieldTable(len, data)
end
function write(io::IO, ft::TAMQPFieldTable)
@debug("write fieldtable", nfields=length(ft.data))
iob = IOBuffer()
for fv in ft.data
write(iob, fv)
end
buff = take!(iob)
len = TAMQPLongUInt(length(buff))
@debug("write fieldtable", len)
l = write(io, hton(len))
if len > 0
l += write(io, buff)
end
l
end
"""
Read a generic frame. All frames have the following wire format:
0 1 3 7 size+7 size+8
+------+---------+---------+ +-------------+ +-----------+
| type | channel | size | | payload | | frame-end |
+------+---------+---------+ +-------------+ +-----------+
octet short long 'size' octets octet
"""
function read(io::IO, ::Type{TAMQPGenericFrame})
hdr = ntoh(read(io, fieldtype(TAMQPGenericFrame, :hdr)))
@assert hdr in (1,2,3,8)
props = read(io, fieldtype(TAMQPGenericFrame, :props))
@debug("reading generic frame", type=hdr, channel=props.channel, payloadsize=props.payloadsize)
payload = read!(io, TAMQPBodyPayload(Vector{TAMQPOctet}(undef, props.payloadsize)))
fend = ntoh(read(io, fieldtype(TAMQPGenericFrame, :fend)))
@assert fend == FrameEnd
TAMQPGenericFrame(hdr, props, payload, fend)
end
write(io::IO, f::TAMQPGenericFrame) = write(io, hton(f.hdr), f.props, f.payload, f.fend)
# """
# Given a generic frame, convert it to appropriate exact frame type.
# """
#function narrow_frame(f::TAMQPGenericFrame)
# if f.hdr == FrameMethod
# return TAMQPMethodFrame(f)
# end
# throw(AMQPProtocolException("Unknown frame type $(f.hdr)"))
#end
function method_name(payload::TAMQPMethodPayload)
c = CLASS_MAP[payload.class]
m = c.method_map[payload.method]
#(c.name, m.name)
string(c.name) * "." * string(m.name)
end
"""
Validate if the method frame is for the given class and method.
"""
function is_method(m::TAMQPMethodFrame, class::Symbol, method::Symbol)
c = CLASS_MAP[m.payload.class]
if c.name === class
m = c.method_map[m.payload.method]
return m.name === method
end
false
end
function method_key(classname::Symbol, methodname::Symbol)
class = CLASSNAME_MAP[classname]
method = CLASSMETHODNAME_MAP[classname,methodname]
(FrameMethod, class.id, method.id)
end
frame_key(frame_type) = (UInt8(frame_type),)
# ----------------------------------------
# IO for types end
# ----------------------------------------
# ----------------------------------------
# Connection and Channel begin
# ----------------------------------------
const UNUSED_CHANNEL = -1
const DEFAULT_CHANNEL = 0
const DEFAULT_CHANNELMAX = 256
const DEFAULT_AUTH_PARAMS = Dict{String,Any}("MECHANISM"=>"AMQPLAIN", "LOGIN"=>"guest", "PASSWORD"=>"guest")
const CONN_STATE_CLOSED = 0
const CONN_STATE_OPENING = 1
const CONN_STATE_OPEN = 2
const CONN_STATE_CLOSING = 3
const CONN_MAX_QUEUED = 1024 #typemax(Int)
const DEFAULT_KEEPALIVE_SECS = 60
abstract type AbstractChannel end
function keepalive!(sock, enable::Bool; interval::Integer=DEFAULT_KEEPALIVE_SECS)
@debug("setting tcp keepalive on tcp socket", enable, interval)
err = ccall(:uv_tcp_keepalive, Cint, (Ptr{Nothing}, Cint, Cuint), sock.handle, enable, interval)
if err != 0
throw(AMQPProtocolException("error setting keepalive on socket to $enable with interval $interval"))
end
return sock
end
mutable struct Connection
virtualhost::String
host::String
port::Int
sock::Union{TCPSocket, BufferedTLSSocket, Nothing}
properties::Dict{Symbol,Any}
capabilities::Dict{String,Any}
channelmax::TAMQPShortInt
framemax::TAMQPLongInt
heartbeat::TAMQPShortInt
enable_heartbeat::Bool
keepalive::Integer
enable_keepalive::Bool
state::UInt8
sendq::Channel{TAMQPGenericFrame}
sendlck::Channel{UInt8}
channels::Dict{TAMQPChannel, AbstractChannel}
sender::Union{Task, Nothing}
receiver::Union{Task, Nothing}
heartbeater::Union{Task, Nothing}
heartbeat_time_server::Float64
heartbeat_time_client::Float64
function Connection(;
virtualhost::String="/",
host::String="localhost",
port::Int=AMQP_DEFAULT_PORT,
send_queue_size::Int=CONN_MAX_QUEUED,
heartbeat::Integer=0,
enable_heartbeat::Bool=true,
keepalive::Integer=DEFAULT_KEEPALIVE_SECS,
enable_keepalive::Bool=true,
)
sendq = Channel{TAMQPGenericFrame}(send_queue_size)
sendlck = Channel{UInt8}(1)
put!(sendlck, 1)
new(virtualhost, host, port, nothing,
Dict{Symbol,Any}(), Dict{String,Any}(), 0, 0,
heartbeat, enable_heartbeat, keepalive, enable_keepalive,
CONN_STATE_CLOSED, sendq, sendlck, Dict{TAMQPChannel, AbstractChannel}(),
nothing, nothing, nothing,
0.0, 0.0)
end
end
mutable struct MessageConsumer
chan_id::TAMQPChannel
consumer_tag::String
recvq::Channel{Message}
callback::Function
receiver::Task
function MessageConsumer(chan_id::TAMQPChannel, consumer_tag::String, callback::Function;
buffer_size::Int=typemax(Int),
buffer::Channel{Message}=Channel{Message}(buffer_size))
c = new(chan_id, consumer_tag, buffer, callback)
c.receiver = @async connection_processor(c, "Consumer $consumer_tag", channel_message_consumer)
c
end
end
close(consumer::MessageConsumer) = close(consumer.recvq)
mutable struct MessageChannel <: AbstractChannel
id::TAMQPChannel
conn::Connection
state::UInt8
flow::Bool
recvq::Channel{TAMQPGenericFrame}
receiver::Union{Task, Nothing}
callbacks::Dict{Tuple,Tuple{Function,Any}}
partial_msgs::Vector{Message} # holds partial messages while they are getting read (message bodies arrive in sequence)
chan_get::Channel{Union{Message, Nothing}} # channel used for received messages, in sync get call (TODO: maybe type more strongly?)
consumers::Dict{String,MessageConsumer}
pending_msgs::Dict{String,Channel{Message}} # holds messages received that do not have a consumer registered
lck::ReentrantLock
closereason::Union{CloseReason, Nothing}
function MessageChannel(id, conn)
new(id, conn, CONN_STATE_CLOSED, true,
Channel{TAMQPGenericFrame}(CONN_MAX_QUEUED), nothing, Dict{Tuple,Tuple{Function,Any}}(),
Message[], Channel{Union{Message, Nothing}}(1), Dict{String,MessageConsumer}(),
Dict{String,Channel{Message}}(), ReentrantLock(), nothing)
end
end
flush(c::MessageChannel) = flush(c.conn)
function flush(c::Connection)
while isready(c.sendq) && (c.sender !== nothing) && !istaskdone(c.sender)
yield()
end
end
sock(c::MessageChannel) = sock(c.conn)
sock(c::Connection) = c.sock
isopen(c::Connection) = c.sock !== nothing && isopen(c.sock)
isopen(c::MessageChannel) = isopen(c.conn) && (c.id in keys(c.conn.channels))
get_property(c::MessageChannel, s::Symbol, default) = get_property(c.conn, s, default)
get_property(c::Connection, s::Symbol, default) = get(c.properties, s, default)
with_sendlock(f, c::MessageChannel) = with_sendlock(f, c.conn)
with_sendlock(f, c::Connection) = with_sendlock(f, c.sendlck)
function with_sendlock(f, sendlck::Channel{UInt8})
lck = take!(sendlck)
try
f()
finally
put!(sendlck, lck)
end
end
send(c::MessageChannel, f) = send(c.conn, f)
send(c::Connection, f) = put!(c.sendq, TAMQPGenericFrame(f))
function send(c::MessageChannel, payload::TAMQPMethodPayload)
@debug("sending without content", methodname=method_name(payload))
frameprop = TAMQPFrameProperties(c.id,0)
send(c, TAMQPMethodFrame(frameprop, payload))
end
function send(c::MessageChannel, payload::TAMQPMethodPayload, msg::Message)
@debug("sending with content", methodname=method_name(payload))
frameprop = TAMQPFrameProperties(c.id,0)
framemax = c.conn.framemax
if framemax <= 0
errormsg = (c.conn.state == CONN_STATE_OPEN) ? "Unexpected framemax ($framemax) value for connection" : "Connection closed"
throw(AMQPClientException(errormsg))
end
with_sendlock(c) do
send(c, TAMQPMethodFrame(frameprop, payload))
hdrpayload = TAMQPHeaderPayload(payload.class, msg)
send(c, TAMQPContentHeaderFrame(frameprop, hdrpayload))
# send one or more message body frames
offset = 1
msglen = length(msg.data)
@debug("sending message with content body", msglen)
while offset <= msglen
msgend = min(msglen, offset + framemax - 1)
bodypayload = TAMQPBodyPayload(msg.data[offset:msgend])
offset = msgend + 1
@debug("sending content body frame", msglen, offset)
send(c, TAMQPContentBodyFrame(frameprop, bodypayload))
end
end
end
# ----------------------------------------
# Async message handler framework begin
# ----------------------------------------
function wait_for_state(c, states; interval=1, timeout=typemax(Int))
timedwait(Float64(timeout); pollint=Float64(interval)) do
# if we are looking for open states, and connection gets closed in the meantime, it's an error, break out
conn_error = !(CONN_STATE_CLOSED in states) && (c.state == CONN_STATE_CLOSED)
state_found = (c.state in states)
conn_error || state_found
end
c.state in states
end
function connection_processor(c, name, fn)
@debug("Starting task", name)
try
while true
fn(c)
end
catch err
reason = "$name task exiting."
if isa(c, MessageConsumer)
reason = reason * " Unhandled exception: $err"
@warn(reason, exception=(err,catch_backtrace()))
close(c)
else
isconnclosed = !isopen(c)
ischanclosed = isa(c, MessageChannel) && isa(err, InvalidStateException) && err.state == :closed
if ischanclosed || isconnclosed
reason = reason * " Connection closed"
if c.state !== CONN_STATE_CLOSING
reason = reason * " by peer"
close(c, false, true)
end
@debug(reason, exception=(err,catch_backtrace()))
else
if !(c.state in (CONN_STATE_CLOSING, CONN_STATE_CLOSED))
reason = reason * " Unhandled exception: $err"
@warn(reason, exception=(err,catch_backtrace()))
end
close(c, false, true)
end
end
end
end
function connection_sender(c::Connection)
@debug("==> sending on conn", host=c.host, port=c.port, virtualhost=c.virtualhost)
nbytes = sendq_to_stream(sock(c), c.sendq)
@debug("==> sent", nbytes)
c.heartbeat_time_client = time() # update heartbeat time for client
nothing
end
function sendq_to_stream(conn::TCPSocket, sendq::Channel{TAMQPGenericFrame})
msg = take!(sendq)
if length(msg.payload.data) > TCP_MIN_WRITEBUFF_SIZE # write large messages directly
nbytes = write(conn, msg)
else # coalesce short messages and do single write
buff = IOBuffer()
nbytes = write(buff, msg)
while isready(sendq) && (nbytes < TCP_MAX_WRITEBUFF_SIZE)
nbytes += write(buff, take!(sendq))
end
write(conn, take!(buff))
end
nbytes
end
function sendq_to_stream(conn::BufferedTLSSocket, sendq::Channel{TAMQPGenericFrame})
# avoid multiple small writes to TLS layer
nbytes = write(conn, take!(sendq))
while isready(sendq) && (nbytes < MbedTLS.MBEDTLS_SSL_MAX_CONTENT_LEN)
nbytes += write(conn, take!(sendq))
end
# flush does a single write of accumulated buffer
flush(conn)
nbytes
end
function connection_receiver(c::Connection)
f = read(sock(c), TAMQPGenericFrame)
# update heartbeat time for server
c.heartbeat_time_server = time()
channelid = f.props.channel
@debug("<== read message on conn", host=c.virtualhost, channelid)
if !(channelid in keys(c.channels))
@warn("Discarding message for unknown channel", channelid)
end
chan = channel(c, channelid)
put!(chan.recvq, f)
nothing
end
function connection_heartbeater(c::Connection)
sleep(c.heartbeat)
isopen(c) || throw(AMQPClientException("Connection closed"))
now = time()
if (now - c.heartbeat_time_client) > c.heartbeat
send_connection_heartbeat(c)
end
if (now - c.heartbeat_time_server) > (2 * c.heartbeat)
@warn("server heartbeat missed", secs=(now - c.heartbeat_time_server))
close(c, false, false)
end
nothing
end
function channel_receiver(c::MessageChannel)
f = take!(c.recvq)
if f.hdr == FrameMethod
m = TAMQPMethodFrame(f)
@debug("<== received", channel=f.props.channel, class=m.payload.class, method=m.payload.method)
cbkey = (f.hdr, m.payload.class, m.payload.method)
elseif f.hdr == FrameHeartbeat
m = TAMQPHeartBeatFrame(f)
@debug("<== received heartbeat", channel=f.props.channel)
cbkey = (f.hdr,)
elseif f.hdr == FrameHeader
m = TAMQPContentHeaderFrame(f)
@debug("<== received contentheader", channel=f.props.channel)
cbkey = (f.hdr,)
elseif f.hdr == FrameBody
m = TAMQPContentBodyFrame(f)
@debug("<== received contentbody", channel=f.props.channel)
cbkey = (f.hdr,)
else
m = f
@warn("<== received unhandled frame type", channel=f.props.channel, type=f.hdr)
cbkey = (f.hdr,)
end
(cb,ctx) = get(c.callbacks, cbkey, (on_unexpected_message, nothing))
@assert f.props.channel == c.id
cb(c, m, ctx)
nothing
end
function channel_message_consumer(c::MessageConsumer)
m = take!(c.recvq)
c.callback(m)
nothing
end
clear_handlers(c::MessageChannel) = (empty!(c.callbacks); nothing)
function handle(c::MessageChannel, classname::Symbol, methodname::Symbol, cb=nothing, ctx=nothing)
cbkey = method_key(classname, methodname)
if cb === nothing
delete!(c.callbacks, cbkey)
else
c.callbacks[cbkey] = (cb, ctx)
end
nothing
end
function handle(c::MessageChannel, frame_type::Integer, cb=nothing, ctx=nothing)
cbkey = frame_key(frame_type)
if cb === nothing
delete!(c.callbacks, cbkey)
else
c.callbacks[cbkey] = (cb, ctx)
end
nothing
end
# ----------------------------------------
# Async message handler framework end
# ----------------------------------------
# ----------------------------------------
# Open channel / connection begin
# ----------------------------------------
function find_unused_channel(c::Connection)
k = keys(c.channels)
maxid = c.channelmax
for id in 0:maxid
if !(id in k)
return id
end
end
throw(AMQPClientException("No free channel available (max: $maxid)"))
end
"""
channel(conn, id, create)
channel(f, args...)
Create or return an existing a channel object.
Multiple channels can be multiplexed over a single connection.
Can be used with the Julia do block syntax to create a channel and close it afterwards.
- `conn`: The connection over which to create the channel.
- `id`: Channels are identified by their numeric id. Specifying `AMQPClient.UNUSED_CHANNEL` as channel
id during creation will automatically assign an unused id.
- `create`: If true, a new channel will be created. Else an existing channel with the specified id
will be returned.
"""
channel(c::MessageChannel, id::Integer) = channel(c.conn, id)
channel(c::Connection, id::Integer) = c.channels[id]
channel(c::MessageChannel, id::Integer, create::Bool) = channel(c.conn, id, create)
function channel(c::Connection, id::Integer, create::Bool; connect_timeout=DEFAULT_CONNECT_TIMEOUT)
if create
if id == UNUSED_CHANNEL
id = find_unused_channel(c)
elseif id in keys(c.channels)
throw(AMQPClientException("Channel Id $id is already in use"))
end
chan = MessageChannel(id, c)
chan.state = CONN_STATE_OPENING
c.channels[chan.id] = chan
if id != DEFAULT_CHANNEL
# open the channel
chan.receiver = @async connection_processor(chan, "ChannelReceiver($(chan.id))", channel_receiver)
handle(chan, :Channel, :OpenOk, on_channel_open_ok)
send_channel_open(chan)
if !wait_for_state(chan, CONN_STATE_OPEN; timeout=connect_timeout)
error_message = "Channel handshake failed"
if nothing !== chan.closereason
error_message = string(error_message, " - ", string(chan.closereason.code), " (", convert(String, chan.closereason.msg), ")")
end
throw(AMQPClientException(error_message))
end
end
else
chan = channel(c, id)
end
chan
end
function channel(f, args...; kwargs...)
chan = channel(args...; kwargs...)
try
f(chan)
catch
rethrow()
finally
close(chan)
end
end
"""
connection(f; kwargs...)
connection(;
virtualhost = "/",
host = "localhost",
port = AMQPClient.AMQP_DEFAULT_PORT,
framemax = 0,
heartbeat = true,
keepalive = DEFAULT_KEEPALIVE_SECS,
send_queue_size = CONN_MAX_QUEUED,
auth_params = AMQPClient.DEFAULT_AUTH_PARAMS,
channelmax = AMQPClient.DEFAULT_CHANNELMAX,
connect_timeout = AMQPClient.DEFAULT_CONNECT_TIMEOUT,
amqps = nothing
)
Creates a fresh connection to the AMQP server.
Returns a connection that can be used to open channels subsequently.
Can be used with the Julia do block syntax to create a connection and close it afterwards.
Keyword arguments:
- `host`: The message server host to connect to. Defaults to "localhost".
- `port`: The message server port to connect to. Defaults to the default AMQP port.
- `virtualhost`: The virtual host to connect to. Defaults to "/".
- `amqps`: If connection is to be done over AMQPS, the TLS options to use. See `amqps_configure`.
- `connect_timeout`: TCP connect timeout to impose. Default `AMQPClient.DEFAULT_CONNECT_TIMEOUT`,
- `framemax`: The maximum frame size to use. Defaults to 0, which means no limit.
- `heartbeat`: `true` to enable heartbeat, `false` to disable. Can also be set to a positive integer,
in which case it is the heartbeat interval in seconds. Defaults to `true`. If `false`, ensure
`keepalive` is enabled to detect dead connections. This parameter is negotiated with the server.
- `keepalive`: `true` to enable TCP keepalives, `false` to disable. Can also be set to a positive integer,
in which case it is the keepalive interval in seconds. Defaults to `DEFAULT_KEEPALIVE_SECS`.
- `send_queue_size`: Maximum number of items to buffer in memory before blocking the send API until
messages are drained. Defaults to CONN_MAX_QUEUED.
- `auth_params`: Parameters to use to authenticate the connection. Defaults to AMQPClient.DEFAULT_AUTH_PARAMS.
- `channelmax`: Maximum channel number to impose/negotiate with the server. Defaults to AMQPClient.DEFAULT_CHANNELMAX.
"""
function connection(; virtualhost="/", host="localhost", port=AMQPClient.AMQP_DEFAULT_PORT,
framemax=0,
heartbeat::Union{Int,Bool}=true,
keepalive::Union{Int,Bool}=DEFAULT_KEEPALIVE_SECS,
send_queue_size::Integer=CONN_MAX_QUEUED,
auth_params=AMQPClient.DEFAULT_AUTH_PARAMS,
channelmax::Integer=AMQPClient.DEFAULT_CHANNELMAX,
connect_timeout=AMQPClient.DEFAULT_CONNECT_TIMEOUT,
amqps::Union{MbedTLS.SSLConfig,Nothing}=nothing)
@debug("connecting", host, port, virtualhost)
keepalive_interval = isa(keepalive, Bool) ? DEFAULT_KEEPALIVE_SECS : keepalive
enable_keepalive = isa(keepalive, Bool) ? keepalive : (keepalive_interval > 0)
heartbeat_interval = isa(heartbeat, Bool) ? 0 : heartbeat
enable_heartbeat = isa(heartbeat, Bool) ? heartbeat : (heartbeat > 0)
conn = Connection(;
virtualhost=virtualhost,
host=host,
port=port,
send_queue_size=send_queue_size,
heartbeat=heartbeat_interval,
enable_heartbeat=enable_heartbeat,
keepalive=keepalive_interval,
enable_keepalive=enable_keepalive,)
chan = channel(conn, AMQPClient.DEFAULT_CHANNEL, true)
# setup handler for Connection.Start
ctx = Dict(:auth_params=>auth_params, :channelmax=>channelmax, :framemax=>framemax, :heartbeat=>heartbeat_interval)
AMQPClient.handle(chan, :Connection, :Start, AMQPClient.on_connection_start, ctx)
# open socket and start processor tasks
sock = connect(conn.host, conn.port)
isdefined(Sockets, :nagle) && Sockets.nagle(sock, false)
isdefined(Sockets, :quickack) && Sockets.quickack(sock, true)
keepalive!(sock, enable_keepalive; interval=keepalive_interval)
conn.sock = (amqps !== nothing) ? setup_tls(sock, host, amqps) : sock
conn.sender = @async AMQPClient.connection_processor(conn, "ConnectionSender", AMQPClient.connection_sender)
conn.receiver = @async AMQPClient.connection_processor(conn, "ConnectionReceiver", AMQPClient.connection_receiver)
chan.receiver = @async AMQPClient.connection_processor(chan, "ChannelReceiver($(chan.id))", AMQPClient.channel_receiver)
# initiate handshake
conn.state = chan.state = AMQPClient.CONN_STATE_OPENING
write(AMQPClient.sock(chan), AMQPClient.ProtocolHeader)
flush(AMQPClient.sock(chan))
if !AMQPClient.wait_for_state(conn, AMQPClient.CONN_STATE_OPEN; timeout=connect_timeout) || !AMQPClient.wait_for_state(chan, AMQPClient.CONN_STATE_OPEN; timeout=connect_timeout)
error_message = "Connection handshake failed"
if nothing !== chan.closereason
error_message = string(error_message, " - ", string(chan.closereason.code), " (", convert(String, chan.closereason.msg), ")")
end
throw(AMQPClientException(error_message))
end
conn
end
function connection(f; kwargs...)
conn = connection(; kwargs...)
try
f(conn)
catch
rethrow()
finally
close(conn)
end
end
# ----------------------------------------
# Open channel / connection end
# ----------------------------------------
# ----------------------------------------
# Close channel / connection begin
# ----------------------------------------
function close(chan::MessageChannel, handshake::Bool=true, by_peer::Bool=false, reply_code=ReplySuccess, reply_text="", class_id=0, method_id=0)
(chan.state == CONN_STATE_CLOSED) && (return nothing)
conn = chan.conn
if chan.id == DEFAULT_CHANNEL
# default channel represents the connection
close(conn, handshake, by_peer, reply_code, reply_text, class_id, method_id)
elseif chan.state != CONN_STATE_CLOSING
# send handshake if needed and when called the first time
chan.state = CONN_STATE_CLOSING
if handshake && !by_peer
send_channel_close(chan, reply_code, reply_text, class_id, method_id)
end
end
# release resources when closed by peer or when closing abruptly
if !handshake || by_peer
close(chan.recvq)
close(chan.chan_get)
map(close, values(chan.consumers))
empty!(chan.consumers)
chan.receiver = nothing
chan.callbacks = Dict{Tuple,Tuple{Function,Any}}()
delete!(chan.conn.channels, chan.id)
chan.state = CONN_STATE_CLOSED
end
nothing
end
function close(conn::Connection, handshake::Bool=true, by_peer::Bool=false, reply_code=ReplySuccess, reply_text="", class_id=0, method_id=0)
(conn.state == CONN_STATE_CLOSED) && (return nothing)
# send handshake if needed and when called the first time
if conn.state != CONN_STATE_CLOSING
conn.state = CONN_STATE_CLOSING
# close all other open channels
for open_channel in collect(values(conn.channels))
if open_channel.id != DEFAULT_CHANNEL
close(open_channel, false, by_peer)
end
end
# send handshake if needed
if handshake && !by_peer
send_connection_close(conn, reply_code, reply_text, class_id, method_id)
end
end
if !handshake || by_peer
# close socket
close(conn.sock)
conn.sock = nothing
# reset all members
conn.properties = Dict{Symbol,Any}()
conn.capabilities = Dict{String,Any}()
conn.channelmax = 0
conn.framemax = 0
conn.heartbeat = 0
# close and reset the sendq channel
close(conn.sendq)
conn.sendq = Channel{TAMQPGenericFrame}(CONN_MAX_QUEUED)
# reset the tasks
conn.sender = nothing
conn.receiver = nothing
conn.heartbeater = nothing
conn.state = CONN_STATE_CLOSED
end
nothing
end
# ----------------------------------------
# Close channel / connection end
# ----------------------------------------
# ----------------------------------------
# Connection and Channel end
# ----------------------------------------
# ----------------------------------------
# Exchange begin
# ----------------------------------------
const EXCHANGE_TYPE_DIRECT = "direct" # must be implemented by servers
const EXCHANGE_TYPE_FANOUT = "fanout" # must be implemented by servers
const EXCHANGE_TYPE_TOPIC = "topic" # optional, must test before typing to open
const EXCHANGE_TYPE_HEADERS = "headers" # optional, must test before typing to open
# The server MUST, in each virtual host, predeclare an exchange instance for each standard
# exchange type that it implements, where the name of the exchange instance, if defined, is "amq."
# followed by the exchange type name.
# The server MUST predeclare a direct exchange with no public name to act as the default
# exchange for content Publish methods and for default queue bindings.
default_exchange_name(excg_type) = ("amq." * excg_type)
default_exchange_name() = ""
function _wait_resp(sendmethod, chan::MessageChannel, default_result::T,
nowait::Bool=true, resp_handler=nothing, resp_class=nothing, resp_meth=nothing,
timeout_result::T=default_result, timeout::Int=DEFAULT_TIMEOUT) where {T}
result = default_result
if !nowait
reply = Channel{T}(1)
# register a callback
handle(chan, resp_class, resp_meth, resp_handler, reply)
end
sendmethod()
if !nowait
# wait for response
result = timeout_result
if :ok === timedwait(()->(isready(reply) || !isopen(chan)), Float64(timeout); pollint=0.01)
if isready(reply)
result = take!(reply)
else
error_message = "Connection closed"
if nothing !== chan.closereason
error_message = string(error_message, " - ", string(chan.closereason.code), " (", convert(String, chan.closereason.msg), ")")
end
throw(AMQPClientException(error_message))
end
end
close(reply)
end
result
end
function exchange_declare(chan::MessageChannel, name::String, typ::String;
passive::Bool=false, durable::Bool=false, auto_delete::Bool=false,
nowait::Bool=false, timeout::Int=DEFAULT_TIMEOUT,
arguments::Dict{String,Any}=Dict{String,Any}())
(isempty(name) || startswith(name, "amq.")) && !passive && throw(AMQPClientException("Exchange name '$name' is reserved. Use a different name."))
if auto_delete
@debug("Warning: auto_delete exchange types are deprecated")
end
_wait_resp(chan, true, nowait, on_exchange_declare_ok, :Exchange, :DeclareOk, false, timeout) do
send_exchange_declare(chan, name, typ, passive, durable, auto_delete, nowait, arguments)
end
end
function exchange_delete(chan::MessageChannel, name::String; if_unused::Bool=false, nowait::Bool=false, timeout::Int=DEFAULT_TIMEOUT)
(isempty(name) || startswith(name, "amq.")) && throw(AMQPClientException("Exchange name '$name' is reserved. Use a different name."))
_wait_resp(chan, true, nowait, on_exchange_delete_ok, :Exchange, :DeleteOk, false, timeout) do
send_exchange_delete(chan, name, if_unused, nowait)
end
end
function exchange_bind(chan::MessageChannel, dest::String, src::String, routing_key::String;
nowait::Bool=false, timeout::Int=DEFAULT_TIMEOUT,
arguments::Dict{String,Any}=Dict{String,Any}())
_wait_resp(chan, true, nowait, on_exchange_bind_ok, :Exchange, :BindOk, false, timeout) do
send_exchange_bind(chan, dest, src, routing_key, nowait, arguments)
end
end
function exchange_unbind(chan::MessageChannel, dest::String, src::String, routing_key::String;
nowait::Bool=false, timeout::Int=DEFAULT_TIMEOUT,
arguments::Dict{String,Any}=Dict{String,Any}())
_wait_resp(chan, true, nowait, on_exchange_unbind_ok, :Exchange, :UnbindOk, false, timeout) do
send_exchange_unbind(chan, dest, src, routing_key, nowait, arguments)
end
end
# ----------------------------------------
# Exchange end
# ----------------------------------------
# ----------------------------------------
# Queue begin
# ----------------------------------------
"""Declare a queue (or query an existing queue).
Returns a tuple: (boolean success/failure, queue name, message count, consumer count)
"""
function queue_declare(chan::MessageChannel, name::String;
passive::Bool=false, durable::Bool=false, exclusive::Bool=false, auto_delete::Bool=false,
nowait::Bool=false, timeout::Int=DEFAULT_TIMEOUT,
arguments::Dict{String,Any}=Dict{String,Any}())
_wait_resp(chan, (true, "", TAMQPMessageCount(0), Int32(0)), nowait, on_queue_declare_ok, :Queue, :DeclareOk, (false,"", TAMQPMessageCount(0), Int32(0)), timeout) do
send_queue_declare(chan, name, passive, durable, exclusive, auto_delete, nowait, arguments)
end
end
function queue_bind(chan::MessageChannel, queue_name::String, excg_name::String, routing_key::String; nowait::Bool=false, timeout::Int=DEFAULT_TIMEOUT, arguments::Dict{String,Any}=Dict{String,Any}())
_wait_resp(chan, true, nowait, on_queue_bind_ok, :Queue, :BindOk, false, timeout) do
send_queue_bind(chan, queue_name, excg_name, routing_key, nowait, arguments)
end
end
function queue_unbind(chan::MessageChannel, queue_name::String, excg_name::String, routing_key::String; arguments::Dict{String,Any}=Dict{String,Any}(), timeout::Int=DEFAULT_TIMEOUT)
nowait = false
_wait_resp(chan, true, nowait, on_queue_unbind_ok, :Queue, :UnbindOk, false, timeout) do
send_queue_unbind(chan, queue_name, excg_name, routing_key, arguments)
end
end
"""Purge messages from a queue.
Returns a tuple: (boolean success/failure, message count)
"""
function queue_purge(chan::MessageChannel, name::String; nowait::Bool=false, timeout::Int=DEFAULT_TIMEOUT)
_wait_resp(chan, (true,TAMQPMessageCount(0)), nowait, on_queue_purge_ok, :Queue, :PurgeOk, (false,TAMQPMessageCount(0)), timeout) do
send_queue_purge(chan, name, nowait)
end
end
"""Delete a queue.
Returns a tuple: (boolean success/failure, message count)
"""
function queue_delete(chan::MessageChannel, name::String; if_unused::Bool=false, if_empty::Bool=false, nowait::Bool=false, timeout::Int=DEFAULT_TIMEOUT)
_wait_resp(chan, (true,TAMQPMessageCount(0)), nowait, on_queue_delete_ok, :Queue, :DeleteOk, (false,TAMQPMessageCount(0)), timeout) do
send_queue_delete(chan, name, if_unused, if_empty, nowait)
end
end
# ----------------------------------------
# Queue end
# ----------------------------------------
# ----------------------------------------
# Tx begin
# ----------------------------------------
function _tx(sendmethod, chan::MessageChannel, respmethod::Symbol, on_resp, timeout::Int)
nowait = false
_wait_resp(chan, true, nowait, on_resp, :Tx, respmethod, false, timeout) do
sendmethod(chan)
end
end
tx_select(chan::MessageChannel; timeout::Int=DEFAULT_TIMEOUT) = _tx(send_tx_select, chan, :SelectOk, on_tx_select_ok, timeout)
tx_commit(chan::MessageChannel; timeout::Int=DEFAULT_TIMEOUT) = _tx(send_tx_commit, chan, :CommitOk, on_tx_commit_ok, timeout)
tx_rollback(chan::MessageChannel; timeout::Int=DEFAULT_TIMEOUT) = _tx(send_tx_rollback, chan, :RollbackOk, on_tx_rollback_ok, timeout)
# ----------------------------------------
# Tx end
# ----------------------------------------
# ----------------------------------------
# Basic begin
# ----------------------------------------
function basic_qos(chan::MessageChannel, prefetch_size, prefetch_count, apply_global::Bool; timeout::Int=DEFAULT_TIMEOUT)
nowait = false
_wait_resp(chan, true, nowait, on_basic_qos_ok, :Basic, :QosOk, false, timeout) do
send_basic_qos(chan, prefetch_size, prefetch_count, apply_global)
end
end
"""Start a queue consumer.
queue: queue name
consumer_tag: id of the consumer, server generates a unique tag if this is empty
no_local: do not deliver own messages
no_ack: no acknowledgment needed, server automatically and silently acknowledges delivery (speed at the cost of reliability)
exclusive: request exclusive access (only this consumer can access the queue)
nowait: do not send a reply method
"""
function basic_consume(chan::MessageChannel, queue::String, consumer_fn::Function; consumer_tag::String="", no_local::Bool=false, no_ack::Bool=false,
exclusive::Bool=false, nowait::Bool=false, arguments::Dict{String,Any}=Dict{String,Any}(), timeout::Int=DEFAULT_TIMEOUT, buffer_sz::Int=typemax(Int))
# register the consumer and get the consumer_tag
result = _wait_resp(chan, (true, ""), nowait, on_basic_consume_ok, :Basic, :ConsumeOk, (false, ""), timeout) do
send_basic_consume(chan, queue, consumer_tag, no_local, no_ack, exclusive, nowait, arguments)
end
# start the message consumer
if result[1]
consumer_tag = result[2]
# set up message buffer beforehand to store messages that the consumer may receive while we are still setting things up,
# or get the buffer that was set up already because we received messages
lock(chan.lck) do
consumer_buffer = get!(chan.pending_msgs, consumer_tag) do
Channel{Message}(buffer_sz)
end
consumer_buffer.sz_max = buffer_sz
chan.consumers[consumer_tag] = MessageConsumer(chan.id, consumer_tag, consumer_fn; buffer=consumer_buffer)
delete!(chan.pending_msgs, consumer_tag)
end
end
result
end
"""Cancels a consumer.
This does not affect already delivered messages, but it does mean the server will not send any more messages for that consumer. The client may receive an arbitrary number of
messages in between sending the cancel method and receiving the cancelok reply.
"""
function basic_cancel(chan::MessageChannel, consumer_tag::String; nowait::Bool=false, timeout::Int=DEFAULT_TIMEOUT)
result = _wait_resp(chan, (true, ""), nowait, on_basic_cancel_ok, :Basic, :CancelOk, (false, ""), timeout) do
send_basic_cancel(chan, consumer_tag, nowait)
end
# clear a message consumer
if result[1]