{"id":51082987,"url":"https://github.com/kanutocd/sidekiq-tenant-policy-cache","last_synced_at":"2026-06-23T20:00:56.225Z","repository":{"id":362897556,"uuid":"1261200954","full_name":"kanutocd/sidekiq-tenant-policy-cache","owner":"kanutocd","description":"Sidekiq middleware for caching tenant policy decisions with `Ratomic::Map` and tracking middleware counters with `Ratomic::Counter`","archived":false,"fork":false,"pushed_at":"2026-06-06T11:46:49.000Z","size":8,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2026-06-06T13:20:34.665Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Ruby","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/kanutocd.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"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}},"created_at":"2026-06-06T11:23:30.000Z","updated_at":"2026-06-06T11:46:53.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/kanutocd/sidekiq-tenant-policy-cache","commit_stats":null,"previous_names":["kanutocd/sidekiq-tenant-policy-cache"],"tags_count":null,"template":false,"template_full_name":null,"purl":"pkg:github/kanutocd/sidekiq-tenant-policy-cache","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kanutocd%2Fsidekiq-tenant-policy-cache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kanutocd%2Fsidekiq-tenant-policy-cache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kanutocd%2Fsidekiq-tenant-policy-cache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kanutocd%2Fsidekiq-tenant-policy-cache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kanutocd","download_url":"https://codeload.github.com/kanutocd/sidekiq-tenant-policy-cache/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kanutocd%2Fsidekiq-tenant-policy-cache/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34704748,"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-06-23T02:00:07.161Z","response_time":65,"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":[],"created_at":"2026-06-23T20:00:52.613Z","updated_at":"2026-06-23T20:00:56.220Z","avatar_url":"https://github.com/kanutocd.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Sidekiq Tenant Policy Cache\n\nThis is focused Sidekiq middleware for caching tenant policy decisions with\n`Ratomic::Map` and tracking middleware counters with `Ratomic::Counter`, without\nchanging Sidekiq job execution semantics for existing users.\n\nThe strongest fit is tenant policy state at the middleware boundary:\n\n- `Ratomic::Map` caches immutable allow/deny decisions by tenant, job class, and\n  queue. Sidekiq creates a new middleware instance per job, so the cache lives in\n  an explicit shared `State` object.\n- `Ratomic::Counter` tracks cache hits, misses, allowed jobs, denied jobs,\n  completions, errors, retries, and bypasses.\n\nThe middleware is opt-in. If no policy is configured, or a job has no tenant, it\nbypasses the policy and yields normally.\n\n## Usage\n\n```ruby\nrequire \"sidekiq_tenant_policy_cache\"\n\nSTATE = SidekiqTenantPolicyCache::TenantPolicyMiddleware::State.new\nPOLICY = lambda do |tenant:, job_class:, queue:, args:, msg:|\n  tenant != \"suspended\"\nend\n\nSidekiq.configure_client do |config|\n  config.client_middleware do |chain|\n    chain.add SidekiqTenantPolicyCache::TenantPolicyMiddleware::Client,\n      policy: POLICY,\n      state: STATE\n  end\nend\n\nSidekiq.configure_server do |config|\n  config.server_middleware do |chain|\n    chain.add SidekiqTenantPolicyCache::TenantPolicyMiddleware::Server,\n      policy: POLICY,\n      state: STATE\n  end\nend\n```\n\nThe middleware looks for `tenant_id` on the job payload first, then in the first\nargument when it is hash-like.\n\nThe policy itself stays a plain Ruby lambda. `Ratomic::Map` caches the computed\nallow/deny decision, so the middleware keeps the decision logic simple while the\nshared state remains atomic and cacheable.\n\n## Limits\n\nThis middleware intentionally does not persist policy decisions, publish metrics, or\nreplace Sidekiq's built-in retry, stats, or Redis behavior. Cached policy values\nshould be small immutable Ruby values such as booleans or symbols.\n\nThe benchmark simulates a policy lookup expensive enough to be worth caching. It\nis not a claim that middleware caching improves a trivial in-memory predicate.\n\n## Verification\n\n```bash\nbundle exec rake\nbundle exec ruby benchmark/tenant_policy_middleware.rb\n```\n\n## Benchmark Knob\n\nThe benchmark uses `ITERATIONS` to control how many jobs it simulates:\n\n```bash\nITERATIONS=100000 bundle exec ruby benchmark/tenant_policy_middleware.rb\n```\n\n## Benchmark Snapshot\n\nOn this machine, with the current code and `ITERATIONS=25000`, the benchmark\nreported:\n\n```text\npolicy lookup every job        0.871869   0.000000   0.871869 (  0.872057)\nratomic middleware cached      0.053152   0.000000   0.053152 (  0.053168)\n{cache_hits: 24999, cache_misses: 1, allowed: 25000, denied: 0, completed: 0, errors: 0, retried: 0, bypassed: 0}\n```\n\nThat is the point of the middleware: repeated policy decisions for the same\ntenant/job/queue combination can be cached once and reused many times.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkanutocd%2Fsidekiq-tenant-policy-cache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkanutocd%2Fsidekiq-tenant-policy-cache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkanutocd%2Fsidekiq-tenant-policy-cache/lists"}