This repository has been archived by the owner on Jul 15, 2022. It is now read-only.
forked from SpigotMC/BungeeCord
-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/SpigotMC/BungeeCord into …
…SpigotMC-master
- Loading branch information
Showing
7 changed files
with
48 additions
and
17 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
30 changes: 23 additions & 7 deletions
30
proxy/src/main/java/net/md_5/bungee/ConnectionThrottle.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 |
---|---|---|
@@ -1,29 +1,45 @@ | ||
package net.md_5.bungee; | ||
|
||
import com.google.common.cache.Cache; | ||
import com.google.common.cache.CacheBuilder; | ||
import com.google.common.cache.CacheLoader; | ||
import com.google.common.cache.LoadingCache; | ||
import java.net.InetAddress; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
public class ConnectionThrottle | ||
{ | ||
|
||
private final Cache<InetAddress, Boolean> throttle; | ||
private final LoadingCache<InetAddress, Integer> throttle; | ||
private final int throttleLimit; | ||
|
||
public ConnectionThrottle(int throttleTime) | ||
public ConnectionThrottle(int throttleTime, int throttleLimit) | ||
{ | ||
this.throttle = CacheBuilder.newBuilder() | ||
.concurrencyLevel( Runtime.getRuntime().availableProcessors() ) | ||
.initialCapacity( 100 ) | ||
.expireAfterWrite( throttleTime, TimeUnit.MILLISECONDS ) | ||
.build(); | ||
.build( new CacheLoader<InetAddress, Integer>() | ||
{ | ||
@Override | ||
public Integer load(InetAddress key) throws Exception | ||
{ | ||
return 0; | ||
} | ||
} ); | ||
this.throttleLimit = throttleLimit; | ||
} | ||
|
||
public void unthrottle(InetAddress address) | ||
{ | ||
int throttleCount = throttle.getUnchecked( address ) - 1; | ||
throttle.put( address, throttleCount ); | ||
} | ||
|
||
public boolean throttle(InetAddress address) | ||
{ | ||
boolean isThrottled = throttle.getIfPresent( address ) != null; | ||
throttle.put( address, true ); | ||
int throttleCount = throttle.getUnchecked( address ) + 1; | ||
throttle.put( address, throttleCount ); | ||
|
||
return isThrottled; | ||
return throttleCount > throttleLimit; | ||
} | ||
} |
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
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
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