This repository was archived by the owner on Nov 9, 2026. It is now read-only.
Repository navigation
fix: Always close cache-lib-go Pub/Sub subscriptions [DEVOP-9863] - #33
Merged
ChristianWitts merged 1 commit intoNov 9, 2026
Merged
Conversation
Each waiter in RememberBlocking opened its own Pub/Sub subscription. go-redis v8 dials that connection outside the pool, and UnsubscribeAndClose returned before Close when UNSUBSCRIBE failed, for example after a connection reset or a cancelled request. The receive goroutine of go-redis then redials forever. In PD #20731 this kept Memorystore at its connection limit, and fnx-api-gateway pods stayed at about 3.5 times their usual goroutine count after the traffic dropped. We now always close the subscription and clear it, also when UNSUBSCRIBE fails, on a context that the request cannot cancel, with a 1 s limit. A mutex makes the cleanup run once. Unsubscribe also closes, because it dropped the PubSub without Close. The wait uses a select with a context timeout in place of the timeout goroutine. We also collapse concurrent waits for the same client, key and SubscriptionTimeout with singleflight, so one pod holds at most one Pub/Sub wait per key and timeout. The public API does not change; UnsubscribeAndClose in CacheOptions now has no effect, because the cleanup always closes. singleflight adds golang.org/x/sync v0.24.0, which needs go 1.26.0 in go.mod. This mirrors the same change in the monorepo copy of the library, so both copies behave the same until the cutover. Refs: #DEVOP-9863 Signed-off-by: Christian Witts <christian@honestbank.com>
|
alexanderjpowell
approved these changes
Nov 9, 2026
ChristianWitts
enabled auto-merge (squash)
November 9, 2026 08:31
kelu-diao
approved these changes
Nov 9, 2026
ChristianWitts
deleted the
christian/devop-9863-cache-lib-go-always-close-pubsub
branch
November 9, 2026 08:38
Contributor
|
🎉 This PR is included in version 1.4.2 🎉 The release is available on GitHub release Your semantic-release bot 📦🚀 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Pull Request Submission Checklist
Please confirm that you have done the following before requesting reviews:
Description
Each waiter in RememberBlocking opened its own Pub/Sub subscription.
go-redis v8 dials that connection outside the pool, and
UnsubscribeAndClose returned before Close when UNSUBSCRIBE failed, for
example after a connection reset or a cancelled request. The receive
goroutine of go-redis then redials forever. In PD #20731 this kept
Memorystore at its connection limit, and fnx-api-gateway pods stayed at
about 3.5 times their usual goroutine count after the traffic dropped.
We now always close the subscription and clear it, also when
UNSUBSCRIBE fails, on a context that the request cannot cancel, with a
1 s limit. A mutex makes the cleanup run once. Unsubscribe also closes,
because it dropped the PubSub without Close. The wait uses a select
with a context timeout in place of the timeout goroutine.
We also collapse concurrent waits for the same client, key and
SubscriptionTimeout with singleflight, so one pod holds at most one
Pub/Sub wait per key and timeout. The public API does not change;
UnsubscribeAndClose in CacheOptions now has no effect, because the
cleanup always closes. singleflight adds golang.org/x/sync v0.24.0,
which needs go 1.26.0 in go.mod.
This mirrors the same change in the monorepo copy of the library, so
both copies behave the same until the cutover.
Refs: #DEVOP-9863
Signed-off-by: Christian Witts christian@honestbank.com
No experiment because this is a bug fix in a shared library.
Validation
cache.go,cache_subscription.goandcache_internal_test.goare byte-identical to the monorepo copy ingo/libs/cache-lib-go. The existingcache_test.gopasses without changes.GOTOOLCHAIN=go1.26.10 go test -race -count=1 ./...: ok (11 top-level tests, including the 6 new ones incache_internal_test.go).golangci-lint run ./...(v2.15.0, repository.golangci.json): 0 issues.golangci-lint fmt --diff ./...: no diff.go vet ./...: clean.go mod tidyin the root and inexamples/pubsub: no diff afterwards.examples/pubsubuses areplaceto the root module, so itsgo.mod/go.sumnow listgolang.org/x/sync;go build ./...there passes.Mutation check: with the old early return on an UNSUBSCRIBE error,
TestUnsubscribeAndClose_ClosesWhenUnsubscribeFailsandTestUnsubscribeAndClose_ConcurrentCallsfail.go mod tidyalso dropsgithub.com/golang/mockfrom the rootgo.mod: no committed code imports it.make generatestill installsmockgen@v1.6.0itself.The pre-commit hooks were not run:
.pre-commit-config.yamlpins golangci-lint v1.63.4, which cannot read the v2.golangci.json. We ran golangci-lint v2.15.0,go vet,go mod tidyandgo test -racedirectly instead.