Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/lann/tcp-timeout-ruby
Ruby TCPSocket-alike with timeouts, avoiding Timeout.timeout
https://github.com/lann/tcp-timeout-ruby
Last synced: 20 days ago
JSON representation
Ruby TCPSocket-alike with timeouts, avoiding Timeout.timeout
- Host: GitHub
- URL: https://github.com/lann/tcp-timeout-ruby
- Owner: lann
- License: mit
- Created: 2013-05-11T18:47:23.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2016-12-08T03:10:29.000Z (almost 8 years ago)
- Last Synced: 2024-10-14T13:49:04.868Z (about 1 month ago)
- Language: Ruby
- Homepage:
- Size: 5.86 KB
- Stars: 17
- Watchers: 3
- Forks: 8
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# TCPTimeout
A wrapper around Ruby Sockets providing timeouts for connect, write, and read
operations using `Socket#*_nonblock` methods and `IO.select` instead of
`Timeout.timeout`.## Usage
`gem install tcp_timeout`
Pass one or more of `:connect_timeout`, `:write_timeout`, and `:read_timeout`
as options to TCPTimeout::TCPSocket.new. If a timeout is omitted or nil, that
operation will behave as a normal Socket would. On timeout, a
`TCPTimeout::SocketTimeout` (subclass of `SocketError`) will be raised.When calling `#read` with a byte length it is possible for it to read some data
before timing out. If you need to avoid losing this data you can pass a buffer
string which will receive the data even after a timeout.Other options:
- `:family` - set the address family for the connection, e.g. `:INET` or `:INET6`
- `:local_host` and `:local_port` - the host and port to bind toTCPTimeout::TCPSocket supports only a subset of IO methods, including:
```close closed? read read_nonblock readbyte readpartial write write_nonblock```
**Example:**
```ruby
begin
sock = TCPTimeout::TCPSocket.new(host, port, connect_timeout: 10, write_timeout: 9)
sock.write('data')
sock.close
rescue TCPTimeout::SocketTimeout
puts "Operation timed out!"
end
```