Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add timeout to CallBuilder.stream() method #76

Merged
merged 2 commits into from
Sep 12, 2016
Merged

Add timeout to CallBuilder.stream() method #76

merged 2 commits into from
Sep 12, 2016

Conversation

bartekn
Copy link
Contributor

@bartekn bartekn commented Sep 8, 2016

This PR introduces rebuilt CallBuilder.stream() method. The specification for EventSource 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 the EventSource 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 the stream() method returns the close function that closes the latest internal EventSource 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 some init message with the latest paging_token by horizon.

Close #74

@@ -128,21 +128,6 @@ describe("server.js tests", function () {
});
});
});

describe("as stream", function () {
it("attaches onmessage handler to an EventSource", function (done) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is tested in integration tests.

@coveralls
Copy link

Coverage Status

Coverage decreased (-1.8%) to 88.382% when pulling 4393281 on stream into 3e844a0 on master.

@coveralls
Copy link

Coverage Status

Coverage decreased (-1.8%) to 88.382% when pulling 4393281 on stream into 3e844a0 on master.

@nullstyle
Copy link
Contributor

+1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Eventsource polyfill doesn’t reconnect
3 participants