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:
Rahul Rudragoudar 2020-07-08 21:28:11 +05:30 committed by GitHub
parent 62b3a098bd
commit 1d7ef512bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 11 deletions

View File

@ -40,16 +40,21 @@ class Captcha(throttle: Int) extends DBConn {
def getCaptcha(id: Id): Array[Byte] = { def getCaptcha(id: Id): Array[Byte] = {
var image :Array[Byte] = null var image :Array[Byte] = null
var blob: Blob = null var blob: Blob = null
imagePstmt.setString(1, id.id) val imageOpt = imagePstmt.synchronized {
val rs: ResultSet = imagePstmt.executeQuery() imagePstmt.setString(1, id.id)
if(rs.next()){ val rs: ResultSet = imagePstmt.executeQuery()
blob = rs.getBlob("image") if(rs.next()){
updatePstmt.setString(1,id.id) blob = rs.getBlob("image")
updatePstmt.executeUpdate() updatePstmt.synchronized{
updatePstmt.setString(1,id.id)
updatePstmt.executeUpdate()
}
}
if(blob != null)
image = blob.getBytes(1, blob.length().toInt)
image
} }
if(blob != null) imageOpt
image = blob.getBytes(1, blob.length().toInt)
image
} }
def generateChallengeSamples() = { def generateChallengeSamples() = {

View File

@ -41,7 +41,7 @@ class RateLimiter extends DBConn {
def checkLimit(user: Int): Boolean = { def checkLimit(user: Int): Boolean = {
synchronized { synchronized {
val current = System.currentTimeMillis() val current = System.currentTimeMillis()
val time_passed = (current - userLastActive(user)) / 1000000000 val time_passed = (current - userLastActive(user)) / 1000
userLastActive(user) = current userLastActive(user) = current
userAllowance(user) += time_passed * (rate/per) userAllowance(user) += time_passed * (rate/per)
if(userAllowance(user) > rate){ userAllowance(user) = rate } if(userAllowance(user) > rate){ userAllowance(user) = rate }
@ -77,7 +77,7 @@ class Server(port: Int){
resp.send(200, write(id)) resp.send(200, write(id))
} else { } else {
resp.getHeaders().add("Content-Type","application/json") 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 0
},"POST") },"POST")