feat: Webhook caching and assorted improvements#36
Open
Conversation
…aming - Add LoaderCache (LRU + singleflight) and CachingWebhooksRepository - Add RetryingWebhookDispatchInserter with exponential backoff for enqueue - Consolidate observability into Metrics (Events, Webhooks, Cache) - Rename repository structs to DBWebhooksRepository / DBFeedbackRecordsRepository - Document repository pattern in AGENTS.md - Integration tests use DATABASE_URL from env when set; make tests loads .env - New config: WEBHOOK_CACHE_SIZE, enqueue retry backoff env vars - Docs: environment-variables, metrics, event-pipeline updates Co-authored-by: Cursor <cursoragent@cursor.com>
✱ Stainless preview buildsThis PR will update the Edit this comment to update it. It will appear in the SDK's changelogs. ✅ hub-typescript studio · code · diff
This comment is auto-generated by GitHub Actions and is automatically kept up to date as you push. |
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 join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description
This PR adds webhook list/GetByID caching with singleflight, retrying webhook enqueue with exponential backoff, consolidates observability into a single
Metricstype, and aligns repository naming and integration tests with local Docker setup.Changes
Webhook caching
pkg/cache/loader_cache.go: New genericLoaderCache(LRU + singleflight) for cache-on-miss with request coalescing.internal/service/caching_webhooks_repo.go:CachingWebhooksRepositorydecorator cachesListEnabledForEventTypeandGetByID; invalidates on Create/Update/Delete.WEBHOOK_CACHE_SIZE(default 4096). Documented in environment variables and metrics docs.Retrying webhook enqueue
internal/service/retrying_webhook_inserter.go:RetryingWebhookDispatchInserterwraps River client with exponential backoff + jitter onInsertManyfailures.WEBHOOK_ENQUEUE_MAX_RETRIES,WEBHOOK_ENQUEUE_INITIAL_BACKOFF,WEBHOOK_ENQUEUE_MAX_BACKOFF(defaults: 3, 100ms, 2s). Documented in env and event-pipeline docs.hub_webhook_enqueue_retries_totalcounter for retry metrics.Observability
internal/observability/aggregate.go: NewMetricsstruct aggregating Events, Webhooks, and Cache metrics.internal/observability/cache.go: Cache metrics (hit/miss by cache name).cmd/api/app.go: Uses singleobservability.NewMetrics(); wiring updated for cache metrics and retrying inserter.Repository naming and docs
repository.WebhooksRepository(struct) renamed toDBWebhooksRepository; constructorNewDBWebhooksRepository. Interface remainsservice.WebhooksRepository.DBFeedbackRecordsRepository/NewDBFeedbackRecordsRepository.Integration tests
tests/integration_test.go: UseDATABASE_URLfrom environment when set (e.g..envwith port 5433); otherwise defaultlocalhost:5432/test_dbfor CI.make testsexports.envbefore runninggo test, so local runs hit the same Postgres as dev.Other
.golangci.yml: Lint/config tweaks as needed.environment-variables.mdx,metrics.mdx, and related reference docs updated for new config and metrics.Testing
pkg/cache:go test ./pkg/cache/... -count=1✅internal/service:go test ./internal/service/... -count=1✅ (includes caching_webhooks_repo and retrying_webhook_inserter)internal/repository:go test ./internal/repository/... -count=1✅make docker-up; use same port as in.env):make tests✅ (loads.env, all tests pass)Evidence:
make tests # PASS; ok github.com/formbricks/hub/tests <1sChecklist
make fmt,make lint).