8000 Add methods for generating shared endpoints, consistent with `io-endp… · socketry/async-io@57f8174 · GitHub
[go: up one dir, main page]

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

Commit 57f8174

Browse files
authored
Add methods for generating shared endpoints, consistent with io-endpoint. (#77)
1 parent 0db44c2 commit 57f8174

File tree

3 files changed

+17
-20
lines changed

3 files changed

+17
-20
lines changed

lib/async/io/endpoint.rb

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -78,23 +78,6 @@ def accept(backlog = Socket::SOMAXCONN, &block)
7878
end
7979
end
8080

81-
# Map all endpoints by invoking `#bind`.
82-
# @yield the bound wrapper.
83-
def bound
84-
wrappers = []
85-
86-
self.each do |endpoint|
87-
wrapper = endpoint.bind
88-
wrappers << wrapper
89-
90-
yield wrapper
91-
end
92-
93-
return wrappers
94-
ensure
95-
wrappers.each(&:close) if $!
96-
end
97-
9881
# 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.
9982
#
10083
# @param string [String] URI as string. Scheme will decide implementation used.

lib/async/io/shared_endpoint.rb

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ module IO
1212
# Pre-connect and pre-bind sockets so that it can be used between processes.
1313
class SharedEndpoint < Endpoint
1414
# Create a new `SharedEndpoint` by binding to the given endpoint.
15-
def self.bound(endpoint, backlog: Socket::SOMAXCONN, close_on_exec: false)
16-
wrappers = endpoint.bound do |server|
15+
def self.bound(endpoint, backlog: Socket::SOMAXCONN, close_on_exec: false, **options)
16+
sockets = Array(endpoint.bind(**options))
17+
18+
wrappers = sockets.each do |server|
1719
# 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.
1820
begin
1921
server.listen(backlog)
@@ -117,5 +119,15 @@ def to_s
117119
"\#<#{self.class} #{@wrappers.size} descriptors for #{@endpoint}>"
118120
end
119121
end
122+
123+
class Endpoint
124+
def bound(**options)
125+
SharedEndpoint.bound(self, **options)
126+
end
127+
128+
def connected(**options)
129+
SharedEndpoint.connected(self, **options)
130+
end
131+
end
120132
end
121133
end

lib/async/io/ssl_endpoint.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,9 @@ def bind
6565
yield SSLServer.new(server, context)
6666
end
6767
else
68-
return SSLServer.new(@endpoint.bind, context)
68+
@endpoint.bind.map do |server|
69+
SSLServer.new(server, context)
70+
end
6971
end
7072
end
7173

0 commit comments

Comments
 (0)
0