Class: NNQ::Transport::ZstdTcp::ZstdConnection
- Inherits:
-
SimpleDelegator
- Object
- SimpleDelegator
- NNQ::Transport::ZstdTcp::ZstdConnection
- Defined in:
- lib/nnq/transport/zstd_tcp/connection.rb
Overview
Wraps an NNQ::Connection, transparently compressing every outbound body through the engine-scoped Codec and decompressing every inbound wire message. Dict frames are shipped in-band (first call after training) and silently installed on receive.
Instance Method Summary collapse
-
#initialize(conn, codec) ⇒ ZstdConnection
constructor
A new instance of ZstdConnection.
-
#receive_message ⇒ Object
Loops until a real payload arrives.
- #send_message(body, header: nil) ⇒ Object
- #write_message(body, header: nil) ⇒ Object
- #write_messages(bodies) ⇒ Object
Constructor Details
#initialize(conn, codec) ⇒ ZstdConnection
Returns a new instance of ZstdConnection.
13 14 15 16 |
# File 'lib/nnq/transport/zstd_tcp/connection.rb', line 13 def initialize(conn, codec) super(conn) @codec = codec end |
Instance Method Details
#receive_message ⇒ Object
Loops until a real payload arrives. Dict frames are installed silently and discarded so the caller only sees plaintext.
48 49 50 51 52 53 54 |
# File 'lib/nnq/transport/zstd_tcp/connection.rb', line 48 def loop do wire = __getobj__. decoded = @codec.decode(wire) return decoded unless decoded.nil? end end |
#send_message(body, header: nil) ⇒ Object
27 28 29 30 31 32 |
# File 'lib/nnq/transport/zstd_tcp/connection.rb', line 27 def (body, header: nil) combined = header ? (header + body) : body wire, dict_frame = @codec.encode(combined) __getobj__.(dict_frame) if dict_frame __getobj__.(wire) end |
#write_message(body, header: nil) ⇒ Object
19 20 21 22 23 24 |
# File 'lib/nnq/transport/zstd_tcp/connection.rb', line 19 def (body, header: nil) combined = header ? (header + body) : body wire, dict_frame = @codec.encode(combined) __getobj__.(dict_frame) if dict_frame __getobj__.(wire) end |
#write_messages(bodies) ⇒ Object
35 36 37 38 39 40 41 42 43 |
# File 'lib/nnq/transport/zstd_tcp/connection.rb', line 35 def (bodies) batch = [] bodies.each do |body| wire, dict_frame = @codec.encode(body) batch << dict_frame if dict_frame batch << wire end __getobj__.(batch) end |