{"id":50362366,"url":"https://github.com/adrielcodeco/gorm-autobatch","last_synced_at":"2026-05-30T02:30:28.648Z","repository":{"id":357410238,"uuid":"1236824732","full_name":"adrielcodeco/gorm-autobatch","owner":"adrielcodeco","description":"GORM plugin that automatically switches between individual and batch database operations based on measured P95 latency — reducing round-trips when your database is slow.","archived":false,"fork":false,"pushed_at":"2026-05-12T16:15:14.000Z","size":14,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2026-05-12T18:14:52.754Z","etag":null,"topics":["batch","database","go","golang","gorm","gorm-plugin","orm","performance"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/adrielcodeco.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null},"funding":{"github":"adrielcodeco"}},"created_at":"2026-05-12T15:55:03.000Z","updated_at":"2026-05-12T16:52:21.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/adrielcodeco/gorm-autobatch","commit_stats":null,"previous_names":["adrielcodeco/gorm-autobatch"],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/adrielcodeco/gorm-autobatch","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adrielcodeco%2Fgorm-autobatch","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adrielcodeco%2Fgorm-autobatch/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adrielcodeco%2Fgorm-autobatch/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adrielcodeco%2Fgorm-autobatch/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/adrielcodeco","download_url":"https://codeload.github.com/adrielcodeco/gorm-autobatch/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adrielcodeco%2Fgorm-autobatch/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":33678270,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-05-30T02:00:06.278Z","response_time":92,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["batch","database","go","golang","gorm","gorm-plugin","orm","performance"],"created_at":"2026-05-30T02:30:28.109Z","updated_at":"2026-05-30T02:30:28.643Z","avatar_url":"https://github.com/adrielcodeco.png","language":"Go","funding_links":["https://github.com/sponsors/adrielcodeco"],"categories":[],"sub_categories":[],"readme":"# gorm-autobatch\n\nA GORM plugin that automatically switches between individual and batch database operations based on measured P95 latency.\n\nWhen latency is high, operations are buffered and flushed as a single transaction — reducing round-trips. When latency is low, operations are sent individually with no overhead.\n\n## How it works\n\n- Tracks operation latency using a **P95 sliding window** (Prometheus-style bucket ring, last 30s by default)\n- When `P95 \u003e LatencyThreshold` → **batch mode**: operations are buffered and flushed together\n- When `P95 ≤ LatencyThreshold` → **individual mode**: operations pass through normally\n- Flush triggers: elapsed time **or** buffer size, whichever comes first\n- Batch semantics are **all-or-nothing** inside a single transaction\n\n## Install\n\n```bash\ngo get github.com/adrielcodeco/gorm-autobatch\n```\n\n\u003e Requires Go 1.21+ and GORM v1.30+\n\n## Usage\n\n```go\nimport (\n    autobatch \"github.com/adrielcodeco/gorm-autobatch\"\n    \"gorm.io/gorm\"\n)\n\ndb, err := gorm.Open(...)\n\nthreshold := 50 * time.Millisecond\nerr = db.Use(autobatch.New(autobatch.Config{\n    LatencyThreshold: \u0026threshold,            // nil = disabled; 0 = always batch; \u003e0 = adaptive\n    FlushTimeout:     10 * time.Millisecond, // flush batch after 10ms idle\n    MaxBatchSize:     100,                   // or when 100 ops are buffered\n    WindowDuration:   30 * time.Second,      // P95 measured over last 30s\n}))\n\n// Regular GORM calls — the plugin decides whether to batch transparently.\ndb.Create(\u0026user)\ndb.Model(\u0026user).Updates(\u0026payload)\ndb.Delete(\u0026record)\n```\n\n## Config\n\n| Field | Default | Description |\n|---|---|---|\n| `LatencyThreshold` | `nil` | P95 above this switches to batch mode (`nil` disables batching) |\n| `FlushTimeout` | `10ms` | Max wait before flushing a partial batch |\n| `MaxBatchSize` | `100` | Max ops per batch before forced flush |\n| `WindowDuration` | `30s` | Sliding window duration for P95 measurement |\n\nAll fields are optional — zero values use the defaults above.\n\n## Supported operations\n\n- `db.Create()`\n- `db.Updates()`\n- `db.Delete()`\n\nQueries (`Find`, `First`, etc.) are not batched.\n\n## Batch semantics\n\nAll operations in a batch run inside a **single transaction**. Each op is\nwrapped in its own `SAVEPOINT`, so a per-op failure (e.g. a unique-constraint\nviolation) is isolated: only the failing caller gets the error, the rest of\nthe batch still commits.\n\nInfrastructure failures of the outer transaction (BEGIN/COMMIT, connection\nloss, savepoint unsupported) propagate to every caller in the batch.\n\nCallers block transparently until their batch is flushed — from the caller's\nperspective it looks like a normal synchronous GORM call.\n\n## Limitations \u0026 caveats\n\n- **Operations inside `db.Transaction(...)` or `db.Begin()` are never batched.**\n  They run inline on the user's transaction to preserve atomicity and rollback\n  semantics. The plugin detects this automatically.\n- **Callbacks registered *after* `gorm:create`/`gorm:update`/`gorm:delete` are\n  skipped for batched ops.** Internally the plugin sets `DryRun = true` after\n  the batch executes the op via a separate session, which short-circuits the\n  rest of the callback chain on the caller's `*gorm.DB`. Hooks declared on the\n  model (e.g. `AfterCreate`) **do** run, but inside the batch session — not on\n  the caller's `*gorm.DB`.\n- **`RowsAffected` is propagated** back to the caller's `*gorm.DB` after the\n  batch executes, but other `Statement` fields are not.\n- **Graceful shutdown:** call `plugin.Close()` before exiting your process to\n  drain in-flight batches. After Close, intercepted ops return\n  `ErrBatcherClosed`.\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fadrielcodeco%2Fgorm-autobatch","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fadrielcodeco%2Fgorm-autobatch","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fadrielcodeco%2Fgorm-autobatch/lists"}