Skip to content
This repository has been archived by the owner on Oct 30, 2023. It is now read-only.

Error handling and shutdown improvements #49

Merged

Conversation

big-andy-coates
Copy link

@big-andy-coates big-andy-coates commented Jan 25, 2018

This PR should hopefully improve the error handling in the client and allow users to shutdown the client and/or a active web socket.

  • Optional onFailure added to BinanceApiCallback to allow user to opt-in to receiving failure info, (Note: the interfacer remains a functional one for simple use-cases, it just now also supports error handling for when you care about such things).
  • BinanceApiWebSocketClient enhanced to return Closeables from calls creating web sockets, so that said web sockets can be later closed, if needed.
  • BinanceApiWebSocketClient enhanced to be Closeable itself, closing the internal OKHttp dispatcher.
  • BinanceApiWebSocketListener no longer throws an exception from onFailure, as this was just being thrown up the stack to the Thread.uncaughtExceptionHandler.

With these changes I can now:

  • detect failures on the web socket and choose to do something about them.
  • shutdown the BinanceApiWebSocketClient, and potentially recreate later, without any resource leaks or references being held to my callback objects.

… and/or a active web socket.

- Optional `onFailure` added to `BinanceApiCallback` to allow user to opt-in to receiving failure info.
- `BinanceApiWebSocketClient` enhanced to return `Closeable`s from calls creating web sockets, so that said web sockets can be later closed, if needed.
- `BinanceApiWebSocketClient` enhanced to be `Closeable` itself, closing the internal OKHttp dispatcher.
- `BinanceApiWebSocketListener` no longer throws an exception from `onFailure`, as this was just being thrown up the stack to the `Thread.uncaughtExceptionHandler`.

With these changes I can now:
- detect failures on the web socket and choose to do something about them.
- shutdown the `BinanceApiWebSocketClient`, and potentially recreate later, without any resource leaks or references being held to my callback objects.
@big-andy-coates big-andy-coates changed the title Improvements to error handling and the ability to shutdown the client… Error handling and shutdown improvements Jan 25, 2018
Copy link

@xezon xezon left a comment

Choose a reason for hiding this comment

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

Very nice.

client.newWebSocket(request, listener);
final WebSocket webSocket = client.newWebSocket(request, listener);
return () -> {
final int code = 1000;
Copy link

Choose a reason for hiding this comment

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

Any particular reason for code value 1000 ?

Copy link
Author

Choose a reason for hiding this comment

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

1000 is the status code for a normal ws shutdown, basically telling the server 'Thanks, I'm done'.

@Override
public void onFailure(WebSocket webSocket, Throwable t, Response response) {
throw new BinanceApiException(t);
if (!closing) {
Copy link

Choose a reason for hiding this comment

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

I wonder if there should still be some sort of notification to inform about the closure, because a regular API user might not see the closure of the socket, leaving him wondering why his application no longer works after a time.

Copy link
Author

Choose a reason for hiding this comment

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

Unfortunately, without this, when the user calls close on the web socket, they user would receive a EOFException on this onFailure callback. This is not intuitive IMHO.

The flag stops the EOFException that occurs in response to the user's close request.

Copy link

Choose a reason for hiding this comment

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

Yea but if the user does not close it and does not check onFailure, then the socket closes unnoticed.

String streamingUrl = String.format("%s/%s", BinanceApiConstants.WS_API_BASE_URL, channel);
Request request = new Request.Builder().url(streamingUrl).build();
client.newWebSocket(request, listener);
final WebSocket webSocket = client.newWebSocket(request, listener);
return () -> {
Copy link

Choose a reason for hiding this comment

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

When is this function object called? Or how to use it?

Copy link
Author

Choose a reason for hiding this comment

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

This is used by the clients of the library. They can call close on the returned Closeable to close the websocket.

@big-andy-coates
Copy link
Author

Hey @xezon - is there anything stopping this being merged?

@xezon
Copy link

xezon commented Feb 1, 2018

I am not the owner of this repository, so I certainly don't block anything.

@mcourteaux
Copy link

I'll join this conversation, as I'm also stumbling upon the EOFExceptions. I think that if you want to close the client, that you need to close the Websockets, as they live on their own thread. Simply doing:

public void close() {
   client.dispatcher().executorService().shutdown();
}

won't exactly close the streams. This will cause the ExecutorService (which is a standard Java API class) to stop accepting new tasks (Callable).

@mcourteaux
Copy link

We will be using your PR coming days. If any trouble arises, I'll get back here.

@big-andy-coates
Copy link
Author

big-andy-coates commented Feb 1, 2018

Hi @mcourteaux, I'd agree. The websocket is closed when the following line is invoked:

webSocket.close(code, null);

Which is invoked here.

The call to shutdown the executor service, which you call out above, was not added as part of this PR, but which is part of the documented way of shutting down the OkHttp client.

@big-andy-coates
Copy link
Author

I am not the owner of this repository, so I certainly don't block anything.

Ah, fair enough @xezon - appreciate the review!

@big-andy-coates
Copy link
Author

Hey @joaopsilva - any chance of a review please?

@lange88
Copy link

lange88 commented Feb 3, 2018

Hi @datalorax I ran into EOF exceptions as well and I am using your solution to restart the websockets after they are closed.

I am running into another problem though, after ~16 hours of streaming the web sockets are no longer receiving any data (no exceptions are thrown, the data just stops). I would like to be able to close the current web socket and start a new one. Any idea how I would go about doing that? I have tried dereferencing the BinanceApiWebSocketClient object, calling the GC and creating a new object, but that does not work. Maybe it would be nice to add manual shutdown functionality as well?

Edit: I think I managed already, the BinanceApiWebSocketClient interface functions return Closeables which you can use to close the websockets.

@big-andy-coates
Copy link
Author

big-andy-coates commented Feb 4, 2018

Edit: I think I managed already, the BinanceApiWebSocketClient interface functions return Closeables which you can use to close the websockets.

Yep, that's correct. I've added Javadocs to BinanceApiWebSocketClient to make this more clear.

-Added Java docs to make it clear you can close the websocket by calling close on the returned `Closeable`
@joaopsilva
Copy link
Member

@datalorax @xezon I very much appreciate your efforts here, it looks good, but I need some more time to review these changes properly, thank you for understanding.

@big-andy-coates
Copy link
Author

@jaggedsoft sure, no problem. Anything I can do to help, (better docs, clarifications, etc), just ask.

@big-andy-coates
Copy link
Author

@jaggedsoft any idea when/if this can be merged?

@joaopsilva joaopsilva merged commit c64cee1 into binance-exchange:master Feb 23, 2018
@joaopsilva
Copy link
Member

Thanks @big-andy-coates.

@big-andy-coates big-andy-coates deleted the error_handling_and_shutdown branch February 23, 2018 13:53
@big-andy-coates
Copy link
Author

@joaopsilva thanks the review!

nilswx pushed a commit to nilswx/binance-java-api that referenced this pull request Dec 14, 2018
…dling_and_shutdown

Error handling and shutdown improvements.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants