Skip to content
This repository has been archived by the owner on Aug 29, 2024. It is now read-only.

Commit

Permalink
Add methods for generating shared endpoints, consistent with `io-endp…
Browse files Browse the repository at this point in the history
…oint`. (#77)
  • Loading branch information
ioquatix authored Jan 22, 2024
1 parent 0db44c2 commit 57f8174
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 20 deletions.
17 changes: 0 additions & 17 deletions lib/async/io/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,23 +78,6 @@ def accept(backlog = Socket::SOMAXCONN, &block)
end
end

# Map all endpoints by invoking `#bind`.
# @yield the bound wrapper.
def bound
wrappers = []

self.each do |endpoint|
wrapper = endpoint.bind
wrappers << wrapper

yield wrapper
end

return wrappers
ensure
wrappers.each(&:close) if $!
end

# Create an Endpoint instance by URI scheme. The host and port of the URI will be passed to the Endpoint factory method, along with any options.
#
# @param string [String] URI as string. Scheme will decide implementation used.
Expand Down
16 changes: 14 additions & 2 deletions lib/async/io/shared_endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ module IO
# Pre-connect and pre-bind sockets so that it can be used between processes.
class SharedEndpoint < Endpoint
# Create a new `SharedEndpoint` by binding to the given endpoint.
def self.bound(endpoint, backlog: Socket::SOMAXCONN, close_on_exec: false)
wrappers = endpoint.bound do |server|
def self.bound(endpoint, backlog: Socket::SOMAXCONN, close_on_exec: false, **options)
sockets = Array(endpoint.bind(**options))

wrappers = sockets.each do |server|
# This is somewhat optional. We want to have a generic interface as much as possible so that users of this interface can just call it without knowing a lot of internal details. Therefore, we ignore errors here if it's because the underlying socket does not support the operation.
begin
server.listen(backlog)
Expand Down Expand Up @@ -117,5 +119,15 @@ def to_s
"\#<#{self.class} #{@wrappers.size} descriptors for #{@endpoint}>"
end
end

class Endpoint
def bound(**options)
SharedEndpoint.bound(self, **options)
end

def connected(**options)
SharedEndpoint.connected(self, **options)
end
end
end
end
4 changes: 3 additions & 1 deletion lib/async/io/ssl_endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ def bind
yield SSLServer.new(server, context)
end
else
return SSLServer.new(@endpoint.bind, context)
@endpoint.bind.map do |server|
SSLServer.new(server, context)
end
end
end

Expand Down

0 comments on commit 57f8174

Please sign in to comment.