You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: lib/protocol/http/body/readable.rb
+5-1
Original file line number
Diff line number
Diff line change
@@ -17,7 +17,11 @@ module Body
17
17
#
18
18
# If you don't want to read from a stream, and instead want to close it immediately, you can call `close` on the body. If the body is already completely consumed, `close` will do nothing, but if there is still data to be read, it will cause the underlying stream to be reset (and possibly closed).
19
19
classReadable
20
-
# Close the stream immediately.
20
+
# Close the stream immediately. After invoking this method, the stream should be considered closed, and all internal resources should be released.
21
+
#
22
+
# If an error occured while handling the output, it can be passed as an argument. This may be propagated to the client, for example the client may be informed that the stream was not fully read correctly.
23
+
#
24
+
# Invoking `#read` after `#close` will return `nil`.
Copy file name to clipboardExpand all lines: lib/protocol/http/body/streamable.rb
+16-23
Original file line number
Diff line number
Diff line change
@@ -17,11 +17,14 @@ module Body
17
17
#
18
18
# When invoking `call(stream)`, the stream can be read from and written to, and closed. However, the stream is only guaranteed to be open for the duration of the `call(stream)` call. Once the method returns, the stream **should** be closed by the server.
19
19
moduleStreamable
20
+
classClosedError < StandardError
21
+
end
22
+
20
23
defself.new(*arguments)
21
24
ifarguments.size == 1
22
-
RequestBody.new(*arguments)
25
+
DeferredBody.new(*arguments)
23
26
else
24
-
ResponseBody.new(*arguments)
27
+
Body.new(*arguments)
25
28
end
26
29
end
27
30
@@ -39,8 +42,6 @@ def initialize(input, block)
39
42
@fiber=Fiber.newdo |from|
40
43
@from=from
41
44
block.call(stream)
42
-
rescueClosed
43
-
# Ignore.
44
45
ensure
45
46
@fiber=nil
46
47
@@ -58,16 +59,18 @@ def write(chunk)
58
59
@from=nil
59
60
@from=from.transfer(chunk)
60
61
else
61
-
raiseRuntimeError,"Stream is not being read!"
62
+
raiseClosedError,"Stream is not being read!"
62
63
end
63
64
end
64
65
65
66
# Can be invoked by the block to close the stream. Closing the output means that no more chunks will be generated.
66
67
defclose(error=nil)
67
68
iffrom=@from
69
+
# We are closing from within the output fiber, so we need to transfer back to `@from`:
68
70
@from=nil
69
71
from.transfer(nil)
70
72
elsif@fiber
73
+
# We are closing from outside the output fiber, so we need to resume the fiber appropriately:
71
74
@from=Fiber.current
72
75
73
76
iferror
@@ -134,20 +137,21 @@ def call(stream)
134
137
135
138
# Closing a stream indicates we are no longer interested in reading from it.
136
139
defclose(error=nil)
137
-
ifoutput=@output
138
-
@output=nil
139
-
output.close(error)
140
-
end
141
-
140
+
$stderr.puts"Closing input: #{@input.inspect}"
142
141
ifinput=@input
143
142
@input=nil
144
143
input.close(error)
145
144
end
145
+
146
+
ifoutput=@output
147
+
@output=nil
148
+
output.close(error)
149
+
end
146
150
end
147
151
end
148
152
149
-
# A request body has an extra `stream` method which can be used to stream data into the body, as the response body won't be available until the request has been sent.
150
-
classRequestBody < Body
153
+
# A deferred body has an extra `stream` method which can be used to stream data into the body, as the response body won't be available until the request has been sent.
0 commit comments