#4371·gofr

Expose Google Pub/Sub consumer flow control (`ReceiveSettings`)

Author: Suhas-zsCreated Sep 24, 2026Updated Sep 24, 2026
Labelstriage

Is your feature request related to a problem? Please describe.

gofr never sets ReceiveSettings on the Google Pub/Sub subscription, and offers no way to override it. getSubscription (google.go:345) hands the subscription straight to subscription.Receive (google.go:263) with SDK defaults — chiefly MaxOutstandingMessages = 1000.

That default is amplified by how gofr consumes:

  • Receive leases up to 1000 messages and calls the handler concurrently.
  • gofr funnels every one through an unbuffered channel (google.go:230) drained by a single goroutine per topic.
  • So ~1000 messages sit leased, each parking a goroutine blocked on the send, while the handler processes them one at a time.

The effect is a permanent goroutine/memory floor and a consumer-latency tail, with no knob to cap it. This is a regression — the pre-gofr framework let services set MaxOutStandingMessages directly.

Real impact: an internal Solr indexer hit sustained index-latency from this, and — unable to reach the cap upstream — had to ship an opt-in in-house Pub/Sub subscriber as a documented stopgap, to be retired once gofr exposes ReceiveSettings.

Reproduce — publish ~1000 messages, then subscribe with a handler that does real work:

go
app := gofr.New()

app.Subscribe("product-change", func(c *gofr.Context) error {
    time.Sleep(200 * time.Millisecond) // real work
    return nil
})

app.Run()

A goroutine dump mid-burst shows the plateau — ~1000 goroutines parked on the unbuffered send at google.go:257, one per leased message:

goroutine profile: total 1103
1000 @ ...
#   gofr.dev/.../google.(*googleClient).startTopicSubscriber.func1   .../google.go:257   [chan send]
#   cloud.google.com/go/pubsub.(*messageIterator).receive.func1      .../pubsub@v1/iterator.go
#   cloud.google.com/go/pubsub.(*Subscription).Receive.func3         .../pubsub@v1/subscription.go

This is not theoretical. In production the per-pod goroutine count sits flat at 1105–1131 for two weeks straight (screenshot in Additional context) — a standing backlog, not a leak.

Describe the solution you'd like

Add optional flow-control fields to google.Config (zero-value ⇒ SDK default, so existing apps are unchanged):

go
type Config struct {
    ProjectID        string
    SubscriptionName string

    // Optional pull flow control; zero falls back to the SDK default.
    MaxOutstandingMessages int
    MaxOutstandingBytes    int
    NumGoroutines          int
}

Apply them in getSubscription before Receive, and wire from env in container.createGooglePubSub using the existing kafka GetOrDefault pattern (GOOGLE_MAX_OUTSTANDING_MESSAGES, GOOGLE_MAX_OUTSTANDING_BYTES, GOOGLE_NUM_GOROUTINES).

Docs note: NumGoroutines is the number of StreamingPull streams, not handler parallelism — worth stating so it isn't mistuned.

Describe alternatives you've considered

  • In-house subscriber fork (what the affected team did) — works, but duplicate maintenance, pins pubsub v1, and must re-derive gofr's subscription name to stay a drop-in.
  • Lower gofr's default below 1000 — breaking and equally opinionated; configurability with an unchanged default is the non-breaking path.
  • Docs only — restores nothing; there's no knob to point at.

Additional context

Production evidence — Go Routines By Pod, Grafana:

Image

Three pod revisions, each pinned at 1105–1131 goroutines across ~09/01–09/14 — flat, never idling down, never drifting up: the fingerprint of 1000 leased messages held by the default cap plus SDK/app overhead. Set MaxOutstandingMessages=100 and this floor drops proportionally.

  • Scope: just the config knobs. The deeper issue — the single-drainer bridge caps effective concurrency at 1/topic regardless of NumGoroutines — is a separate follow-up, intentionally not bundled here.
  • Adjacent, separate: #4154 (the shared-end data race) touches the same function; neither fixes the other.
  • Consistency: kafka already exposes flow knobs via env; this closes the gap for Google.
  • Env: gofr v1.61.0, go 1.26, pubsub v1.