From 1d7ef512bdbfb91478b1008b390f63a4531dd505 Mon Sep 17 00:00:00 2001 From: Rahul Rudragoudar Date: Wed, 8 Jul 2020 21:28:11 +0530 Subject: [PATCH] DB synchronisation on media endpoint (#47) * Update sql to map uuid to token Signed-off-by: Rahul Rudragoudar * Fix millis to secs conversion Signed-off-by: Rahul Rudragoudar * Add synchronisation to media enpoint DB access Signed-off-by: Rahul Rudragoudar * Change error code for rate limiter Signed-off-by: Rahul Rudragoudar --- src/main/scala/lc/Main.scala | 23 ++++++++++++++--------- src/main/scala/lc/Server.scala | 4 ++-- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/main/scala/lc/Main.scala b/src/main/scala/lc/Main.scala index 8496f2f..f03ae4b 100644 --- a/src/main/scala/lc/Main.scala +++ b/src/main/scala/lc/Main.scala @@ -40,16 +40,21 @@ class Captcha(throttle: Int) extends DBConn { def getCaptcha(id: Id): Array[Byte] = { var image :Array[Byte] = null var blob: Blob = null - imagePstmt.setString(1, id.id) - val rs: ResultSet = imagePstmt.executeQuery() - if(rs.next()){ - blob = rs.getBlob("image") - updatePstmt.setString(1,id.id) - updatePstmt.executeUpdate() + val imageOpt = imagePstmt.synchronized { + imagePstmt.setString(1, id.id) + val rs: ResultSet = imagePstmt.executeQuery() + if(rs.next()){ + blob = rs.getBlob("image") + updatePstmt.synchronized{ + updatePstmt.setString(1,id.id) + updatePstmt.executeUpdate() + } + } + if(blob != null) + image = blob.getBytes(1, blob.length().toInt) + image } - if(blob != null) - image = blob.getBytes(1, blob.length().toInt) - image + imageOpt } def generateChallengeSamples() = { diff --git a/src/main/scala/lc/Server.scala b/src/main/scala/lc/Server.scala index 70e4765..d40e957 100644 --- a/src/main/scala/lc/Server.scala +++ b/src/main/scala/lc/Server.scala @@ -41,7 +41,7 @@ class RateLimiter extends DBConn { def checkLimit(user: Int): Boolean = { synchronized { val current = System.currentTimeMillis() - val time_passed = (current - userLastActive(user)) / 1000000000 + val time_passed = (current - userLastActive(user)) / 1000 userLastActive(user) = current userAllowance(user) += time_passed * (rate/per) if(userAllowance(user) > rate){ userAllowance(user) = rate } @@ -77,7 +77,7 @@ class Server(port: Int){ resp.send(200, write(id)) } else { resp.getHeaders().add("Content-Type","application/json") - resp.send(400, write("""{"error": "Not a valid user or rate limit reached!"}""")) + resp.send(401, write("""{"error": "Not a valid user or rate limit reached!"}""")) } 0 },"POST")