Skip to content

Commit

Permalink
v0.14.0 (#15)
Browse files Browse the repository at this point in the history
- optimise #read
- optimise `Configuration#equal?`
- reorganise tests
- tidy-up
  • Loading branch information
mblumtritt authored Mar 9, 2024
1 parent fa4e684 commit 31901c1
Show file tree
Hide file tree
Showing 17 changed files with 82 additions and 174 deletions.
1 change: 1 addition & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--require helper
17 changes: 7 additions & 10 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,18 @@

$stdout.sync = $stderr.sync = true

require 'rake/clean'
require 'bundler/gem_tasks'

require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:test) { _1.ruby_opts = %w[-w] }

require 'yard'
YARD::Rake::YardocTask.new(:doc) { _1.stats_options = %w[--list-undoc] }

desc 'Run YARD development server'
task('doc:dev' => :clobber) { exec('yard server --reload') }

CLEAN << '.yardoc'
CLOBBER << 'doc'

task(:default) { exec('rake --tasks') }

RSpec::Core::RakeTask.new(:test) { |task| task.ruby_opts = %w[-w] }

YARD::Rake::YardocTask.new(:doc) do |task|
task.stats_options = %w[--list-undoc]
end

desc 'Run YARD development server'
task('doc:dev' => :clobber) { exec('yard server --reload') }
9 changes: 2 additions & 7 deletions lib/tcp-client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ def self.open(address, configuration = nil)
# @see #with_deadline
#
def self.with_deadline(timeout, address, configuration = nil)
client = nil
raise(NoBlockGivenError) unless block_given?
client = new
client.with_deadline(timeout) do
Expand All @@ -116,9 +115,7 @@ def self.with_deadline(timeout, address, configuration = nil)
# @attribute [r] :closed?
# @return [Boolean] whether the connection is closed
#
def closed?
@socket.nil? || @socket.closed?
end
def closed? = @socket.nil? || @socket.closed?

#
# Close the current connection if connected.
Expand Down Expand Up @@ -240,9 +237,7 @@ def readline(separator = $/, chomp: false, timeout: nil, exception: nil)
#
# @see Address#to_s
#
def to_s
@address&.to_s || ''
end
def to_s = @address.to_s

#
# Executes a block with a given overall time limit.
Expand Down
41 changes: 13 additions & 28 deletions lib/tcp-client/address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Address
# @return [Addrinfo] the address info
#
def addrinfo
freeze if @addrinfo.nil?
freeze unless @addrinfo
@addrinfo
end

Expand All @@ -28,7 +28,7 @@ def addrinfo
# @return [String] the host name
#
def host
freeze if @host.nil?
freeze unless @host
@host
end
alias hostname host
Expand All @@ -37,9 +37,7 @@ def host
# @attribute [r] port
# @return [Integer] the port number
#
def port
addrinfo.ip_port
end
def port = addrinfo.ip_port

#
# Initializes an address
Expand Down Expand Up @@ -73,18 +71,14 @@ def port
#
# @param port [Integer] the addressed port
#
def initialize(addr)
@addr = addr
end
def initialize(addr) = (@addr = addr)

#
# Convert `self` to a Hash containing host and port attribute.
#
# @return [Hash] host and port
#
def to_hash
{ host: host, port: port }
end
def to_hash = { host: host, port: port }

#
# Convert `self` to a Hash containing host and port attribute.
Expand All @@ -93,16 +87,12 @@ def to_hash
# @overload to_h(&block)
# @return [Hash] host and port
#
def to_h(&block)
block ? to_hash.to_h(&block) : to_hash
end
def to_h(&block) = block ? to_hash.to_h(&block) : to_hash

#
# @return [String] text representation of self as "host:port"
#
def to_s
host.index(':') ? "[#{host}]:#{port}" : "#{host}:#{port}"
end
def to_s = host.index(':') ? "[#{host}]:#{port}" : "#{host}:#{port}"

#
# Force the address resolution and prevents further modifications of itself.
Expand All @@ -114,20 +104,15 @@ def freeze
solve
@addrinfo.freeze
@host.freeze
@addr = nil
super
end

# @!visibility private
def ==(other)
to_hash == other.to_h
end
def ==(other) = to_hash == other.to_h
alias eql? ==

# @!visibility private
def equal?(other)
self.class == other.class && self == other
end
def equal?(other) = self.class == other.class && self == other

private

Expand All @@ -142,6 +127,7 @@ def solve
else
from_string(@addr)
end
@addr = nil
end

def from_self_class(address)
Expand All @@ -154,14 +140,13 @@ def from_self_class(address)
end

def from_addrinfo(addrinfo)
@host = addrinfo.getnameinfo(Socket::NI_NUMERICSERV).first
@addrinfo = addrinfo
@host = (@addrinfo = addrinfo).getnameinfo(Socket::NI_NUMERICSERV).first
end

def from_string(str)
@host, port = host_n_port(str.to_s)
return @addrinfo = Addrinfo.tcp(@host, port) if @host
from_addrinfo(Addrinfo.tcp(nil, port))
@addrinfo = Addrinfo.tcp(@host, port)
@host ||= @addrinfo.getnameinfo(Socket::NI_NUMERICSERV).first
end

def host_n_port(str)
Expand Down
53 changes: 20 additions & 33 deletions lib/tcp-client/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,7 @@ def reverse_lookup=(value)
# @!parse attr_reader :ssl?
# @return [Boolean] whether SSL is configured, see {#ssl_params}
#
def ssl?
@ssl_params ? true : false
end
def ssl? = @ssl_params ? true : false

#
# Parameters used to initialize a SSL context. SSL/TLS will only be used if
Expand All @@ -111,7 +109,7 @@ def ssl_params=(value)
if value.respond_to?(:to_hash)
Hash[value.to_hash]
elsif value.respond_to?(:to_h)
value.nil? ? nil : Hash[value.to_h]
value.nil? ? nil : value.to_h.dup
else
value ? {} : nil
end
Expand All @@ -133,7 +131,7 @@ def ssl_params=(value)
attr_reader :connect_timeout

def connect_timeout=(value)
@connect_timeout = seconds(value)
@connect_timeout = as_seconds(value)
end

#
Expand All @@ -146,8 +144,7 @@ def connect_timeout=(value)
attr_reader :connect_timeout_error

def connect_timeout_error=(value)
raise(NotAnExceptionError, value) unless exception_class?(value)
@connect_timeout_error = value
@connect_timeout_error = as_exception(value)
end

#
Expand All @@ -161,7 +158,7 @@ def connect_timeout_error=(value)
attr_reader :read_timeout

def read_timeout=(value)
@read_timeout = seconds(value)
@read_timeout = as_seconds(value)
end

#
Expand All @@ -174,8 +171,7 @@ def read_timeout=(value)
attr_reader :read_timeout_error

def read_timeout_error=(value)
raise(NotAnExceptionError, value) unless exception_class?(value)
@read_timeout_error = value
@read_timeout_error = as_exception(value)
end

#
Expand All @@ -189,7 +185,7 @@ def read_timeout_error=(value)
attr_reader :write_timeout

def write_timeout=(value)
@write_timeout = seconds(value)
@write_timeout = as_seconds(value)
end

#
Expand All @@ -202,8 +198,7 @@ def write_timeout=(value)
attr_reader :write_timeout_error

def write_timeout_error=(value)
raise(NotAnExceptionError, value) unless exception_class?(value)
@write_timeout_error = value
@write_timeout_error = as_exception(value)
end

#
Expand All @@ -218,7 +213,7 @@ def write_timeout_error=(value)
# @see #write_timeout
#
def timeout=(value)
@connect_timeout = @write_timeout = @read_timeout = seconds(value)
@connect_timeout = @write_timeout = @read_timeout = as_seconds(value)
end

#
Expand All @@ -235,9 +230,8 @@ def timeout=(value)
# @see #write_timeout_error
#
def timeout_error=(value)
raise(NotAnExceptionError, value) unless exception_class?(value)
@connect_timeout_error =
@read_timeout_error = @write_timeout_error = value
@read_timeout_error = @write_timeout_error = as_exception(value)
end

# @!endgroup
Expand Down Expand Up @@ -291,9 +285,7 @@ def to_hash
#
# @see #configure
#
def to_h(&block)
block ? to_hash.to_h(&block) : to_hash
end
def to_h(&block) = block ? to_hash.to_h(&block) : to_hash

#
# Configures the instance with given options Hash.
Expand All @@ -318,7 +310,7 @@ def to_h(&block)
# @return [Configuration] self
#
def configure(options)
options.each_pair { |attribute, value| set(attribute, value) }
options.each_pair { set(*_1) }
self
end

Expand All @@ -336,30 +328,25 @@ def initialize_copy(_org)
end

# @!visibility private
def ==(other)
to_hash == other.to_h
end
def ==(other) = to_hash == other.to_h
alias eql? ==

# @!visibility private
def equal?(other)
self.class == other.class && self == other
end
def equal?(other) = self.class == other.class && to_hash == other.to_hash

private

def exception_class?(value)
value.is_a?(Class) && value < Exception
def as_seconds(value) = value&.to_f&.positive? ? value : nil

def as_exception(value)
return value if value.is_a?(Class) && value < Exception
raise(NotAnExceptionError, value, caller(2))
end

def set(attribute, value)
public_send("#{attribute}=", value)
rescue NoMethodError
raise(UnknownAttributeError, attribute)
end

def seconds(value)
value&.to_f&.positive? ? value : nil
raise(UnknownAttributeError, attribute, caller(2))
end
end
end
12 changes: 3 additions & 9 deletions lib/tcp-client/deadline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ def initialize(timeout)
@deadline = timeout&.positive? ? now + timeout : nil
end

def valid?
!@deadline.nil?
end
def valid? = !@deadline.nil?

def remaining_time
@deadline && (remaining = @deadline - now) > 0 ? remaining : nil
Expand All @@ -18,13 +16,9 @@ def remaining_time
private

if defined?(Process::CLOCK_MONOTONIC)
def now
Process.clock_gettime(Process::CLOCK_MONOTONIC)
end
def now = Process.clock_gettime(Process::CLOCK_MONOTONIC)
else
def now
::Time.now
end
def now = ::Time.now
end
end

Expand Down
24 changes: 10 additions & 14 deletions lib/tcp-client/default_configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,15 @@ def configure(options = nil, &block)
end

class Configuration
class << self
#
# @attribute [r] :default
# @return [Configuration] used by default if no dedicated configuration
# was specified
#
# @see TCPClient.open
# @see TCPClient.with_deadline
# @see TCPClient#connect
#
def default
TCPClient.default_configuration
end
end
#
# @attribute [r] :default
# @return [Configuration] used by default if no dedicated configuration
# was specified
#
# @see TCPClient.open
# @see TCPClient.with_deadline
# @see TCPClient#connect
#
def self.default = TCPClient.default_configuration
end
end
Loading

0 comments on commit 31901c1

Please sign in to comment.