-
Notifications
You must be signed in to change notification settings - Fork 622
Error handling and shutdown improvements #49
Error handling and shutdown improvements #49
Conversation
… 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.
There was a problem hiding this 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; |
There was a problem hiding this comment.
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 ?
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 () -> { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
Hey @xezon - is there anything stopping this being merged? |
I am not the owner of this repository, so I certainly don't block anything. |
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:
won't exactly close the streams. This will cause the ExecutorService (which is a standard Java API class) to stop accepting new tasks (Callable). |
We will be using your PR coming days. If any trouble arises, I'll get back here. |
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. |
Ah, fair enough @xezon - appreciate the review! |
Hey @joaopsilva - any chance of a review please? |
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. |
Yep, that's correct. I've added Javadocs to |
-Added Java docs to make it clear you can close the websocket by calling close on the returned `Closeable`
@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. |
@jaggedsoft sure, no problem. Anything I can do to help, (better docs, clarifications, etc), just ask. |
@jaggedsoft any idea when/if this can be merged? |
Thanks @big-andy-coates. |
@joaopsilva thanks the review! |
…dling_and_shutdown Error handling and shutdown improvements.
This PR should hopefully improve the error handling in the client and allow users to shutdown the client and/or a active web socket.
onFailure
added toBinanceApiCallback
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 returnCloseable
s from calls creating web sockets, so that said web sockets can be later closed, if needed.BinanceApiWebSocketClient
enhanced to beCloseable
itself, closing the internal OKHttp dispatcher.BinanceApiWebSocketListener
no longer throws an exception fromonFailure
, as this was just being thrown up the stack to theThread.uncaughtExceptionHandler
.With these changes I can now:
BinanceApiWebSocketClient
, and potentially recreate later, without any resource leaks or references being held to my callback objects.