Skip to content

Commit

Permalink
Introduce method for send buffer size and extract module
Browse files Browse the repository at this point in the history
Signed-off-by: Pedro Tanaka <[email protected]>
  • Loading branch information
pedro-stanaka committed Dec 19, 2024
1 parent fc59717 commit 55e0c2b
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 26 deletions.
37 changes: 37 additions & 0 deletions lib/statsd/instrument/connection_behavior.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# frozen_string_literal: true

module StatsD
module Instrument
module ConnectionBehavior
def close
@socket&.close
rescue IOError, SystemCallError => e
StatsD.logger.debug do
"[#{self.class.name}] Error closing socket: #{e.class}: #{e.message}"
end
ensure
@socket = nil
end

def send_buffer_size
if socket
socket.getsockopt(Socket::SOL_SOCKET, Socket::SO_SNDBUF).int
else
@max_packet_size
end
end

private

def setup_socket(socket)
socket.setsockopt(Socket::SOL_SOCKET, Socket::SO_SNDBUF, @max_packet_size.to_i)
socket
rescue IOError => e
StatsD.logger.debug do
"[#{self.class.name}] Failed to create socket: #{e.class}: #{e.message}"
end
nil
end
end
end
end
13 changes: 5 additions & 8 deletions lib/statsd/instrument/udp_connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
module StatsD
module Instrument
class UdpConnection
include ConnectionBehavior

DEFAULT_MAX_PACKET_SIZE = 1_472

attr_reader :host, :port
Expand All @@ -17,11 +19,6 @@ def send_datagram(message)
socket.send(message, 0)
end

def close
@socket&.close
@socket = nil
end

def type
:udp
end
Expand All @@ -31,9 +28,9 @@ def type
def socket
@socket ||= begin
socket = UDPSocket.new
socket.setsockopt(Socket::SOL_SOCKET, Socket::SO_SNDBUF, @max_packet_size)
socket.connect(@host, @port)
socket
setup_socket(socket)&.tap do |s|
s.connect(@host, @port)
end
end
end
end
Expand Down
23 changes: 5 additions & 18 deletions lib/statsd/instrument/uds_connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
module StatsD
module Instrument
class UdsConnection
include ConnectionBehavior

DEFAULT_MAX_PACKET_SIZE = 8_192

def initialize(socket_path, max_packet_size: DEFAULT_MAX_PACKET_SIZE)
Expand All @@ -17,17 +19,7 @@ def initialize(socket_path, max_packet_size: DEFAULT_MAX_PACKET_SIZE)
end

def send_datagram(message)
socket.sendmsg(message, 0)
end

def close
@socket&.close
rescue IOError, SystemCallError => e
StatsD.logger.debug do
"[#{self.class.name}] Error closing socket: #{e.class}: #{e.message}"
end
ensure
@socket = nil
socket&.sendmsg(message, 0)
end

def host
Expand All @@ -47,14 +39,9 @@ def type
def socket
@socket ||= begin
socket = Socket.new(Socket::AF_UNIX, Socket::SOCK_DGRAM)
socket.setsockopt(Socket::SOL_SOCKET, Socket::SO_SNDBUF, @max_packet_size.to_i)
socket.connect(Socket.pack_sockaddr_un(@socket_path))
socket
rescue IOError => e
StatsD.logger.debug do
"[#{self.class.name}] Failed to create socket: #{e.class}: #{e.message}"
setup_socket(socket)&.tap do |s|
s.connect(Socket.pack_sockaddr_un(@socket_path))
end
nil
end
end
end
Expand Down

0 comments on commit 55e0c2b

Please sign in to comment.