Skip to content

Commit

Permalink
Updating routes for cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Deems committed Dec 16, 2024
1 parent cd84e8b commit adc944d
Show file tree
Hide file tree
Showing 18 changed files with 603 additions and 502 deletions.
1 change: 0 additions & 1 deletion .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ on:
- "go.sum"
- "go.mod"
- "**.go"
- "scripts/errcheck_excludes.txt"
- ".github/workflows/golangci-lint.yml"
- ".golangci.yml"
pull_request:
Expand Down
19 changes: 17 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
.PHONY: all build lint fmt lintfix test checkpath check cover test-norace deps help version docker-clean

GO = go
GOIMPORTS = goimports
SOURCES := $(shell find . -name '*.go')
VERSION := $(shell git describe --tags --always --dirty)

.PHONY: all
all: build

.PHONY: help
help:
@echo "Available targets:"
@echo " all : Build the project"
Expand All @@ -18,65 +18,80 @@ help:
@echo " deps : Update dependencies"
@echo " version : Show current version"

.PHONY: version
version:
@echo "Current version: $(VERSION)"

define check_binary
@command -v $(1) >/dev/null 2>&1 || { echo "Error: $(1) binary not in PATH"; exit 1; }
endef

.PHONY: checkpath
checkpath:
$(call check_binary,go)

.PHONY: check
check: checkpath
$(call check_binary,golangci-lint)

.PHONY: docker-clean
docker-clean:
docker compose down --volumes
docker compose down --rmi all

.PHONY: clean
clean: docker-clean
rm -f throttle-proxy

.PHONY: build
build: throttle-proxy

.PHONY: throttle-proxy
throttle-proxy: $(SOURCES)
@echo ">> building binaries..."
@$(GO) build -o $@ github.com/kevindweb/throttle-proxy

.PHONY: fmt
fmt:
go fmt ./...

.PHONY: lint
lint: fmt
@$(GOIMPORTS) -l -w -local $(shell head -n 1 go.mod | cut -d ' ' -f 2) .
@golangci-lint run

.PHONY: lintfix
lintfix: fmt
@golangci-lint run --fix

.PHONY: ruff
ruff:
ruff check .

TEST_FLAGS := -v -coverprofile .cover/cover.out
TEST_PATH := ./...

.PHONY: test
test:
@echo 'Running unit tests...'
@mkdir -p .cover
@GOFLAGS=$(GOFLAGS) go test $(TEST_FLAGS) -race -count=10 $(TEST_PATH)

.PHONY: test-norace
test-norace:
@echo 'Running unit tests without race detection...'
@mkdir -p .cover
@GOFLAGS=$(GOFLAGS) go test $(TEST_FLAGS) $(TEST_PATH)

.PHONY: cover
cover: check
ifndef CI
go tool cover -html .cover/cover.out
else
go tool cover -html .cover/cover.out -o .cover/all.html
endif

.PHONY: deps
deps:
go get -u ./...
go mod tidy
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ docker compose down
docker-compose up --build
```

- Generate fake traffic with `./sandbox/traffic.py`
- Generate fake traffic with `./scripts/traffic_generator.py`
- View metrics in the [local Grafana instance](http://localhost:3000/d/be68n82lvzg8wa/throttle-proxy-metrics)

### Lint and Test
Expand Down
3 changes: 2 additions & 1 deletion docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ services:
-internal-listen-address=0.0.0.0:7776
-proxy-write-timeout=1m
-proxy-read-timeout=1m
-unsafe-passthrough-paths=/graph,/static,/manifest.json,/api/v1
-proxy-paths=/api/v1/query
-passthrough-paths=/graph,/static,/manifest.json
-enable-jitter=true
-jitter-delay=1s
-enable-observer=true
Expand Down
7 changes: 5 additions & 2 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

`go install github.com/kevindweb/throttle-proxy@latest`

## Run
### Locally

`make build`

## Usage

### Config File

```
Expand All @@ -20,7 +22,8 @@ throttle-proxy -config-file examples/config.yaml
throttle-proxy -upstream=http://localhost:9095 \
-insecure-listen-address=0.0.0.0:7777 \
-internal-listen-address=0.0.0.0:7776 \
-unsafe-passthrough-paths=/api/v2 \
-proxy-paths=/api/v2/endpoint-to-proxy \
-passthrough-paths=/api/v2/endpoint-to-passthrough \
-proxy-read-timeout=30s \
-proxy-write-timeout=30s \
-enable-jitter=true \
Expand Down
6 changes: 4 additions & 2 deletions examples/config.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
upstream: http://localhost:9095
insecure_listen_addr: 0.0.0.0:7777
internal_listen_addr: 0.0.0.0:7776
unsafe_passthrough_paths:
- /api/v2
proxy_paths:
- /api/v1/query
passthrough_paths:
- /favicon.ico
proxy_read_timeout: 5s
proxy_write_timeout: 5s
proxymw_config:
Expand Down
8 changes: 1 addition & 7 deletions examples/roundtripper/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@
### Run

```
PROXY_URL="http://localhost:9095" \
PROXY_QUERY="vector(82)" \
PROXY_WARN="80" \
PROXY_EMERGENCY="100" \
CWIND_MIN="1" \
CWIND_MAX="100" \
JITTER_PROXY_DELAY="1s" \
CONFIG_FILE="examples/config.yaml" \
go run examples/roundtripper/main.go
```
85 changes: 21 additions & 64 deletions examples/roundtripper/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,85 +6,48 @@ import (
"log"
"net/http"
"os"
"strconv"
"time"

"gopkg.in/yaml.v3"

"github.com/kevindweb/throttle-proxy/proxymw"
)

func FullConfigRoundTripper() (*proxymw.RoundTripperEntry, error) {
u := os.Getenv("PROXY_URL")
if u == "" {
return nil, fmt.Errorf("empty PROXY_URL")
}
query := os.Getenv("PROXY_QUERY")
if query == "" {
return nil, fmt.Errorf("empty PROXY_QUERY")
}
warn, err := getEnvInt("PROXY_WARN")
if err != nil {
return nil, err
}
emer, err := getEnvInt("PROXY_EMERGENCY")
if err != nil {
return nil, err
}
cwndmin, err := getEnvInt("CWIND_MIN")
if err != nil {
return nil, err
}
cwndmax, err := getEnvInt("CWIND_MAX")
func fullConfigRoundTripper(ctx context.Context) (*proxymw.RoundTripperEntry, error) {
cfg, err := parseConfigFile(os.Getenv("CONFIG_FILE"))
if err != nil {
return nil, err
}

delay := os.Getenv("JITTER_PROXY_DELAY")
jitterDelay, err := time.ParseDuration(delay)
if err != nil {
return nil, err
}

cfg := proxymw.Config{
BackpressureConfig: proxymw.BackpressureConfig{
EnableBackpressure: true,
BackpressureMonitoringURL: u,
BackpressureQueries: []proxymw.BackpressureQuery{
{
Query: query,
WarningThreshold: float64(warn),
EmergencyThreshold: float64(emer),
},
},
CongestionWindowMin: cwndmin,
CongestionWindowMax: cwndmax,
},

EnableJitter: true,
JitterDelay: jitterDelay,

EnableObserver: true,
}

mw, err := proxymw.NewRoundTripperFromConfig(cfg, http.DefaultTransport)
if err != nil {
return nil, err
}

mw.Init(context.Background())
mw.Init(ctx)
return mw, err
}

func getEnvInt(s string) (int, error) {
a := os.Getenv(s)
i, err := strconv.Atoi(a)
func parseConfigFile(configFile string) (proxymw.Config, error) {
// nolint:gosec // accept configuration file as input
file, err := os.Open(configFile)
if err != nil {
return 0, err
return proxymw.Config{}, fmt.Errorf("error opening config file: %v", err)
}
defer file.Close()

var cfg proxymw.Config
decoder := yaml.NewDecoder(file)
if err := decoder.Decode(&cfg); err != nil {
return proxymw.Config{}, fmt.Errorf("error decoding YAML: %v", err)
}
return i, nil

return cfg, nil
}

func main() {
rt, err := FullConfigRoundTripper()
ctx := context.Background()
rt, err := fullConfigRoundTripper(ctx)
if err != nil {
log.Fatal(err)
}
Expand All @@ -94,13 +57,7 @@ func main() {
Transport: rt,
}

ctx := context.Background()
request, err := http.NewRequestWithContext(
ctx,
http.MethodGet,
"https://google.com",
http.NoBody,
)
request, err := http.NewRequestWithContext(ctx, http.MethodGet, "https://x.com", http.NoBody)
if err != nil {
log.Fatal(fmt.Errorf("failed to make request: %w", err))
}
Expand Down
Loading

0 comments on commit adc944d

Please sign in to comment.