Skip to content

Commit 0611dcb

Browse files
authored
Improvements (#87)
* Fix return -> continue in UpdateCache to prevent goroutine death The UpdateCache goroutine used `return` instead of `continue` on error paths (empty database, query parse failure, table extraction failure). A single transient error would permanently kill the goroutine, stopping all server-response caching for the plugin's lifetime. Resolves #64. * Fix IsCacheNeeded to check parsed query instead of cache key IsCacheNeeded was called with the full cache key (server:db:request_bytes) instead of the actual SQL query string. Date/time function detection against raw binary bytes was unreliable. Now the query is parsed first via GetQueryFromRequest and uppercased before checking. * Fix ExitOnStartupError ordering and close(nil) panic in main.go Move config field assignments (including ExitOnStartupError) before the API client initialization block so the flag is set when first checked. Also guard the deferred channel close to prevent a panic when config is nil and the channel was never initialized. * Add CacheErrorsCounter, fix double-counting misses, and fix typos Introduce a dedicated CacheErrorsCounter to separate Redis operation failures from genuine cache misses. Previously CacheMissesCounter was incremented for SET/DEL/SCAN errors, making the metric unreliable. Also fix double-counting in OnTrafficFromClient where a single miss incremented CacheMissesCounter twice. Fix typos: cachedRespnseKey -> cachedResponseKey, DateFucntion -> DateFunction. * Extract duplicated startup error handling into helper function Replace three copies of the ExitOnStartupError check + manual close + os.Exit(1) pattern with a single handleStartupError helper that accepts variadic io.Closer resources. Reduces boilerplate and ensures consistent behavior across all startup error paths. * Add explicit config validation with sensible defaults Previously cast.To* silently returned zero-values for invalid config. Now redisURL, expiry, and scanCount are validated with warnings and defaults applied when values are missing or invalid. * Add config validation and graceful goroutine shutdown via WaitGroup Add a WaitGroup to the Plugin struct so the UpdateCache goroutine signals completion when the channel is closed, allowing the process to drain pending cache updates before exit. Tests updated to use the same mechanism. * Update gatewayd-plugin-sdk to v0.4.4, gatewayd to v0.11.0, and all direct deps SDK v0.4.4 brings redis/go-redis/v9, go-plugin v1.7.0, pgx/v5 v5.8.0, grpc v1.79.1, and protobuf v1.36.11. Also updates miniredis v2.36.1, sentry v0.42.0, cast v1.10.0, testify v1.11.1, and prometheus v1.23.2 via make update-direct-deps. * Migrate from go-redis/redis/v8 to redis/go-redis/v9 Replace deprecated github.com/go-redis/redis/v8 with the maintained github.com/redis/go-redis/v9. Updated import paths in plugin.go, main.go, plugin_test.go, and the depguard config in .golangci.yaml. * Update CI actions to v4/v5, add govulncheck to test workflow Bump actions/checkout to v4, actions/setup-go to v5 (Node 20), and softprops/action-gh-release to v2. Add govulncheck step to the test workflow for security scanning. setup-go v5 includes built-in module caching. * Add tests for IsCacheNeeded, OnClosed, invalidateDML, and goroutine resilience - TestIsCacheNeeded: table-driven test covering all PostgreSQL date/time functions plus positive cases (SELECT, INSERT, empty) - TestOnClosed: verifies client-to-database key cleanup on connection close - TestOnClosedNilClient: verifies graceful handling of nil client - TestInvalidateDML: verifies INSERT invalidates cached SELECT responses and their table index keys - TestInvalidateDMLSelectIgnored: verifies SELECT queries don't trigger invalidation - TestUpdateCacheContinuesOnError: confirms the goroutine survives errors and processes subsequent messages (validates the return->continue fix) * Fix golangci-lint issues: import ordering and nestif complexity Sort imports alphabetically to satisfy gci linter. Reduce nestif complexity in UpdateCache by inverting the caching precondition into an early continue.
1 parent 07373a2 commit 0611dcb

9 files changed

Lines changed: 421 additions & 214 deletions

File tree

.github/workflows/release.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ jobs:
1313
runs-on: ubuntu-latest
1414
steps:
1515
- name: Checkout code
16-
uses: actions/checkout@v3
16+
uses: actions/checkout@v4
1717
with:
1818
fetch-depth: 0
1919
- name: Set up Go 1.24
20-
uses: actions/setup-go@v3
20+
uses: actions/setup-go@v5
2121
with:
2222
go-version: "1.24"
2323
cache: true
@@ -27,7 +27,7 @@ jobs:
2727
run: |
2828
make build-release
2929
- name: Create release and add artifacts
30-
uses: softprops/action-gh-release@v1
30+
uses: softprops/action-gh-release@v2
3131
with:
3232
files: |
3333
dist/*.tar.gz

.github/workflows/test.yaml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,26 @@ jobs:
1212
test:
1313
name: Test gatewayd-plugin-cache
1414
runs-on: ubuntu-latest
15-
# Timeout after 5 minutes, to avoid hanging tests
1615
timeout-minutes: 5
1716
steps:
1817
- name: Checkout 🛎️
19-
uses: actions/checkout@v3
18+
uses: actions/checkout@v4
2019
with:
2120
fetch-depth: 0
2221

2322
- name: Install Go 🧑‍💻
24-
uses: actions/setup-go@v3
23+
uses: actions/setup-go@v5
2524
with:
2625
go-version: "1.24"
2726

2827
- name: Lint code issues 🚨
2928
uses: golangci/golangci-lint-action@v6
3029

30+
- name: Run govulncheck 🔒
31+
uses: golang/govulncheck-action@v1
32+
with:
33+
go-version-input: "1.24"
34+
3135
- name: Run Go tests 🔬
3236
run: go test -cover -covermode atomic -coverprofile=profile.cov -v ./...
3337

.golangci.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ linters-settings:
3535
- "github.com/prometheus/client_golang"
3636
- "github.com/hashicorp/go-plugin"
3737
- "github.com/hashicorp/go-hclog"
38-
- "github.com/go-redis/redis/v8"
38+
- "github.com/redis/go-redis/v9"
3939
- "github.com/getsentry/sentry-go"
4040
- "github.com/spf13/cast"
4141
- "github.com/go-co-op/gocron"

go.mod

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,59 @@
11
module github.com/gatewayd-io/gatewayd-plugin-cache
22

3-
go 1.24
3+
go 1.24.0
44

55
require (
6-
github.com/alicebob/miniredis/v2 v2.35.0
7-
github.com/gatewayd-io/gatewayd v0.10.2
8-
github.com/gatewayd-io/gatewayd-plugin-sdk v0.4.3
9-
github.com/getsentry/sentry-go v0.35.0
6+
github.com/alicebob/miniredis/v2 v2.36.1
7+
github.com/gatewayd-io/gatewayd v0.11.0
8+
github.com/gatewayd-io/gatewayd-plugin-sdk v0.4.4
9+
github.com/getsentry/sentry-go v0.42.0
1010
github.com/go-co-op/gocron v1.37.0
11-
github.com/go-redis/redis/v8 v8.11.5
1211
github.com/hashicorp/go-hclog v1.6.3
13-
github.com/hashicorp/go-plugin v1.6.3
14-
github.com/jackc/pgx/v5 v5.7.5
15-
github.com/prometheus/client_golang v1.23.0
16-
github.com/spf13/cast v1.9.2
17-
github.com/stretchr/testify v1.10.0
12+
github.com/hashicorp/go-plugin v1.7.0
13+
github.com/jackc/pgx/v5 v5.8.0
14+
github.com/prometheus/client_golang v1.23.2
15+
github.com/redis/go-redis/v9 v9.18.0
16+
github.com/spf13/cast v1.10.0
17+
github.com/stretchr/testify v1.11.1
1818
github.com/zenizh/go-capturer v0.0.0-20211219060012-52ea6c8fed04
19-
google.golang.org/grpc v1.74.2
20-
google.golang.org/protobuf v1.36.7
19+
google.golang.org/grpc v1.79.1
20+
google.golang.org/protobuf v1.36.11
2121
)
2222

2323
require (
2424
github.com/beorn7/perks v1.0.1 // indirect
2525
github.com/cespare/xxhash/v2 v2.3.0 // indirect
2626
github.com/davecgh/go-spew v1.1.1 // indirect
2727
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
28-
github.com/expr-lang/expr v1.17.5 // indirect
28+
github.com/expr-lang/expr v1.17.8 // indirect
2929
github.com/fatih/color v1.18.0 // indirect
3030
github.com/golang/protobuf v1.5.4 // indirect
3131
github.com/google/uuid v1.6.0 // indirect
32-
github.com/grpc-ecosystem/grpc-gateway/v2 v2.26.3 // indirect
32+
github.com/grpc-ecosystem/grpc-gateway/v2 v2.27.8 // indirect
3333
github.com/hashicorp/yamux v0.1.2 // indirect
3434
github.com/mattn/go-colorable v0.1.14 // indirect
3535
github.com/mattn/go-isatty v0.0.20 // indirect
3636
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
3737
github.com/oklog/run v1.1.0 // indirect
38-
github.com/onsi/gomega v1.34.1 // indirect
39-
github.com/pganalyze/pg_query_go/v6 v6.1.0 // indirect
38+
github.com/pganalyze/pg_query_go/v6 v6.2.2 // indirect
4039
github.com/pmezard/go-difflib v1.0.0 // indirect
4140
github.com/prometheus/client_model v0.6.2 // indirect
42-
github.com/prometheus/common v0.65.0 // indirect
41+
github.com/prometheus/common v0.67.5 // indirect
4342
github.com/prometheus/procfs v0.16.1 // indirect
44-
github.com/redis/go-redis/v9 v9.12.0 // indirect
4543
github.com/robfig/cron/v3 v3.0.1 // indirect
46-
github.com/rogpeppe/go-internal v1.13.1 // indirect
44+
github.com/rogpeppe/go-internal v1.14.1 // indirect
4745
github.com/rs/zerolog v1.34.0 // indirect
4846
github.com/tetratelabs/wazero v1.9.0 // indirect
4947
github.com/wasilibs/go-pgquery v0.0.0-20250409022910-10ac41983c07 // indirect
5048
github.com/wasilibs/wazero-helpers v0.0.0-20250123031827-cd30c44769bb // indirect
5149
github.com/yuin/gopher-lua v1.1.1 // indirect
50+
go.opentelemetry.io/otel/sdk/metric v1.40.0 // indirect
5251
go.uber.org/atomic v1.11.0 // indirect
53-
golang.org/x/net v0.40.0 // indirect
54-
golang.org/x/sys v0.33.0 // indirect
55-
golang.org/x/text v0.25.0 // indirect
56-
google.golang.org/genproto/googleapis/api v0.0.0-20250528174236-200df99c418a // indirect
57-
google.golang.org/genproto/googleapis/rpc v0.0.0-20250528174236-200df99c418a // indirect
52+
go.yaml.in/yaml/v2 v2.4.3 // indirect
53+
golang.org/x/net v0.49.0 // indirect
54+
golang.org/x/sys v0.40.0 // indirect
55+
golang.org/x/text v0.34.0 // indirect
56+
google.golang.org/genproto/googleapis/api v0.0.0-20260209200024-4cfbd4190f57 // indirect
57+
google.golang.org/genproto/googleapis/rpc v0.0.0-20260209200024-4cfbd4190f57 // indirect
5858
gopkg.in/yaml.v3 v3.0.1 // indirect
5959
)

0 commit comments

Comments
 (0)