Giter Club home page Giter Club logo

efficient-api-session-manager's Introduction

Efficient API Session Manager (In Memory)

About

  • Allows efficiient management of API keys issued to different users.

  • Operations related to all endpoints are completed in O(1) time complexity.

Time complexity

  • O(1)

  • Data Structure: Bi-directional Multi Map

    • Allows lookup by both key and value in O(1) time
    • Implementation file: ./api/model/biMultiMap.js

Routes

/api/generate-key - GET /api/get-key - POST /api/unblock/:key - PUT /api/delete/:key - DELETE /api/keep-alive/:key - PUT /api/blocked - GET /api/available - GET

Approach and asumptions

To provide all the functionality in the problem statement in a clear and logical way. We will provide user object in the body of all routes except GET.

  • A user gets an API key through the /get-key endpoint
  • The API key is blocked for him for 60s only during which no other user can get that key
  • After that it is unblocked and other users may get access to the same key
  • When some other user gets access to the key the previous user will loose access.
  • If no other user has aquired the key since it got unblocked (after the one minute mark or through the /unblock endpoint), The previous user may renew his key upto 5 mins and block it for himself for 60s more.
  • If no other user aquires the key and the original user does not renew it within 5 mins then the API key gets deleted.

Here is what the user object might look like { "user": "2356465211561" }

Workflow and Steps for testing

npm start

Step 1 : Send a GET request to /generate-key to generate an API key

Step 2 : Send a GET request to /available to see the newly created API key in available state

Step 3 : Send a POST request to /get-key with the user string in the body in the following format

{
    "user": "saurabh"
}

This would block the an API key for 60 secs.

Step 4 : Send a GET request to /blocked we will now be able to see our API key in blocked state.

Step 5 : Wait for a minute, and repeat Step 4 we could see our API key has become active again.

Step 6 : Send a POST request to /get-key with details of a different user in the body. The API we will again move to a the blocked state.

Step 7 : Send a PUT request to /keep-alive/:key with details of the original user in the body. We would get a 403 Forbidden error with a message asying that this user no longer has access to the api key. If we send the request with details of the user currently assosiated to the key then the request would go thorugh, 5 minutes timer to delete the key would reset and the key would be blocked for 60s for the current user.

Step 8 : Wait for 5 minutes and check available and blocked endpoints, the key would be deleted now as we did not send a keep alive request in 5 minutes or some other user did not acquire the key after one minute.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.