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] = {
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() = {

View File

@ -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")