This is an automated email from the ASF dual-hosted git repository.

toulmean pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-tuweni.git


The following commit(s) were added to refs/heads/main by this push:
     new f752636  add caching handler
     new a49cab1  Merge pull request #329 from atoulme/caching_handler
f752636 is described below

commit f7526364efafc7ff93baeb0f57100ff46be217c7
Author: Antoine Toulme <anto...@lunar-ocean.com>
AuthorDate: Fri Jul 30 21:41:50 2021 -0700

    add caching handler
---
 jsonrpc/build.gradle                               |  1 +
 .../tuweni/jsonrpc/methods/MethodsHandler.kt       | 38 ++++++++++++++++++++--
 .../tuweni/jsonrpc/methods/MethodsHandlerTest.kt   | 26 +++++++++++++++
 3 files changed, 62 insertions(+), 3 deletions(-)

diff --git a/jsonrpc/build.gradle b/jsonrpc/build.gradle
index e09379c..e56512f 100644
--- a/jsonrpc/build.gradle
+++ b/jsonrpc/build.gradle
@@ -30,6 +30,7 @@ dependencies {
   implementation project(':crypto')
   implementation project(':concurrent')
   implementation project(':eth')
+  implementation project(':kv')
   implementation project(':net')
   implementation project(':units')
 
diff --git 
a/jsonrpc/src/main/kotlin/org/apache/tuweni/jsonrpc/methods/MethodsHandler.kt 
b/jsonrpc/src/main/kotlin/org/apache/tuweni/jsonrpc/methods/MethodsHandler.kt
index 30fb39d..81cd4a0 100644
--- 
a/jsonrpc/src/main/kotlin/org/apache/tuweni/jsonrpc/methods/MethodsHandler.kt
+++ 
b/jsonrpc/src/main/kotlin/org/apache/tuweni/jsonrpc/methods/MethodsHandler.kt
@@ -20,11 +20,13 @@ import com.netflix.concurrency.limits.limit.FixedLimit
 import com.netflix.concurrency.limits.limiter.SimpleLimiter
 import io.opentelemetry.api.metrics.LongCounter
 import io.opentelemetry.api.metrics.common.Labels
+import kotlinx.coroutines.runBlocking
 import org.apache.tuweni.eth.JSONRPCRequest
 import org.apache.tuweni.eth.JSONRPCResponse
 import org.apache.tuweni.eth.methodNotEnabled
 import org.apache.tuweni.eth.methodNotFound
 import org.apache.tuweni.eth.tooManyRequests
+import org.apache.tuweni.kv.KeyValueStore
 
 class MethodsRouter(val methodsMap: Map<String, (JSONRPCRequest) -> 
JSONRPCResponse>) {
 
@@ -93,6 +95,36 @@ class ThrottlingHandler(private val threshold: Int, private 
val delegateHandler:
   }
 }
 
-// TODO DelegateHandler - choose from a number of handlers to see which to 
delegate to.
-// TODO FilterHandler - filter incoming requests per allowlist
-// TODO CachingHandler - cache some incoming requests
+class CachingHandler(private val allowedMethods: List<String>, private val 
cacheStore: KeyValueStore<String, JSONRPCResponse>, private val 
delegateHandler: (JSONRPCRequest) -> JSONRPCResponse) {
+
+  fun handleRequest(request: JSONRPCRequest): JSONRPCResponse {
+    var found = false
+    for (method in allowedMethods) {
+      if (request.method.startsWith(method)) {
+        found = true
+        break
+      }
+    }
+    if (!found) {
+      return delegateHandler(request)
+    } else {
+      val serializedRequest = serializeRequest(request)
+      return runBlocking {
+        var response = cacheStore.get(serializedRequest)
+        if (response == null) {
+          response = delegateHandler(request)
+          if (response.error == null) {
+            cacheStore.put(serializedRequest, response)
+          }
+          response
+        } else {
+          return@runBlocking response.copy(id = request.id)
+        }
+      }
+    }
+  }
+
+  private fun serializeRequest(request: JSONRPCRequest): String {
+    return request.method + "|" + request.params.joinToString(",")
+  }
+}
diff --git 
a/jsonrpc/src/test/kotlin/org/apache/tuweni/jsonrpc/methods/MethodsHandlerTest.kt
 
b/jsonrpc/src/test/kotlin/org/apache/tuweni/jsonrpc/methods/MethodsHandlerTest.kt
index 9441a10..69fb2a5 100644
--- 
a/jsonrpc/src/test/kotlin/org/apache/tuweni/jsonrpc/methods/MethodsHandlerTest.kt
+++ 
b/jsonrpc/src/test/kotlin/org/apache/tuweni/jsonrpc/methods/MethodsHandlerTest.kt
@@ -28,6 +28,7 @@ import org.apache.tuweni.eth.JSONRPCRequest
 import org.apache.tuweni.eth.JSONRPCResponse
 import org.apache.tuweni.eth.methodNotFound
 import org.apache.tuweni.junit.BouncyCastleExtension
+import org.apache.tuweni.kv.MapKeyValueStore
 import org.junit.jupiter.api.Assertions.assertEquals
 import org.junit.jupiter.api.Assertions.assertNotNull
 import org.junit.jupiter.api.Assertions.assertNull
@@ -165,3 +166,28 @@ class ThrottlingHandlerTest {
     }
   }
 }
+
+class CachingHandlerTest {
+
+  @Test
+  fun testCache() {
+    val map = HashMap<String, JSONRPCResponse>()
+    val kv = MapKeyValueStore.open(map)
+    val handler = CachingHandler(listOf("foo"), kv) {
+      if (it.params.size > 0) {
+        JSONRPCResponse(id = 1, error = JSONRPCError(1234, ""))
+      } else {
+        JSONRPCResponse(id = 1)
+      }
+    }
+    assertEquals(0, map.size)
+    handler.handleRequest(JSONRPCRequest(id = 1, method = "foo", params = 
arrayOf()))
+    assertEquals(1, map.size)
+    handler.handleRequest(JSONRPCRequest(id = 1, method = "bar", params = 
arrayOf()))
+    assertEquals(1, map.size)
+    handler.handleRequest(JSONRPCRequest(id = 1, method = "foo", params = 
arrayOf()))
+    assertEquals(1, map.size)
+    handler.handleRequest(JSONRPCRequest(id = 1, method = "foo", params = 
arrayOf("bleh")))
+    assertEquals(1, map.size)
+  }
+}

---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@tuweni.apache.org
For additional commands, e-mail: commits-h...@tuweni.apache.org

Reply via email to