Skip to content

Commit

Permalink
Throttling implementation
Browse files Browse the repository at this point in the history
First implemented version

---------

Co-authored-by: Jose Manuel Cardona <josemanuel.cardona@softonic.com>
Co-authored-by: Alberto Gonzalez <alberto.gonzalez@softonic.com>
Co-authored-by: Juan Lotito <juan.lotito@softonic.com>
  • Loading branch information
4 people authored Dec 4, 2024
1 parent 5a5d002 commit 5f33684
Show file tree
Hide file tree
Showing 11 changed files with 598 additions and 18 deletions.
18 changes: 18 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, build with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Never ignore gitkeep files
!.gitignore

# Ignoring generated files
bin
16 changes: 16 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
dist: bionic

language: go

env: GO111MODULE=on GOFLAGS='-mod vendor'

go:
- 1.13.x


before_script:
- go install github.com/golangci/golangci-lint/cmd/golangci-lint

script:
- make build
- golangci-lint run
1 change: 1 addition & 0 deletions CODEOWNERS
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* @joskfg @mindhells @paupm @lotykun @santinoncs
11 changes: 11 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM golang:1.23-bookworm AS build

COPY ./ /go/src/github.com/softonic/hp-throttling

RUN cd /go/src/github.com/softonic/hp-throttling && make build

FROM scratch

COPY --from=build /go/src/github.com/softonic/hp-throttling/bin/hp-throttling /

ENTRYPOINT ["/hp-throttling", "-logtostderr"]
13 changes: 13 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Copyright 2019 Softonic International S.A.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
dep:
go mod download
build: dep
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -ldflags="-w -s" -o bin/hp-throttling pkg/main.go
docker-build:
docker build -t softonic/hp-pass-middleware:dev .
43 changes: 25 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,36 @@
# HP Passthrough Middleware
# HP throttling Middleware

[Homing pigeon](https://github.com/softonic/homing-pigeon) middleware that just pass the data.
[Homing pigeon](https://github.com/softonic/homing-pigeon) middleware that throttles messages to limit writes.

### Overview

This dummy middleware can be used as base middleware to do your own implementations.

### Requirements

All the [Homing pigeon](https://github.com/softonic/homing-pigeon) middlewares must accept at least the next env vars.

| Name | Description | Example |
| ---------- | ------------------------------------------ | ------------------------------------------------------------------------------------ |
| IN_SOCKET | Socket where the data will be received | `"/sockets/input.sock"` |
| OUT_SOCKET | Socket to send data to the next middleware | `"passthrough:///unix:///sockets/input.sock"` or `""` if this is the last middleware |
This middleware is a simple implementation of a throttling middleware. It will limit the number of messages per second that can be sent to the next middleware, allowing for
burst usage.

### How it works

The middleware receives a `github.com/softonic/homing-pigeon/middleware` message as request in the `IN_SOCKET`, so you can
modify the input data to send it to the next middlewares. After the next middlewares are executed, it intercepts the response
so it can be modified again before it is finally returned.
The middleware uses the `rate` library of Go to implement throttling and burst functionality. This means that you can configure the number of messages allowed per second, as well as the ability to
handle occasional bursts of traffic. The configuration is done according to the documentation provided by the [`rate` library](https://pkg.go.dev/golang.org/x/time/rate).

- **Limit**: This defines the rate at which events are allowed. It's expressed in events per second. If you set a `Limit` of 5, you are allowing 5 events to occur per second. If the value is set to 0,
throttling is disabled.

The main homing-pigeon package come with an `UnimplementedMiddleware` to allow you to implement just the middleware bussiness logic. If you need
more control, you can implement your middleware from scratch. Take a look at `UnimplementedMiddleware` to know the basic implementation.
- **Burst**: This is the maximum number of events that are allowed to burst beyond the `Limit` in a short period. Even if the rate is controlled by the `Limit`, a burst allows occasional bursts of
event traffic greater than the normal rate.

### Usage

You just need to execute the binary or use docker to automatically run it. If you want to show some logging in the output, you can use the [kglog](https://github.com/kubernetes/klog) flags in the command.
If you are using the [homing-pigeon-chart](https://github.com/softonic/homing-pigeon-chart/) Helm chart, you can enable the throttling middleware by setting the following values in the `values.yaml` file:

```yaml
requestMiddlewares:
- name: throttling
repository: softonic/hp-throttling
tag: latest
pullPolicy: IfNotPresent
env:
- name: THROTTLE_LIMIT
value: "100"
- name: THROTTLE_BURST
value: "100"
```
15 changes: 15 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
version: '3.7'

services:
hp-middleware-throttling:
volumes:
- ./sockets:/tmp
build:
context: .
dockerfile: Dockerfile
image: softonic/hp-throttling:0.1.0
entrypoint: ["/hp-throttling"]
environment:
IN_SOCKET: "/tmp/hp"
THROTTLE_LIMIT: "100"
THROTTLE_BURST: "100"
18 changes: 18 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module github.com/softonic/hp-throttling

require (
github.com/softonic/homing-pigeon v0.0.0-20191212144147-c9d6d0b55981
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4
k8s.io/klog v1.0.0
)

require (
github.com/golang/protobuf v1.3.2 // indirect
golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553 // indirect
golang.org/x/sys v0.0.0-20191210023423-ac6580df4449 // indirect
golang.org/x/text v0.3.2 // indirect
google.golang.org/genproto v0.0.0-20191206224255-0243a4be9c8f // indirect
google.golang.org/grpc v1.25.1 // indirect
)

go 1.23
Loading

0 comments on commit 5f33684

Please sign in to comment.