Skip to content

Commit

Permalink
Serialize requests
Browse files Browse the repository at this point in the history
The KM200 itself is not thread safe. To protect users against wrong
usage, this API will serialize all requests.
  • Loading branch information
malkusch committed Nov 6, 2023
1 parent 7d0e22e commit ad9cc52
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 18 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,10 @@ km200.endpoints().forEach(System.out::println);

### Thread safety

Code wise the API is thread safe, however your KM200 itself might not be. I did observe issues when querying my KM200 concurrently. It performed badly and with errors. It is advised to use this API not concurrently.
Code wise this API is thread safe, it is highly recommended to not
use it concurrently. Your KM200 gateway itself is not thread safe. In order
to protect users from wrong usage, this API will serialize all requests, i.e.
concurrent requests will not happen concurrently.

## License

Expand Down
44 changes: 27 additions & 17 deletions src/main/java/de/malkusch/km200/KM200.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,10 @@
* }
* </pre>
*
* Although code wise this class is thread safe, it is highly recommended to not
* use it concurrently. Chances are that your KM200 gateway itself is not thread
* safe.
* Code wise this class is thread safe, it is highly recommended to not use it
* concurrently. Your KM200 gateway itself is not thread safe. In order to
* protect users from wrong usage, this API will serialize all requests, i.e.
* concurrent requests will not happen concurrently.
*/
public final class KM200 {

Expand Down Expand Up @@ -96,8 +97,9 @@ public KM200(String uri, Duration timeout, String gatewayPassword, String privat
* The base URI of your KM200 e.g. http://192.168.0.44
* @param retries
* The amount of retries. Set to {@link #RETRY_DISABLED} to
* disable retrying. Retries add a waiting delay between each retry
* of several seconds, because the km200 recovers very slowly.
* disable retrying. Retries add a waiting delay between each
* retry of several seconds, because the km200 recovers very
* slowly.
* @param timeout
* An IO timeout for individual requests to your heater. Retries
* might block the API longer than this timeout.
Expand Down Expand Up @@ -249,23 +251,31 @@ private <T> HttpResponse<T> sendWithRetries(FailsafeExecutor<HttpResponse<T>> re
}
}

private final Object serializedSendLock = new Object();

private <T> HttpResponse<T> send(HttpRequest request, BodyHandler<T> bodyHandler)
throws IOException, InterruptedException, KM200Exception {

var response = http.send(request, bodyHandler);
var status = response.statusCode();
/*
* The KM200 itself is not thread safe. This lock serializes all
* requests to protect users from a wrong concurrent usage of this API.
*/
synchronized (serializedSendLock) {
var response = http.send(request, bodyHandler);
var status = response.statusCode();

if (status >= 200 && status <= 299) {
return response;
if (status >= 200 && status <= 299) {
return response;

} else {
throw switch (status) {
case 403 -> new KM200Exception.Forbidden(request.uri() + " is forbidden");
case 404 -> new KM200Exception.NotFound(request.uri() + " was not found");
case 423 -> new KM200Exception.Locked(request.uri() + " was locked");
case 500 -> new KM200Exception.ServerError(request.uri() + " resulted in a server error");
default -> new KM200Exception(request.uri() + " failed with response code " + status);
};
} else {
throw switch (status) {
case 403 -> new KM200Exception.Forbidden(request.uri() + " is forbidden");
case 404 -> new KM200Exception.NotFound(request.uri() + " was not found");
case 423 -> new KM200Exception.Locked(request.uri() + " was locked");
case 500 -> new KM200Exception.ServerError(request.uri() + " resulted in a server error");
default -> new KM200Exception(request.uri() + " failed with response code " + status);
};
}
}
}

Expand Down

0 comments on commit ad9cc52

Please sign in to comment.