11# ApiGuard
22
3+ - [ Overview] ( #overview )
4+ - [ Features] ( #features )
5+ - [ How it works] ( #how-it-works )
6+ - [ Getting Started] ( #getting-started )
7+ - [ Prerequisites] ( #prerequisites )
8+ - [ Installation] ( #installation )
9+ - [ Configuration] ( #configuration )
10+ - [ Building and Running via Zig] ( #building-and-running-the-service-via-zig )
11+ - [ Running the Service in Production] ( #running-the-service-in-production )
12+ - [ Running the Service via Docker
13+ Container] ( #running-the-service-via-docker-container )
14+ - [ Furhat Kotlin Client Example] ( #furhat-kotlin-client-example )
15+ - [ ApiGuard API] ( #apiguard-api )
16+ - [ Endpoints] ( #endpoints )
17+ - [ Example Responses] ( #example-responses )
18+
319## Overview
420
521ApiGuard is a rate limiting service designed to manage and protect API endpoints
@@ -42,7 +58,7 @@ incoming requests to consecutive time-slots.
4258For n=500, the time-slots are 120ms apart. So the first request will have a
4359delay of 0ms since it is the first one. The second request will be put on the
4460second time-slot 120ms apart from the first -> delay=120ms, if both requests
45- arrived at the same time.
61+ arrive at the same time.
4662
4763If the second request comes in 50ms after the first, then the allocated
4864time-slot for it will still be the second time-slot 120ms apart from the first
@@ -56,7 +72,7 @@ request of another client than the one that sent the first three, that other
5672client's first request will be delayed by 360ms, according to its assigned
5773time-slot.
5874
59- Time-slots that aren't used because of slow clients, will be put into a list of
75+ Time-slots that aren't used because of slow clients will be put into a list of
6076"free passes" that encounter ** no delay** . This allows for quickly catching up
6177and lowering the API request delay when the API hasn't been used for a few
6278time-slots.
@@ -71,7 +87,7 @@ old 498 free passes must not be used to guarantee time-slotting to work.
7187All the constraints mentioned above have been tested under load. See
7288[ clientbot] ( clientbot.md ) for more information.
7389
74- The following example illustrates many of the things mentioned above: 5
90+ The following example illustrates many of the things mentioned above: 3
7591concurrent clients that sleep for 5s every 50 requests, allowing the server to
7692use some of the free passes after those delays, etc. While for each individual
7793client the mean delay is > 120ms, API-requests for all clients combined are
@@ -188,10 +204,10 @@ curl http://127.0.0.1:${APIGUARD_PORT}${APIGUARD_SLUG}/request_access
1882042 . Load the image into docker:
189205 ``` bash
190206 docker load < downloaded_file
191- # or, if you build it with nix:
207+ # or, if you built it with nix:
192208 docker load < result
193209 ```
194- 3. Create an api_guard.rc file, see [configuration](# configuration) and set its PORT to
210+ 3. Create an api_guard.rc file, see [configuration](# configuration), and set its PORT to
195211 5500. That port is just used inside the container:
196212 ` ` `
197213 PORT=5500
@@ -214,7 +230,37 @@ curl http://127.0.0.1:${APIGUARD_PORT}${APIGUARD_SLUG}/request_access
214230 # run a simple request
215231 curl http://127.0.0.1:${APIGUARD_PORT}${APIGUARD_SLUG} /request_access
216232 ` ` `
217- # # Usage
233+ # ## Furhat Kotlin Client Example
234+
235+ Here comes a code snippet you can use in a Furhat robot skill which is what this
236+ service has initially been developed for:
237+
238+ 1. Add the ` khttp` dependency to your ` build.gradle` :
239+ ` ` ` gradle
240+ dependencies {
241+ implementation ' com.furhatrobotics.furhatos:furhat-commons:2.7.1'
242+ implementation ' com.theokanning.openai-gpt3-java:service:0.16.0'
243+ compile ' khttp:khttp:1.0.0'
244+ }
245+ ` ` `
246+
247+ 2. Before making a request to your API, insert the following lines:
248+ ` ` ` kotlin
249+ // make a khttp request to api guard
250+ val API_GUARD_URL = " https://your.domain.org/api_guard"
251+ val API_GUARD_KEY = " YOUR AUTH TOKEN"
252+ val guard_response = khttp.get(
253+ " $API_GUARD_URL /request_access?handle_delay=true" ,
254+ headers=mapOf(" Authorization" to " Bearer " + API_GUARD_KEY)
255+ ).text
256+ print(" API Guard said: $guard_response " )
257+ ` ` `
258+
259+ This will let the server take care of rate limiting your API calls.
260+
261+ ** Note:** Make sure to prepend ' Bearer ' to your API token as shown above!
262+
263+ # # ApiGuard API
218264
219265# ## Endpoints
220266
@@ -254,36 +300,6 @@ curl http://127.0.0.1:${APIGUARD_PORT}${APIGUARD_SLUG}/request_access
254300 - ** Example** :
255301 - [` AUTH_TOKEN=YOUR_TOKEN ./test_set_params.sh` ](./test_set_params.sh): how to set values via ` /set_rate_limit`
256302
257- # ## Furhat Kotlin Example
258-
259- Here comes a code snippet you can use in a Furhat robot skill which is what this
260- service has initially been developed for:
261-
262- 1. Add the ` khttp` dependency to your ` build.gradle` :
263- ` ` ` gradle
264- dependencies {
265- implementation ' com.furhatrobotics.furhatos:furhat-commons:2.7.1'
266- implementation ' com.theokanning.openai-gpt3-java:service:0.16.0'
267- compile ' khttp:khttp:1.0.0'
268- }
269- ` ` `
270-
271- 2. Before making a request to your API, insert the following lines:
272- ` ` ` kotlin
273- // make a khttp request to api guard
274- val API_GUARD_URL = " https://your.domain.org/api_guard"
275- val API_GUARD_KEY = " YOUR AUTH TOKEN"
276- val guard_response = khttp.get(
277- " $API_GUARD_URL /request_access?handle_delay=true" ,
278- headers=mapOf(" Authorization" to " Bearer " + API_GUARD_KEY)
279- ).text
280- print(" API Guard said: $guard_response " )
281- ` ` `
282-
283- This will let the server take care of rate limiting your API calls.
284-
285- ** Note:** Make sure to prepend ' Bearer ' to your API token as shown above!
286-
287303# ## Example Responses
288304
289305Please see the examples:
0 commit comments