-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ETCM-331] Separate RateLimit validation in a different trait
- Loading branch information
1 parent
df265cb
commit 16cd9d5
Showing
1 changed file
with
25 additions
and
0 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
src/main/scala/io/iohk/ethereum/jsonrpc/server/http/RateLimit.scala
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,25 @@ | ||
package io.iohk.ethereum.jsonrpc.server.http | ||
|
||
import java.time.Clock | ||
|
||
import akka.http.scaladsl.model.RemoteAddress | ||
import com.twitter.util.LruMap | ||
import io.iohk.ethereum.jsonrpc.server.http.JsonRpcHttpServer.JsonRpcHttpServerConfig | ||
import io.iohk.ethereum.utils.Logger | ||
|
||
trait RateLimitValidator extends Logger { | ||
|
||
val config: JsonRpcHttpServerConfig | ||
|
||
val latestRequestTimestamps = new LruMap[RemoteAddress, Long](config.rateLimit.latestTimestampCacheSize) | ||
|
||
val clock: Clock = Clock.systemUTC() | ||
|
||
def validate(clientAddress: RemoteAddress):Boolean = { | ||
val timeMillis = clock.instant().toEpochMilli | ||
val latestRequestTimestamp = latestRequestTimestamps.getOrElse(clientAddress, 0L) | ||
val response = latestRequestTimestamp + config.rateLimit.minRequestInterval.toMillis < timeMillis | ||
if(response) latestRequestTimestamps.put(clientAddress, timeMillis) | ||
response | ||
} | ||
} |