Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

perf(events): cache compiled event schemas and avoid string copy on validation - #2022

Open
Tyagiquamar wants to merge 1 commit into
formancehq:mainfrom
Tyagiquamar:perf/cache-compiled-event-schemas
Open

Tyagiquamar wants to merge 1 commit into
formancehq:mainfrom
Tyagiquamar:perf/cache-compiled-event-schemas

Conversation

@Tyagiquamar

Copy link
Copy Markdown
Contributor

Summary

Cache compiled *gojsonschema.Schema instances in events and pass raw []byte payloads directly to gojsonschema.NewBytesLoader during validation.

Root Cause & Problem

ComputeSchema, ComputeSchemaForVersion, Check, and CheckForVersion previously re-read embedded YAML files, unmarshaled YAML into maps, marshaled back to JSON, and compiled a fresh gojsonschema.Schema on every single call. In addition, validate converted data to string via gojsonschema.NewStringLoader(string(data)), allocating a full copy of every validated event payload. Because all service schemas and base.yaml are embedded at compile time via //go:embed, recompiling on every validation adds unnecessary CPU and allocation overhead.

Technical Solution

  • Added sync.Map caches (versionedSchemaCache and latestSchemaCache) in events/events.go keyed by (service, version, event) and (service, event) respectively.
  • Only successful schema compilations are stored via LoadOrStore so invalid/unknown service, version, or event lookups do not populate the cache.
  • Updated validate in events/events.go to use gojsonschema.NewBytesLoader(data) directly.
  • Added regression and concurrency tests in events/events_test.go:
    • TestComputeSchemaCachesCompiledSchemas: verifies repeated calls to ComputeSchema and ComputeSchemaForVersion return the same cached *gojsonschema.Schema instance, and error cases are not cached.
    • TestConcurrentCheckAndComputeSchema: exercises concurrent ComputeSchema, ComputeSchemaForVersion, Check, and CheckForVersion calls across 16 goroutines under -race.

Test Plan

Executed in D:\pers\open source\stack\events inside golang:1.27.4-trixie (--memory=1500m):

gofmt -l .
go vet ./...
go test -race -v ./...

Observed results:

  • go vet ./...: passed with exit code 0.
  • go test -race -v ./...: all tests passed (ok github.com/formancehq/stacks/libs/events 2.668s), including TestComputeSchemaCachesCompiledSchemas, TestConcurrentCheckAndComputeSchema, TestGatewayAuditEventSchemas, TestGatewayAuditRejectsInvalidEvents, TestGatewayAuditAcceptsCaptureLimit, TestGatewayAuditAcceptsLegacyHTTPAndAdditiveFields, TestReconciliationAlertEventSchemas, TestLedgerV3EventSchemas, TestEmbeddedServicePathsUseForwardSlashes, and TestComputeSchemaUsesLatestVersionContainingEvent.

@NumaryBot NumaryBot added risk: medium NumaryBot classified this pull request as medium risk. bot-reviewed NumaryBot completed its review workflow for the current head. review-approved The NumaryBot review gate is satisfied for the current head. labels Nov 26, 2026

@NumaryBot NumaryBot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The required automated review completed with no remaining findings.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot-reviewed NumaryBot completed its review workflow for the current head. review-approved The NumaryBot review gate is satisfied for the current head. risk: medium NumaryBot classified this pull request as medium risk.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants