Giter Site home page Giter Site logo

borrowanumber's Introduction

BorrowANumber

Purpose

This was a project where I needed to solve an issue with multiple load performance tests simultaneously (using k6), the test users were experiencing collisions with usernames.

Rather than work out complex logic to have them determine which user number they were, I created this small development server to resolve it, all it does is serve up numbers for them. They borrow a number, and when they're done, they return it for another load test user.

Reminder this is just an example and no guarantees are provided. A Production grade WSGI server like gunicorn would be recommended over this basic flask example.

Example Linux Installation

  1. Install Python
  2. Install Redis Cluster (I used redis-cluster helm chart from bitnami) and telepresence on a k3s cluster, but regular redis-cluster works fine too.
  3. (Optional) - setup virtualenv for your python
  4. Install from requirements: pip install -r requirements.txt
  5. Create a .env file with your variables:
    REDIS_PASSWORD="secret"
    RANGE_START=10000
    RANGE_AMOUNT=10000
    This example would serve numbers 10000-20000
  6. Run the server: python BorrowANumber.py

REST API

The REST API to the example app is described below.

Get list of checked numbers

Request

GET /checked

curl http://localhost:5000/checked

Response headers

Connection: close
Content-Length: 27
Content-Type: application/json
Date: Fri, 01 Mar 2024 00:00:00 GMT
Server: Werkzeug/3.0.1 Python/3.12.0

Response body

{
  "checked_out_numbers": []
}

Check out a new number

Request

GET /check

curl http://localhost:5000/check

Response headers

HTTP/1.1 200 OK
Connection: close
Content-Length: 31
Content-Type: application/json
Date: Fri, 01 Mar 2024 00:00:00 GMT
Server: Werkzeug/3.0.1 Python/3.12.0

Response body

{
    "checked_out_number": "16713"
}

Return a number

Request

POST /return

curl -H "Content-Type: application/json" -XPOST http://localhost:5000/return

Response headers

HTTP/1.1 200 OK
Connection: close
Content-Length: 49
Content-Type: application/json
Date: Fri, 01 Mar 2024 00:00:00 GMT
Server: Werkzeug/3.0.1 Python/3.12.0

Response body

{
  "message": "Number 16713 returned successfully"
}

Reset all numbers

Request

GET /reset

curl http://localhost:5000/reset

Response headers

HTTP/1.1 200 OK
Connection: close
Content-Length: 17
Content-Type: application/json
Date: Fri, 01 Mar 2024 00:00:00 GMT
Server: Werkzeug/3.0.1 Python/3.12.0

Response body

{
  "reset": "true"
}

borrowanumber's People

Contributors

axiompraxis avatar

Watchers

 avatar

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.