Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitbf9f02b

Browse files
committed
[2.0.0-SNAPSHOT]
SemaphoreRequestQueueManager improved
1 parent9b366c3 commitbf9f02b

File tree

38 files changed

+89
-80
lines changed

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
publicinterfaceRequestQueueManagerextendsAutoCloseable {
1414

15-
RequestQueueManagerDEFAULT =newSemaphoreRequestQueueManager(1,Duration.ofMillis(5050L),
16-
Duration.ofMillis(5050L),0);
17-
18-
RequestQueueManagerPERSONAL =newSemaphoreRequestQueueManager(5,Duration.ofMillis(1050L),
19-
Duration.ofMillis(1050L),5);
15+
RequestQueueManagerDEFAULT =newSemaphoreRequestQueueManager(1,Duration.ofMillis(5005L));
16+
RequestQueueManagerPERSONAL =newSemaphoreRequestQueueManager(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
privatefinallongqueueResetTimeInMillis;
2222

2323
publicSemaphoreRequestQueueManager(intsize,DurationresetIn) {
24-
this(size,resetIn,resetIn);
25-
}
26-
27-
publicSemaphoreRequestQueueManager(intsize,DurationresetIn,DurationdelayIn) {
28-
this(size,resetIn,delayIn,size);
29-
}
30-
31-
publicSemaphoreRequestQueueManager(intsize,DurationqueueResetTimeIn,DurationdelayIn,intinitialSize) {
32-
this.semaphore =newSemaphore(initialSize);
33-
this.queueResetTimeInMillis =queueResetTimeIn.toMillis();
34-
this.executorService.scheduleAtFixedRate(releaseLocks(size +1),
35-
delayIn.toMillis(),queueResetTimeInMillis,TimeUnit.MILLISECONDS);
24+
this.semaphore =newSemaphore(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
privateRunnablereleaseLocks(inttoRelease) {
49-
return () ->semaphore.release(toRelease);
41+
return () -> {
42+
intavailablePermits =semaphore.availablePermits();
43+
intneededPermits =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
packageio.goodforgod.api.etherscan;
22

33
importio.goodforgod.api.etherscan.manager.RequestQueueManager;
4+
importjava.util.Map;
45
importorg.junit.jupiter.api.AfterAll;
56
importorg.junit.jupiter.api.Assertions;
67

78
publicclassApiRunnerextendsAssertions {
89

910
privatestaticfinalStringDEFAULT_KEY ="YourApiKeyToken";
1011

11-
privatestaticfinalEtherScanAPIapi;
12-
privatestaticfinalStringapiKey;
12+
privatestaticfinalStringAPI_KEY;
13+
privatestaticfinalEtherScanAPIAPI;
1314

1415
static {
15-
finalStringkey =System.getenv("API_KEY");
16-
finalRequestQueueManagerqueueManager =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+
finalRequestQueueManagerqueueManager = (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
publicstaticStringgetApiKey() {
26-
returnapiKey;
34+
returnAPI_KEY;
2735
}
2836

2937
publicstaticEtherScanAPIgetApi() {
30-
returnapi;
38+
returnAPI;
3139
}
3240

3341
@AfterAll
3442
publicstaticvoidcleanup()throwsException {
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
importio.goodforgod.api.etherscan.http.EthHttpClient;
66
importio.goodforgod.api.etherscan.http.impl.UrlEthHttpClient;
77
importio.goodforgod.api.etherscan.model.Balance;
8+
importjava.net.URI;
89
importjava.time.Duration;
910
importjava.util.concurrent.TimeUnit;
1011
importjava.util.function.Supplier;
@@ -16,7 +17,7 @@
1617
*/
1718
classEtherScanAPITestsextendsApiRunner {
1819

19-
privatefinalEthNetworksnetwork =EthNetworks.KOVAN;
20+
privatefinalEthNetworksnetwork =EthNetworks.SEPOLIA;
2021

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

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

5454
@Test
5555
voidnoTimeoutOnReadTobalala() {
56-
Supplier<EthHttpClient>supplier = () ->newUrlEthHttpClient(Duration.ofMillis(30000));
5756
Balancebalance =getApi().account().balance("0xF318ABc9A5a92357c4Fea8d082dade4D43e780B7");
5857
assertNotNull(balance);
5958
}
@@ -68,8 +67,12 @@ void noTimeoutUnlimitedAwait() {
6867
voidtimeout()throwsInterruptedException {
6968
TimeUnit.SECONDS.sleep(5);
7069
Supplier<EthHttpClient>supplier = () ->newUrlEthHttpClient(Duration.ofMillis(300),Duration.ofMillis(300));
71-
EtherScanAPIapi =EtherScanAPI.builder().withApiKey(getApiKey()).withNetwork(EthNetworks.KOVAN).withHttpClient(supplier)
70+
EtherScanAPIapi =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
}

‎src/test/java/io/goodforgod/api/etherscan/account/AccountBalanceListTest.java‎renamed to ‎src/test/java/io/goodforgod/api/etherscan/account/AccountBalanceListTests.java‎

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-
classAccountBalanceListTestextendsApiRunner {
16+
classAccountBalanceListTestsextendsApiRunner {
1717

1818
@Test
1919
voidcorrect() {

‎src/test/java/io/goodforgod/api/etherscan/account/AccountBalanceTest.java‎renamed to ‎src/test/java/io/goodforgod/api/etherscan/account/AccountBalanceTests.java‎

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-
classAccountBalanceTestextendsApiRunner {
13+
classAccountBalanceTestsextendsApiRunner {
1414

1515
privatefinalEtherScanAPIapi =getApi();
1616

‎src/test/java/io/goodforgod/api/etherscan/account/AccountMinedBlocksTest.java‎renamed to ‎src/test/java/io/goodforgod/api/etherscan/account/AccountMinedBlocksTests.java‎

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-
classAccountMinedBlocksTestextendsApiRunner {
14+
classAccountMinedBlocksTestsextendsApiRunner {
1515

1616
privatefinalEtherScanAPIapi =getApi();
1717

‎src/test/java/io/goodforgod/api/etherscan/account/AccountTokenBalanceTest.java‎renamed to ‎src/test/java/io/goodforgod/api/etherscan/account/AccountTokenBalanceTests.java‎

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-
classAccountTokenBalanceTestextendsApiRunner {
13+
classAccountTokenBalanceTestsextendsApiRunner {
1414

1515
privatefinalEtherScanAPIapi =getApi();
1616

‎src/test/java/io/goodforgod/api/etherscan/account/AccountTxErc20Test.java‎renamed to ‎src/test/java/io/goodforgod/api/etherscan/account/AccountTxErc20Tests.java‎

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-
classAccountTxErc20TestextendsApiRunner {
13+
classAccountTxErc20TestsextendsApiRunner {
1414

1515
@Test
1616
voidcorrect() {

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp