Skip to content

Commit bf9f02b

Browse files
committed
[2.0.0-SNAPSHOT]
SemaphoreRequestQueueManager improved
1 parent 9b366c3 commit bf9f02b

38 files changed

+89
-80
lines changed

β€Ž.github/workflows/gradle.ymlβ€Ž

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,16 @@ jobs:
3232
run: ./gradlew spotlessCheck
3333

3434
- name: Test
35+
if: matrix.java == '11'
3536
run: ./gradlew test jacocoTestReport
3637
env:
37-
API_KEY: ${{ secrets.API_KEY }}
38+
API_KEY: ${{ secrets.ETHERSCAN_API_KEY_1 }}
39+
40+
- name: Test
41+
if: matrix.java == '17'
42+
run: ./gradlew test jacocoTestReport
43+
env:
44+
API_KEY: ${{ secrets.ETHERSCAN_API_KEY_2 }}
3845

3946
- name: SonarQube
4047
if: matrix.java == '17'

β€Žsrc/main/java/io/goodforgod/api/etherscan/manager/RequestQueueManager.javaβ€Ž

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,8 @@
1212
*/
1313
public interface RequestQueueManager extends AutoCloseable {
1414

15-
RequestQueueManager DEFAULT = new SemaphoreRequestQueueManager(1, Duration.ofMillis(5050L),
16-
Duration.ofMillis(5050L), 0);
17-
18-
RequestQueueManager PERSONAL = new SemaphoreRequestQueueManager(5, Duration.ofMillis(1050L),
19-
Duration.ofMillis(1050L), 5);
15+
RequestQueueManager DEFAULT = new SemaphoreRequestQueueManager(1, Duration.ofMillis(5005L));
16+
RequestQueueManager PERSONAL = new SemaphoreRequestQueueManager(5, Duration.ofMillis(1005L));
2017

2118
/**
2219
* Waits in queue for chance to take turn

β€Žsrc/main/java/io/goodforgod/api/etherscan/manager/impl/SemaphoreRequestQueueManager.javaβ€Ž

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,10 @@ public final class SemaphoreRequestQueueManager implements RequestQueueManager,
2121
private final long queueResetTimeInMillis;
2222

2323
public SemaphoreRequestQueueManager(int size, Duration resetIn) {
24-
this(size, resetIn, resetIn);
25-
}
26-
27-
public SemaphoreRequestQueueManager(int size, Duration resetIn, Duration delayIn) {
28-
this(size, resetIn, delayIn, size);
29-
}
30-
31-
public SemaphoreRequestQueueManager(int size, Duration queueResetTimeIn, Duration delayIn, int initialSize) {
32-
this.semaphore = new Semaphore(initialSize);
33-
this.queueResetTimeInMillis = queueResetTimeIn.toMillis();
34-
this.executorService.scheduleAtFixedRate(releaseLocks(size + 1),
35-
delayIn.toMillis(), queueResetTimeInMillis, TimeUnit.MILLISECONDS);
24+
this.semaphore = new Semaphore(0);
25+
this.queueResetTimeInMillis = resetIn.toMillis();
26+
this.executorService.scheduleAtFixedRate(releaseLocks(size),
27+
resetIn.toMillis(), queueResetTimeInMillis, TimeUnit.MILLISECONDS);
3628
}
3729

3830
@SuppressWarnings("java:S899")
@@ -46,7 +38,13 @@ public void takeTurn() {
4638
}
4739

4840
private Runnable releaseLocks(int toRelease) {
49-
return () -> semaphore.release(toRelease);
41+
return () -> {
42+
int availablePermits = semaphore.availablePermits();
43+
int neededPermits = toRelease - availablePermits;
44+
if (neededPermits > 0) {
45+
semaphore.release(neededPermits);
46+
}
47+
};
5048
}
5149

5250
@Override
Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,45 @@
11
package io.goodforgod.api.etherscan;
22

33
import io.goodforgod.api.etherscan.manager.RequestQueueManager;
4+
import java.util.Map;
45
import org.junit.jupiter.api.AfterAll;
56
import org.junit.jupiter.api.Assertions;
67

78
public class ApiRunner extends Assertions {
89

910
private static final String DEFAULT_KEY = "YourApiKeyToken";
1011

11-
private static final EtherScanAPI api;
12-
private static final String apiKey;
12+
private static final String API_KEY;
13+
private static final EtherScanAPI API;
1314

1415
static {
15-
final String key = System.getenv("API_KEY");
16-
final RequestQueueManager queueManager = RequestQueueManager.DEFAULT;
17-
18-
apiKey = (key == null || key.isEmpty())
19-
? DEFAULT_KEY
20-
: key;
21-
api = EtherScanAPI.builder().withApiKey(ApiRunner.apiKey).withNetwork(EthNetworks.MAINNET).withQueue(queueManager)
16+
API_KEY = System.getenv().entrySet().stream()
17+
.filter(e -> e.getKey().startsWith("ETHERSCAN_API_KEY"))
18+
.map(Map.Entry::getValue)
19+
.findFirst()
20+
.orElse(DEFAULT_KEY);
21+
22+
final RequestQueueManager queueManager = (DEFAULT_KEY.equals(API_KEY))
23+
? RequestQueueManager.DEFAULT
24+
: RequestQueueManager.PERSONAL;
25+
26+
API = EtherScanAPI.builder()
27+
.withApiKey(ApiRunner.API_KEY)
28+
.withNetwork(EthNetworks.MAINNET)
29+
.withQueue(queueManager)
2230
.build();
2331
}
2432

2533
public static String getApiKey() {
26-
return apiKey;
34+
return API_KEY;
2735
}
2836

2937
public static EtherScanAPI getApi() {
30-
return api;
38+
return API;
3139
}
3240

3341
@AfterAll
3442
public static void cleanup() throws Exception {
35-
api.close();
43+
API.close();
3644
}
3745
}

β€Žsrc/test/java/io/goodforgod/api/etherscan/EtherScanAPITests.javaβ€Ž

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import io.goodforgod.api.etherscan.http.EthHttpClient;
66
import io.goodforgod.api.etherscan.http.impl.UrlEthHttpClient;
77
import io.goodforgod.api.etherscan.model.Balance;
8+
import java.net.URI;
89
import java.time.Duration;
910
import java.util.concurrent.TimeUnit;
1011
import java.util.function.Supplier;
@@ -16,7 +17,7 @@
1617
*/
1718
class EtherScanAPITests extends ApiRunner {
1819

19-
private final EthNetworks network = EthNetworks.KOVAN;
20+
private final EthNetworks network = EthNetworks.SEPOLIA;
2021

2122
@Test
2223
void validKey() {
@@ -46,14 +47,12 @@ void noTimeoutOnRead() {
4647

4748
@Test
4849
void noTimeoutOnReadGroli() {
49-
Supplier<EthHttpClient> supplier = () -> new UrlEthHttpClient(Duration.ofMillis(300));
5050
Balance balance = getApi().account().balance("0xF318ABc9A5a92357c4Fea8d082dade4D43e780B7");
5151
assertNotNull(balance);
5252
}
5353

5454
@Test
5555
void noTimeoutOnReadTobalala() {
56-
Supplier<EthHttpClient> supplier = () -> new UrlEthHttpClient(Duration.ofMillis(30000));
5756
Balance balance = getApi().account().balance("0xF318ABc9A5a92357c4Fea8d082dade4D43e780B7");
5857
assertNotNull(balance);
5958
}
@@ -68,8 +67,12 @@ void noTimeoutUnlimitedAwait() {
6867
void timeout() throws InterruptedException {
6968
TimeUnit.SECONDS.sleep(5);
7069
Supplier<EthHttpClient> supplier = () -> new UrlEthHttpClient(Duration.ofMillis(300), Duration.ofMillis(300));
71-
EtherScanAPI api = EtherScanAPI.builder().withApiKey(getApiKey()).withNetwork(EthNetworks.KOVAN).withHttpClient(supplier)
70+
EtherScanAPI api = EtherScanAPI.builder()
71+
.withApiKey(getApiKey())
72+
.withNetwork(() -> URI.create("https://api-unknown.etherscan.io/api"))
73+
.withHttpClient(supplier)
7274
.build();
75+
7376
assertThrows(EtherScanConnectionException.class,
7477
() -> api.account().blocksMined("0x0010f94b296A852aAac52EA6c5Ac72e03afD032D"));
7578
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* @author GoodforGod
1414
* @since 03.11.2018
1515
*/
16-
class AccountBalanceListTest extends ApiRunner {
16+
class AccountBalanceListTests extends ApiRunner {
1717

1818
@Test
1919
void correct() {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* @author GoodforGod
1111
* @since 03.11.2018
1212
*/
13-
class AccountBalanceTest extends ApiRunner {
13+
class AccountBalanceTests extends ApiRunner {
1414

1515
private final EtherScanAPI api = getApi();
1616

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* @author GoodforGod
1212
* @since 03.11.2018
1313
*/
14-
class AccountMinedBlocksTest extends ApiRunner {
14+
class AccountMinedBlocksTests extends ApiRunner {
1515

1616
private final EtherScanAPI api = getApi();
1717

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* @author GoodforGod
1111
* @since 03.11.2018
1212
*/
13-
class AccountTokenBalanceTest extends ApiRunner {
13+
class AccountTokenBalanceTests extends ApiRunner {
1414

1515
private final EtherScanAPI api = getApi();
1616

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* @author GoodforGod
1111
* @since 03.11.2018
1212
*/
13-
class AccountTxErc20Test extends ApiRunner {
13+
class AccountTxErc20Tests extends ApiRunner {
1414

1515
@Test
1616
void correct() {

0 commit comments

Comments
Β (0)