aggregated counting + local counting

https://github.com/patrickmn/go-cache
Prerequist: have a redis cache to store key-value pair
Goal:
- Provide TakeToken and GetBucketStats api
- No need to a separte process to keep adding token to bucket or remove key-value pair from cache to prevent overuse memory
Implementation:
- Start each bucket full:

- Each bucket auto expired after reach max tokens:

- Only need to save token number and timestamp when bucket reached saved token number

- Create an Azure Redis Cache
export AZURE_SUBSCRIPTION_ID=8ecadfc9-d1a3-4ea4-b844-0d9f87e4d7c8
export AZURE_RESOURCE_GROUP=xinywaTestRG
export location=eastus
# create resouregroup
az group create --name $AZURE_RESOURCE_GROUP --location $location --subscription $AZURE_SUBSCRIPTION_ID
# create azure redis cache
export AZURE_REDIS_NAME=xinywaRedisCache
az redis create --location $location --name $AZURE_REDIS_NAME --resource-group $AZURE_RESOURCE_GROUP --subscription $AZURE_SUBSCRIPTION_ID --sku Basic --vm-size c0- Set them to the respective environment variables
export AZURE_SUBSCRIPTION_ID=8ecadfc9-d1a3-4ea4-b844-0d9f87e4d7c8
export AZURE_RESOURCE_GROUP=xinywaTestRG
export AZURE_REDIS_NAME=xinywaRedisCache
export MSI_RESOURCE_ID="/subscriptions/8ecadfc9-d1a3-4ea4-b844-0d9f87e4d7c8/resourcegroups/xinywaTestRG/providers/Microsoft.ManagedIdentity/userAssignedIdentities/xinywamsi"
export MSI_OBJECT_ID=0a35e6d8-1026-4082-8890-8725c27e7594- Clone the repo. Then in the terminal, run the following command to start the application.
cd test/withazureredis
go run main.goThe HTTP server will start on port 8080.
-
Send request to test cache and throttle:
Create request with billingAccount id1:
curl -i -X POST -d '{"billingAccount":"id1"}' localhost:8080/billingAccount/Get bucket stats for billingAccount id1:
curl -i localhost:8080/billingAccount/id1
- Deployer redis cluster (minimum 6 nodes)
kubectl apply -f rediscluster/redis-cluster.yaml
kubectl apply -f rediscluster/redis-configmap.yaml
kubectl apply -f rediscluster/redis-service.yaml
rediscluster/roles.sh
- Deploy test server and expose service
kubectl apply -f cmd/rediscluster/template/deployment.yaml
kubectl expose deployment rate-limit-server --name=rate-limit-svc --port=8080 --target-port=8080 --type=NodePort
kubectl port-forward svc/rate-limit-svc 8080:80
- Clone the repo. Then in the terminal, run the following command to start the application.
cd test/withrediscluster
go run main.goThe HTTP server will start on port 8080.
-
Send request to test cache and throttle:
Create request with billingAccount id1:
curl -i -X POST -d '{"billingAccount":"id1"}' localhost:8080/billingAccount/Get bucket stats for billingAccount id1:
curl -i localhost:8080/billingAccount/id1

