Skip to content
This repository was archived by the owner on Jan 29, 2024. It is now read-only.

Package rlguard to be published on Pypi #8

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .env.example

This file was deleted.

40 changes: 40 additions & 0 deletions .github/workflows/publish_to_pypi.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: Publish to PyPI

on:
release:
types: [published]

jobs:
build-and-publish:
name: Build and publish to PyPI
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- name: Set up Python 3.8
uses: actions/setup-python@v2
with:
python-version: 3.8

- name: Install pypa/build
run: >-
python -m
pip install
build
--user

- name: Build a binary wheel and a source tarball
env:
GITHUB_REF_TAG: ${{ github.ref }}
run: |
echo "Extracting version from GITHUB_REF_TAG: $GITHUB_REF_TAG"
export RATE_LIMITING_GUARD_VERSION=$(echo "$GITHUB_REF_TAG" | sed -e "s#^refs/tags/##")
python -m build --sdist --wheel --outdir dist/ .

- name: Publish to PyPI
uses: pypa/gh-action-pypi-publish@release/v1
with:
user: __token__
password: ${{ secrets.PYPI_API_TOKEN }}
verbose: true
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
.env
**/.env
.cache
dist
build
src/rate_limiting_guard.egg-info
39 changes: 23 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ This repository provides:
| | |
| +-------------+ +---------v--------+ |
| | | | | |
| | Redis <-----+ Syncer | |
| | Store <-----+ Syncer | |
| | | | | |
(2) request| +--^-------^--+ +------------------+ |request (2)
data| | | |data
Expand Down Expand Up @@ -86,46 +86,53 @@ This repository provides:

## Installation

Both service and library are provided as examples. We encourage you to investigate them and to adapt them to your needs.
```
pip install rate_limiting_guard
```

### Syncer service

The purpose of `syncer` service is to synchronize the internal state (counters which are kept in Redis) with Sentinel Hub. It does so by periodically refilling the buckets according to user's rate limiting policies.
```python
from rate_limiting_guard.syncer import start_syncer
```

The purpose of `syncer` service is to synchronize the internal state (counters which are kept in Redis/Zookeper) with Sentinel Hub. It does so by periodically refilling the buckets according to user's rate limiting policies.

Before running the `syncer` service, first edit `.env` file and set user credentials:
```
CLIENT_ID=...
CLIENT_SECRET="..."
REFRESH_BUCKETS_SEC=...
ZOOKEEPER_HOSTS=...
REDIS_HOST=...
REDIS_PORT=...
REVISIT_TIME_MSEC=...
LOGLEVEL=...
```

Note: since `CLIENT_SECRET` contains special characters by design, you should enclose it in double quotes.

Syncer service depends on Redis. You can run both of them using Docker and Docker-compose:
Syncer service depends on Redis/Zookeper. You can run both of them using Docker and Docker-compose:
```
$ docker-compose build
$ docker-compose up -d
```

With time, the values in the buckets might drift away from the actual values on Sentinel Hub. `RLGuard` can automatically refresh the values in the buckets, just edit the `.env` file and set:
```
REFRESH_BUCKETS_SEC=<refreshing interval in seconds>
```

### RLGuard library

The purpose of `RLGuard` library is to make applying for a permission to make a request to Sentinel Hub a bit easier. It provides two functions:
- `apply_for_request`: updates the counters in the central storage (Redis) and calculates the delay worker should wait for before making a request to Sentinel Hub, and
- `calculate_processing_units`: helper function to calculate the number of [Processing Units](https://docs.sentinel-hub.com/api/latest/api/overview/processing-unit/) the request will use

For the time being, the library is only available as part of this repository (i.e., it can't be installed via `pip` and similar mechanisms).
```python
from rate_limiting_guard.lib import calculate_processing_units, apply_for_request, RedisRepository, ZooKeeperRepository
```

To use it:
- copy `lib/rlguard.py` file to your project,
- import it, and
- use `calculate_processing_units` and `apply_for_request` in your code.
The purpose of `rate_limiting_guard.lib` module is to make applying for a permission to make a request to Sentinel Hub a bit easier. It provides two functions:
- `apply_for_request`: updates the counters in the central storage (Redis/Zookeper) and calculates the delay worker should wait for before making a request to Sentinel Hub, and
- `calculate_processing_units`: helper function to calculate the number of [Processing Units](https://docs.sentinel-hub.com/api/latest/api/overview/processing-unit/) the request will use

For an example see `lib/example.py`.

## Additional information

See [DETAILS.md](./DETAILS.md) for additional implementation information.
See [DETAILS.md](https://github.com/sentinel-hub/rate-limiting-guard/blob/master/DETAILS.md) for additional implementation information.
3 changes: 1 addition & 2 deletions e2etest/performance.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
currentdir = os.path.dirname(os.path.realpath(__file__))
parentdir = os.path.dirname(currentdir)
sys.path.append(parentdir)
from lib.rlguard import apply_for_request, SyncerDownException
from lib.rlguard.repository import RedisRepository
from src.rate_limiting_guard.lib import apply_for_request, SyncerDownException, RedisRepository


MOCKSH_ROOT_URL = "http://127.0.0.1:8000"
Expand Down
8 changes: 8 additions & 0 deletions example/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CLIENT_ID=
CLIENT_SECRET="..."
REFRESH_BUCKETS_SEC=
ZOOKEEPER_HOSTS=
REDIS_HOST=
REDIS_PORT=
REVISIT_TIME_MSEC=
LOGLEVEL=
1 change: 1 addition & 0 deletions lib/Pipfile → example/Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ black = "==20.8b1"
requests = "*"
redis = "*"
kazoo = "*"
rate-limiting-guard = {editable = true, path = "./.."}

[requires]
python_version = "3.8"
4 changes: 1 addition & 3 deletions lib/example.py → example/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
import requests
import requests.exceptions

from rlguard import calculate_processing_units, OutputFormat, apply_for_request, SyncerDownException
from rlguard.repository import Repository, RedisRepository

from rate_limiting_guard.lib import calculate_processing_units, OutputFormat, apply_for_request, SyncerDownException, Repository, RedisRepository

logging.basicConfig(level=os.environ.get("LOGLEVEL", "INFO").upper())

Expand Down
File renamed without changes.
212 changes: 0 additions & 212 deletions lib/Pipfile.lock

This file was deleted.

3 changes: 0 additions & 3 deletions lib/setup.py

This file was deleted.

Loading