@@ -9,7 +9,7 @@ module AMQProxy
9
9
Log = ::Log .for(self )
10
10
getter credentials : Credentials
11
11
@channel_map = Hash (UInt16 , UpstreamChannel ?).new
12
- @outgoing_frames = Channel ( AMQ :: Protocol :: Frame ) .new( 128 )
12
+ @lock = Mutex .new
13
13
@frame_max : UInt32
14
14
@channel_max : UInt16
15
15
@heartbeat : UInt16
@@ -21,7 +21,6 @@ module AMQProxy
21
21
@frame_max = tune_ok.frame_max
22
22
@channel_max = tune_ok.channel_max
23
23
@heartbeat = tune_ok.heartbeat
24
- spawn write_loop
25
24
end
26
25
27
26
# Keep a buffer of publish frames
@@ -125,36 +124,27 @@ module AMQProxy
125
124
else
126
125
Log .debug { " Disconnected" }
127
126
ensure
128
- @outgoing_frames .close
129
127
socket.close rescue nil
130
128
close_all_upstream_channels
131
129
end
132
130
133
- private def write_loop (socket = @socket )
134
- while frame = @outgoing_frames .receive?
135
- socket.write_bytes frame, IO ::ByteFormat ::NetworkEndian
136
- socket.flush unless expect_more_publish_frames?(frame)
137
- case frame
138
- when AMQ ::Protocol ::Frame ::Channel ::Close
139
- @channel_map [frame.channel] = nil
140
- when AMQ ::Protocol ::Frame ::Channel ::CloseOk
141
- @channel_map .delete(frame.channel)
142
- when AMQ ::Protocol ::Frame ::Connection ::CloseOk
143
- break
144
- end
131
+ # Send frame to client, channel id should already be remapped by the caller
132
+ def write (frame : AMQ ::Protocol ::Frame )
133
+ @lock .synchronize do
134
+ @socket .write_bytes frame, IO ::ByteFormat ::NetworkEndian
135
+ @socket .flush unless expect_more_publish_frames?(frame)
136
+ end
137
+ case frame
138
+ when AMQ ::Protocol ::Frame ::Channel ::Close
139
+ @channel_map [frame.channel] = nil
140
+ when AMQ ::Protocol ::Frame ::Channel ::CloseOk
141
+ @channel_map .delete(frame.channel)
142
+ when AMQ ::Protocol ::Frame ::Connection ::CloseOk
143
+ @socket .close rescue nil
145
144
end
146
145
rescue ex : IO ::Error
147
146
# Client closed connection, suppress error
148
- ensure
149
- @outgoing_frames .close
150
- socket.close rescue nil
151
- end
152
-
153
- # Send frame to client, channel id should already be remapped by the caller
154
- def write (frame : AMQ ::Protocol ::Frame )
155
- @outgoing_frames .send frame
156
- rescue Channel ::ClosedError
157
- # do nothing
147
+ @socket .close rescue nil
158
148
end
159
149
160
150
def close_connection (code, text, frame = nil )
@@ -196,9 +186,8 @@ module AMQProxy
196
186
# @socket.read_timeout = 1.seconds
197
187
end
198
188
199
- # Close the outgoing frames channel which will let write_loop close the socket
200
189
def close_socket
201
- @outgoing_frames .close
190
+ @socket .close rescue nil
202
191
end
203
192
204
193
private def set_socket_options (socket = @socket )
0 commit comments