Skip to content

Commit

Permalink
Use Loom friendly locking
Browse files Browse the repository at this point in the history
  • Loading branch information
malkusch committed Nov 6, 2023
1 parent 3d3db3c commit 350ee96
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/main/java/de/malkusch/km200/http/SerializedHttp.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,38 @@
package de.malkusch.km200.http;

import java.io.IOException;
import java.util.concurrent.locks.ReentrantLock;

import de.malkusch.km200.KM200Exception;

public final class SerializedHttp implements Http {

private final Http http;
private final ReentrantLock lock = new ReentrantLock();

public SerializedHttp(Http http) {
this.http = http;
}

private final Object lock = new Object();

@Override
public Response get(String path) throws IOException, InterruptedException, KM200Exception {
synchronized (lock) {
lock.lockInterruptibly();
try {
return http.get(path);

} finally {
lock.unlock();
}
}

@Override
public Response post(String path, byte[] body) throws IOException, InterruptedException, KM200Exception {
synchronized (lock) {
lock.lockInterruptibly();
try {
return http.post(path, body);

} finally {
lock.unlock();
}
}
}

0 comments on commit 350ee96

Please sign in to comment.