Help us improve
Share bugs, ideas, or general feedback.
From golang-boost
Guides Go developers using the boost framework to subscribe to GCP Pub/Sub, covering the canonical apubsub.New path and a production workaround for the helper.go context.Background bug that prevents SIGTERM draining.
npx claudepluginhub xgodev/boost --plugin golang-boostHow this skill is triggered — by the user, by Claude, or both
Slash command
/golang-boost:boost-bootstrap-adapter-pubsubThis skill is limited to the following tools:
The summary Claude sees in its skill listing — used to decide when to auto-load this skill
**REQUIRED BACKGROUND:**
Constructs a Google Cloud Pub/Sub client using the boost factory wrapper, covering NewClient, lifecycle (defer Close), and sharing the client between publisher and subscriber adapters.
Writes idiomatic Go with goroutines, channels, interfaces, generics, and robust error handling. Optimizes performance with pprof and enforces production-grade testing patterns.
Implements concurrent Go patterns with goroutines/channels, builds gRPC/REST microservices, optimizes performance with pprof, enforces idiomatic Go via generics/interfaces/testing.
Share bugs, ideas, or general feedback.
REQUIRED BACKGROUND:
boost-bootstrap-function — handler typing rule.boost-factory-pubsub — *pubsub.Client construction.boost-bootstrap-middleware — recovery/logger/publisher chain.boost-extra-middleware — NewAnyErrorWrapper for the workaround path.import (
apubsub "github.com/xgodev/boost/bootstrap/function/adapter/contrib/cloud.google.com/pubsub/v1"
"github.com/xgodev/boost/bootstrap/function"
)
fn, _ := function.New[*cloudevents.Event](rec, lmi, pmi)
fn.Run(ctx, handle, apubsub.New[*cloudevents.Event](pb))
Fine for prototypes. Not safe for production graceful shutdown — see next section.
bootstrap/function/adapter/contrib/cloud.google.com/pubsub/v1/helper.go:51 hard-codes:
if err := subscriber.Subscribe(context.Background()); err != nil { ... }
A signal.NotifyContext passed to fn.Run(ctx, ...) reaches the middleware wrapper but not the subscription receive loop. SIGTERM does not gracefully drain in-flight messages.
The same shape exists in the NATS and Kafka adapter helpers.
Bypass fn.Run and drive NewSubscriber directly with a signal-aware ctx:
import (
apubsub "github.com/xgodev/boost/bootstrap/function/adapter/contrib/cloud.google.com/pubsub/v1"
"github.com/xgodev/boost/extra/middleware"
"github.com/xgodev/boost/bootstrap/function"
)
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
opts, err := apubsub.DefaultOptions()
if err != nil { log.Fatalf("subscriber options: %v", err) }
// TODO(boost-upstream): bootstrap/function/adapter/contrib/cloud.google.com/pubsub/v1/helper.go:51
// hard-codes context.Background(), so fn.Run cannot drain SIGTERM. Bypassing
// helper here and driving NewSubscriber with a signal-aware ctx + the same
// recovery → logger → publisher chain. Collapse back to fn.Run once the
// helper accepts a ctx parameter (track upstream issue).
wrp := middleware.NewAnyErrorWrapper[*cloudevents.Event](
ctx, "bootstrap", rec, lmi, pmi,
)
wrappedHandler := function.Wrapper[*cloudevents.Event](wrp, handle)
var wg sync.WaitGroup
for _, sub := range opts.Subscriptions {
wg.Add(1)
go func(name string) {
defer wg.Done()
s := apubsub.NewSubscriber[*cloudevents.Event](pb, wrappedHandler, name, opts)
if err := s.Subscribe(ctx); err != nil && !errors.Is(err, context.Canceled) {
log.WithField("subscription", name).Errorf("subscriber exited: %v", err)
}
}(sub)
}
<-ctx.Done()
wg.Wait()
The // TODO(boost-upstream): block is not optional documentation — it's the marker that says "this is a workaround for a known upstream issue, not a stylistic choice". Without it:
fn.Run.boost-upstream to inventory all such workarounds across the codebase.| Red flag | Fix |
|---|---|
pubsub.Client.Subscription(...).Receive(...) directly | Use apubsub.NewSubscriber(...).Subscribe(ctx) |
Bypass of fn.Run without // TODO(boost-upstream): naming helper.go:51 | Add the comment, OR use canonical fn.Run and accept ungraceful shutdown |
apubsub.New[T] returning T = cloudevents.Event (value) | T = *cloudevents.Event everywhere in the chain |