Add timeout to CallBuilder.stream() method #76
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR introduces rebuilt
CallBuilder.stream()
method. The specification forEventSource
states that it should fail the connection (not attempt to reconnect) after receiving HTTP status code other than 200, 305, 401, 407, 301, 302, 303, and 307.This is problematic because when horizon is behind a proxy server, it may fail with 504 Gateway Timeout when there are no new events for a long period of time and we want to reconnect if such event occurs.
The first attempt to fix this was using
readyState
property of theEventSource
object. The problem is the value of this property does not follow the spec (ex. it does not equal CLOSED when connection is closed).In the solution proposed in this PR, we create a timeout (right now set to 15 seconds) along with a new
EventSource
object. If no messages appear, timeout function is called and restarts the connection. When new message arrives, timeout is cancelled and set again.Because of the fact that we create new
EventSource
objects thestream()
method returns theclose
function that closes the latest internalEventSource
object and cancells the timeout.Unfortunatelly, there is still a case when some events may be omited. Let's say that a user streams the latest transactions with the cursor:
now
. There is a small time window during reconnect phase when the stream is stopped. This can be solved, however, by sending someinit
message with the latestpaging_token
by horizon.Close #74