Skip to content

Commit 5205637

Browse files
authored
Merge pull request #3311 from Watson1978/io-timeout-and-wait-priority
IO: timeout / timeout= / wait_priority と IO::TimeoutError を追加 (Ruby 3.2)
2 parents 2e3e9fc + 55bf45c commit 5205637

3 files changed

Lines changed: 79 additions & 0 deletions

File tree

manual/api/_builtin/IO.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2431,6 +2431,46 @@ p IO.new(IO.sysopen("/")).path # => "/"
24312431
p IO.new(IO.sysopen("/"), path: "foo").path # => "foo"
24322432
```
24332433

2434+
### def timeout -> Numeric | nil
2435+
2436+
self に設定されている入出力のタイムアウトを返します。
2437+
設定されていない場合は nil を返します。
2438+
2439+
タイムアウトの設定については [m:IO#timeout=] を参照してください。
2440+
2441+
```ruby title="例"
2442+
r, w = IO.pipe
2443+
p r.timeout # => nil
2444+
r.timeout = 0.5
2445+
p r.timeout # => 0.5
2446+
```
2447+
2448+
- **SEE** [m:IO#timeout=]
2449+
2450+
### def timeout=(numeric) -> Numeric
2451+
### def timeout=(nil) -> nil
2452+
2453+
self に入出力のタイムアウトを秒単位で設定します。
2454+
2455+
設定すると、可能な限りすべてのブロッキング操作にこのタイムアウトが適用されます。
2456+
操作が設定した時間を超えると [c:IO::TimeoutError] が発生します。
2457+
2458+
影響を受けるのは [m:IO#gets][m:IO#puts][m:IO#read][m:IO#write]
2459+
[m:IO#wait_readable][m:IO#wait_writable] などです
2460+
([c:Socket] のブロッキング操作にも影響します)。
2461+
2462+
- **param** `numeric` -- タイムアウトの秒数を数値で指定します。
2463+
nil を指定するとタイムアウトを解除します。
2464+
2465+
```ruby title="例"
2466+
r, w = IO.pipe
2467+
r.timeout = 0.1
2468+
2469+
r.read # ~> IO::TimeoutError
2470+
```
2471+
2472+
- **SEE** [m:IO#timeout], [c:IO::TimeoutError]
2473+
24342474
#@end
24352475

24362476
## Constants
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
library: _builtin
3+
since: "3.2"
4+
---
5+
# class IO::TimeoutError < IOError
6+
7+
入出力の操作が [m:IO#timeout=] で設定した時間を超えたときに発生します。
8+
9+
```ruby
10+
r, w = IO.pipe
11+
r.timeout = 0.1
12+
13+
r.read # ~> IO::TimeoutError
14+
```
15+
16+
- **SEE** [m:IO#timeout=], [m:IO#timeout]

manual/api/io/wait.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,26 @@ timeout を指定した場合は、指定秒数経過するまでブロックし
6969
- **param** `timeout` -- タイムアウトまでの秒数を指定します。
7070

7171
- **SEE** [m:IO#wait_readable]
72+
73+
#@since 3.0
74+
### def wait_priority(timeout = nil) -> bool | self | nil
75+
76+
self が優先データを受信して読み込み可能になるまでブロックし、
77+
読み込み可能になったら真値を返します。
78+
79+
優先データ(緊急データ)は [m:Socket::Constants::MSG_OOB] フラグを用いて
80+
送受信され、通常はストリーム型のソケットに限られます。
81+
82+
より詳しくは、一度ブロックしてから読み込み可能になった場合には
83+
self を返します。
84+
内部のバッファにデータがある場合には
85+
ブロックせずに true を返します。
86+
87+
timeout を指定した場合は、指定秒数経過するまでブロックし、タ
88+
イムアウトした場合は nil を返します。
89+
90+
- **param** `timeout` -- タイムアウトまでの秒数を指定します。
91+
nil を指定すると読み込み可能になるまで待ち続けます。
92+
93+
- **SEE** [m:IO#wait_readable], [m:IO#wait_writable]
94+
#@end

0 commit comments

Comments
 (0)