Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

It's time for v1.0.0 #16

Merged
merged 1 commit into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/tcp-client/ssl_socket.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
end

require_relative 'deadline'
require_relative 'mixin/io_with_deadline'
require_relative 'with_deadline'

class TCPClient
class SSLSocket < ::OpenSSL::SSL::SSLSocket
include IOWithDeadlineMixin
include WithDeadline

def initialize(socket, address, configuration, deadline, exception)
ssl_params = Hash[configuration.ssl_params]
Expand Down
4 changes: 2 additions & 2 deletions lib/tcp-client/tcp_socket.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

require 'socket'
require_relative 'deadline'
require_relative 'mixin/io_with_deadline'
require_relative 'with_deadline'

class TCPClient
class TCPSocket < ::Socket
include IOWithDeadlineMixin
include WithDeadline

def initialize(address, configuration, deadline, exception)
super(address.addrinfo.ipv6? ? :INET6 : :INET, :STREAM)
Expand Down
2 changes: 1 addition & 1 deletion lib/tcp-client/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

class TCPClient
# The current version number.
VERSION = '0.14.0'
VERSION = '1.0.0'
end
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
# frozen_string_literal: true

class TCPClient
module IOWithDeadlineMixin
class << self
private

def included(mod)
return if defined?(mod.wait_writable) && defined?(mod.wait_readable)
mod.include(defined?(mod.to_io) ? WaitWithIO : WaitWithSelect)
end
end

module WithDeadline
def read_with_deadline(nbytes, deadline, exception)
raise(exception) unless deadline.remaining_time
return fetch_avail(deadline, exception) if nbytes.nil?
Expand Down Expand Up @@ -79,10 +70,10 @@ def with_deadline(deadline, exception)
case ret = yield
when :wait_writable
remaining_time = deadline.remaining_time or raise(exception)
wait_writable(remaining_time) or raise(exception)
wait_write[remaining_time] or raise(exception)
when :wait_readable
remaining_time = deadline.remaining_time or raise(exception)
wait_readable(remaining_time) or raise(exception)
wait_read[remaining_time] or raise(exception)
else
return ret
end
Expand All @@ -91,18 +82,28 @@ def with_deadline(deadline, exception)
raise(exception)
end

module WaitWithIO
def wait_writable(time) = to_io.wait_writable(time)
def wait_readable(time) = to_io.wait_readable(time)
def wait_write
@wait_write ||=
if defined?(wait_writable)
->(t) { wait_writable(t) }
elsif defined?(to_io)
->(t) { to_io.wait_writable(t) }
else
->(t) { ::IO.select(nil, [self], nil, t) }
end
end

module WaitWithSelect
def wait_writable(time) = ::IO.select(nil, [self], nil, time)
def wait_readable(time) = ::IO.select([self], nil, nil, time)
def wait_read
@wait_read ||=
if defined?(wait_readable)
->(t) { wait_readable(t) }
elsif defined?(to_io)
->(t) { to_io.wait_readable(t) }
else
->(t) { ::IO.select([self], nil, nil, t) }
end
end

private_constant(:WaitWithIO, :WaitWithSelect)
end

private_constant(:IOWithDeadlineMixin)
private_constant(:WithDeadline)
end
Loading