This is a custom Krakend Ratelimiter Plugin for demonstration.
Read the full blog here https://techpro.ninja/krakend-plugin-golang-tutorial/
- Using docker krakend/builder:2.2.1 to generate the
.so
file. The version of builder should be same as the Krakend deployment or else the plugin will not work.
docker run -it -v "some_path:/go/src/krakend-ratelimiter-plugin" -w /go/src/krakend-ratelimiter-plugin krakend/builder:2.2.1 go build -buildmode=plugin -o krakend-ratelimiter-plugin.so .
Once built, copy the krakend-ratelimiter-plugin.so
file to tests folder from the Docker container
- Build Docker image
cd tests
docker build . -t test-v1
- Running the Docker image
docker run -it -p 8001:8001 test-v1
At this point, you can check
To check bucket details http://localhost:8001/__bucket-tracker
This is a service level plugin meaning it resides in the root of krakend.json file
"plugin": {
"pattern": ".so",
"folder": "/etc/krakend/plugins/"
},
...
"extra_config":{
"plugin/http-server":{
"name":["krakend-ratelimiter-plugin"],
"krakend-ratelimiter-plugin":{
"trackerPath": "/__bucket-tracker"
}
}
}
Configurable values are trackerPath
. Do not change anything else.
Path to track the bucket capacity and token stock details inside the plugin. For example, https://<SERVER_ADDRESS>/__bucket-tracker
.
Note: If you change the plugin name from krakend-ratelimiter-plugin
, do remeber to change the following line in main.go
file as well
var pluginName = "krakend-ratelimiter-plugin"
Also, remember to change the values in Dockefile
in tests folder