-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
89 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/main/java/com/sparrowwallet/tern/http/client/DefaultHttpProxySupplier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.sparrowwallet.tern.http.client; | ||
|
||
import java.util.Optional; | ||
|
||
public class DefaultHttpProxySupplier implements IHttpProxySupplier { | ||
private final String host; | ||
private final int port; | ||
private final HttpProxy httpProxy; | ||
|
||
public DefaultHttpProxySupplier(String host, int port) { | ||
this.host = host; | ||
this.port = port; | ||
this.httpProxy = computeHttpProxy(host, port); | ||
} | ||
|
||
private HttpProxy computeHttpProxy(String host, int port) { | ||
if(host == null || host.isEmpty() || port < 0) { | ||
return null; | ||
} | ||
|
||
return new HttpProxy(HttpProxyProtocol.SOCKS, host, port); | ||
} | ||
|
||
public String getHost() { | ||
return host; | ||
} | ||
|
||
public int getPort() { | ||
return port; | ||
} | ||
|
||
@Override | ||
public Optional<HttpProxy> getHttpProxy(HttpUsage httpUsage) { | ||
return Optional.ofNullable(httpProxy); | ||
} | ||
|
||
@Override | ||
public void changeIdentity() { | ||
//not implemented | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
src/main/java/com/sparrowwallet/tern/http/client/RateLimiter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.sparrowwallet.tern.http.client; | ||
|
||
import java.util.concurrent.atomic.AtomicInteger; | ||
import java.util.concurrent.locks.ReentrantLock; | ||
|
||
public class RateLimiter { | ||
private final int maxTokens; | ||
private final long refillIntervalMillis; | ||
private final AtomicInteger tokens; | ||
private long lastRefillTimestamp; | ||
private final ReentrantLock lock = new ReentrantLock(); | ||
|
||
public RateLimiter(int maxTokens, long refillIntervalMillis) { | ||
this.maxTokens = maxTokens; | ||
this.refillIntervalMillis = refillIntervalMillis; | ||
this.tokens = new AtomicInteger(maxTokens); | ||
this.lastRefillTimestamp = System.currentTimeMillis(); | ||
} | ||
|
||
public boolean tryAcquire() { | ||
refill(); | ||
if (tokens.get() > 0) { | ||
tokens.decrementAndGet(); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
private void refill() { | ||
lock.lock(); | ||
try { | ||
long now = System.currentTimeMillis(); | ||
long elapsed = now - lastRefillTimestamp; | ||
if (elapsed > refillIntervalMillis) { | ||
int tokensToAdd = (int) (elapsed / refillIntervalMillis); | ||
tokens.set(Math.min(maxTokens, tokens.get() + tokensToAdd)); | ||
lastRefillTimestamp = now; | ||
} | ||
} finally { | ||
lock.unlock(); | ||
} | ||
} | ||
} |
44 changes: 0 additions & 44 deletions
44
src/main/java/com/sparrowwallet/tern/http/client/TorHttpProxySupplier.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters