DB synchronisation on media endpoint (#47)
* Update sql to map uuid to token Signed-off-by: Rahul Rudragoudar <rr83019@gmail.com> * Fix millis to secs conversion Signed-off-by: Rahul Rudragoudar <rr83019@gmail.com> * Add synchronisation to media enpoint DB access Signed-off-by: Rahul Rudragoudar <rr83019@gmail.com> * Change error code for rate limiter Signed-off-by: Rahul Rudragoudar <rr83019@gmail.com>
This commit is contained in:
parent
62b3a098bd
commit
1d7ef512bd
|
@ -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() = {
|
||||
|
|
|
@ -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")
|
||||
|
|
Loading…
Reference in New Issue