Skip to content

Key server#1

Open
aryanku-dev wants to merge 7 commits intomainfrom
key-server
Open

Key server#1
aryanku-dev wants to merge 7 commits intomainfrom
key-server

Conversation

@aryanku-dev
Copy link
Owner

In this assignment, Sinatra is being used for the api endpoints.
The endpoints are:
-> POST /generateKeys => creates 5 unique new keys
-> GET /key => returns any available key
-> PUT /releasekey/:id => unblocks the key in use
-> DELETE /deletekey/:id => deletes the key if being used
-> PUT /keepalive/:id => Keeps the keys alive if called by the client.

At the start of the app.rb file, there is a logic to handle automatic cleaning of the keys, i.e., it unblocks the keys which are in use for more than 1 minute and deletes the keys with respect to a 5 minute expiration period.
The rest of the core methods which have been used are in the key_server.rb file.

@aryanku-dev aryanku-dev requested a review from puneet00 October 17, 2025 13:20
key_server.rb Outdated
true
end

def free_up
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cleanup is not consistent and takes O(n)
this means app will return expired keys

key_server.rb Outdated
return true
end

def delete_key(key)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

race conditions in this code

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

similar race conditions in other methods

key_server.rb Outdated
return true
end

def delete_key(key)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

similar race conditions in other methods

@aryanku-dev aryanku-dev requested a review from puneet00 October 27, 2025 07:37
key_server.rb Outdated

def keep_alive(key)
key = key.to_sym
@lock.synchronize {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

free_up is O(n)
so every method using this lock is O(n)
current solution doesn't meet problem statement constraints

@aryanku-dev aryanku-dev requested a review from puneet00 October 29, 2025 08:22
key_server.rb Outdated
end
expired_free = @redis.zrangebyscore(FREE_ZSET, '-inf', now)
expired_free.each do |k|
@redis.multi do |r|
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

multi will cause race condition here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants