Skip to content
This repository was archived by the owner on May 3, 2023. It is now read-only.

Commit 1b27aad

Browse files
fix: reset connection keep cookies
1 parent dda724a commit 1b27aad

File tree

6 files changed

+66
-45
lines changed

6 files changed

+66
-45
lines changed

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@
3838
<version>RELEASE</version>
3939
<scope>test</scope>
4040
</dependency>
41+
<dependency>
42+
<groupId>org.projectlombok</groupId>
43+
<artifactId>lombok</artifactId>
44+
<version>1.18.24</version>
45+
<scope>compile</scope>
46+
</dependency>
4147
</dependencies>
4248

4349
<properties>
Lines changed: 21 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package net.httpclient.wrapper.ratelimiter;
22

3+
import lombok.Getter;
4+
import lombok.Setter;
35
import org.jetbrains.annotations.NotNull;
46
import org.jetbrains.annotations.Nullable;
57

@@ -8,7 +10,10 @@
810

911
public class RateLimiter {
1012

13+
@Getter @Setter @NotNull
1114
private Duration duration;
15+
16+
@Getter @Setter @Nullable
1217
private Instant lastAcquire;
1318

1419
/*
@@ -26,25 +31,18 @@ public RateLimiter(@NotNull Duration duration) {
2631
/**
2732
* Sleep until the duration is passed.
2833
*/
29-
public void acquire() {
30-
synchronized (this) {
31-
try {
32-
dangerousAcquire();
33-
} catch (InterruptedException e) {
34-
e.printStackTrace();
35-
}
36-
}
37-
}
38-
39-
public void dangerousAcquire() throws InterruptedException {
40-
synchronized (this) {
34+
public synchronized void acquire() {
35+
try {
4136
if (lastAcquire == null) {
4237
lastAcquire = Instant.now();
4338
return;
4439
}
45-
Duration durationToSleep = getRemainingTime();
46-
if (durationToSleep.isZero()) Thread.sleep(durationToSleep.toMillis());
40+
Duration durationToSleep = getRemainingDuration();
41+
if (!durationToSleep.isZero() && !durationToSleep.isNegative())
42+
Thread.sleep(durationToSleep.toMillis());
4743
lastAcquire = Instant.now();
44+
} catch (InterruptedException e) {
45+
e.printStackTrace();
4846
}
4947
}
5048

@@ -56,31 +54,16 @@ public void reset() {
5654
}
5755

5856
/**
59-
* Get the remaining time before the next acquire.
60-
* @return The remaining time in milliseconds.
57+
* Get the time to wait before executing the next acquire.
58+
* @return The time to wait in Duration.
6159
*/
62-
public @NotNull Duration getRemainingTime() {
63-
if (lastAcquire == null) return Duration.ZERO;
64-
long lastRequest = lastAcquire.toEpochMilli();
65-
long now = Instant.now().toEpochMilli();
66-
long result = lastRequest + duration.toMillis() - now;
67-
return (result > 0) ? Duration.ofMillis(result) : Duration.ZERO;
68-
}
69-
70-
/*
71-
$ Getters
72-
*/
73-
74-
public @NotNull Duration getDuration() {
75-
return duration;
76-
}
77-
78-
public @Nullable Instant getLastAcquire() {
79-
return lastAcquire;
80-
}
81-
82-
public void setDuration(@NotNull Duration duration) {
83-
this.duration = duration;
60+
public @NotNull Duration getRemainingDuration() {
61+
if (lastAcquire == null) return (Duration.ZERO);
62+
if (duration.isZero()) return (Duration.ZERO);
63+
Instant now = Instant.now();
64+
Instant timeToExec = lastAcquire.plus(duration);
65+
if (timeToExec.isBefore(now)) return (Duration.ZERO);
66+
return (Duration.between(now, timeToExec));
8467
}
8568

8669
}

src/main/java/net/httpclient/wrapper/response/RequestResponse.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,9 @@ public long getTime() {
7777
return (-1);
7878
return (end.getTime() - start.getTime());
7979
}
80+
81+
public String getReasonPhrase() {
82+
return (httpResponse.getStatusLine().getReasonPhrase());
83+
}
84+
8085
}

src/main/java/net/httpclient/wrapper/session/HttpClientSessionAsync.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,12 @@ public class HttpClientSessionAsync extends HttpClientSession {
4545
$ Constructor
4646
*/
4747

48+
/**
49+
* Create a new session with a default number of simultaneous requests
50+
* set to <b>1</b>.
51+
*/
4852
public HttpClientSessionAsync() {
49-
this(3);
53+
this(1);
5054
}
5155

5256
public HttpClientSessionAsync(int maxSynchronousRequest) {

src/main/java/net/httpclient/wrapper/session/HttpClientSessionRateLimited.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import net.httpclient.wrapper.response.RequestResponse;
77
import org.apache.http.NameValuePair;
88
import org.apache.http.entity.ContentType;
9+
import org.jetbrains.annotations.NotNull;
910

1011
import java.io.IOException;
1112
import java.time.Duration;
@@ -16,12 +17,12 @@ public class HttpClientSessionRateLimited extends HttpClientSession {
1617
private final RateLimiter rateLimiter;
1718

1819
public HttpClientSessionRateLimited() {
19-
this(1.0);
20+
this(Duration.ofSeconds(1));
2021
}
2122

22-
public HttpClientSessionRateLimited(double permitsPerSecond) {
23+
public HttpClientSessionRateLimited(@NotNull Duration duration) {
2324
super();
24-
rateLimiter = new RateLimiter(Duration.ofSeconds((int)permitsPerSecond));
25+
rateLimiter = new RateLimiter(duration);
2526
}
2627

2728
@Override
@@ -108,4 +109,11 @@ public RequestResponse forceSendForm(String url, List<NameValuePair> form) throw
108109
return (super.sendForm(url, form));
109110
}
110111

112+
/*
113+
$ Getters
114+
*/
115+
116+
public RateLimiter getRateLimiter() {
117+
return rateLimiter;
118+
}
111119
}

src/test/java/net/httpclient/wrapper/ratelimiter/RateLimiterTest.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,34 @@ public void constructTest() {
1717

1818
@Test
1919
public void acquireTest() {
20-
RateLimiter rateLimiter = new RateLimiter(Duration.ofSeconds(1));
20+
int timeSec = 3;
21+
RateLimiter rateLimiter = new RateLimiter(Duration.ofSeconds(timeSec));
22+
System.out.println("Start acquire test: " + Instant.now());
2123
assertDoesNotThrow(() -> rateLimiter.acquire());
24+
System.out.println("Waiting " + timeSec + " second: " + Instant.now());
2225
assertDoesNotThrow(() -> rateLimiter.acquire());
26+
System.out.println("Waiting " + timeSec + " second: " + Instant.now());
27+
assertDoesNotThrow(() -> rateLimiter.acquire());
28+
System.out.println("End of test: " + Instant.now());
2329
}
2430

2531
@Test
2632
public void getRemainingTimeTest() throws InterruptedException {
2733
int durationInSeconds = 3;
2834
RateLimiter rateLimiter = new RateLimiter(Duration.ofSeconds(durationInSeconds));
2935
rateLimiter.acquire();
30-
assertEquals(durationInSeconds, rateLimiter.getRemainingTime().toSeconds());
36+
assertEquals(durationInSeconds - 1, rateLimiter.getRemainingDuration().toSeconds());
3137
rateLimiter.reset();
32-
assertEquals(Duration.ZERO, rateLimiter.getRemainingTime());
38+
assertEquals(Duration.ZERO, rateLimiter.getRemainingDuration());
39+
}
40+
41+
@Test
42+
public void testChangeDuration() {
43+
int durationInSeconds = 1;
44+
RateLimiter rateLimiter = new RateLimiter(Duration.ofSeconds(durationInSeconds));
45+
rateLimiter.acquire();
46+
rateLimiter.setDuration(Duration.ofSeconds(2));
47+
assertEquals(2, rateLimiter.getDuration().toSeconds());
3348
}
3449

3550
}

0 commit comments

Comments
 (0)