Exemple
golang.org/x/time v0.11.0: token bucket rate limiting with Wait and Allow
Échantillon vérifié pour golang golang.org/x/time v0.11.0: token bucket rate limiting with Wait and Allow. Le contrat s'est exécuté sur go 1.26 · linux…
sha256:6731d057d62d179cd8b25bdd0ed5bf80f0ae0aabcaa55a5c61941d23ae85488a
Ce réseau offre une seule chose : un échantillon qui compile. Il l'a exécuté dans un bac à sable et conservé le reçu signé. Il ne note rien et ne garantit rien : si le même code compile chez vous, il ne l'a pas mesuré.
Combien de clés de signature distinctes ont déposé un reçu de contrat réussi. Une seule, c'est l'auteur ; plus d'une signifie que quelqu'un d'autre l'a compilé aussi. Une clé est auto-générée sans identité enregistrée derrière, donc on compte des clés, pas des personnes.
MIT-0
Preuves d'exécution
L'environnement déclaré et les exécutions signées sont séparés, pour que vous voyiez exactement ce que cet échantillon a exécuté et où.
- Base de preuve
- Contrat signé réussi
- Reçus de vérification
- 2
- Clés de signature qui l’ont compilé
- 1
Environnement déclaré
linux 24 · ubuntu · glibc 2.39 amd64 go 1.26 go 1
Environnements des exécutions de vérification
| Environnement | Contrat | Étapes | Exécution |
|---|---|---|---|
| go 1.26 · linux alpine/x64 · docker ed25519:c1973797be207ac4 | PASS | compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS CONTAINER_RUN · golang@1golang:1.26-alpine@sha256:28d89ee9cc0f… |
2026-08-23 |
| go 1.26 · linux debian/x64 · docker ed25519:c1973797be207ac4 | PASS | compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS CONTAINER_RUN · golang@1golang:1.26@sha256:e30143be198a… |
2026-09-07 |
Cas
HOW- Objectif
- token bucket rate limiting with Wait and Allow
- Paquets
- Symboles
-
- golang.org/x/time/rate.Limiter.Allow
- golang.org/x/time/rate.Limiter.Wait
- golang.org/x/time/rate.NewLimiter
- Créé
- 2026-08-23T16:25:22Z
Contrat
- limiter := rate.NewLimiter(rate.Limit(10), 1)
- limiter.Allow() == true
- limiter.Allow() == false
- ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond)
- limiter.Wait(ctx) == nil
Fichiers
- PROMPT.md
- csx.json
- go.mod
- go.sum
- ratelimit.go
- spec.json
- test/contract_test.go
Code source
Clean-room public code sample — generation instructions
Write a brand-new, minimal, self-contained code sample in this clean-room directory.
Do not copy, paraphrase, or reference any existing project source. Work only from this spec.
A csx.json manifest scaffold already exists. Do not recreate it from memory. Preserve its case.goal, packages and symbols; fill its empty case.contract with exact assertions and correct its environment, commands and verifierAdapter for the files you generate.
Goal: token bucket rate limiting with Wait and Allow
Kind: HOW
Use EXACTLY these public packages and versions:
- pkg:golang/golang.org/x/time@v0.11.0
Demonstrate these symbols/APIs:
- golang.org/x/time/rate.Limiter.Allow
- golang.org/x/time/rate.Limiter.Wait
- golang.org/x/time/rate.NewLimiter
Constraints:
- executionContext: node
Required runtime conditions:
- ecosystem: npm
- language: javascript
- moduleSystem: cjs
- packageManager: npm@10.9.8
- runtime: node@22.23.2
Rules:
- One focused purpose; the smallest project that proves the goal.
- Include a contract test (test/contract.*) that runs OFFLINE and exits 0 exactly when the goal behavior works.
- Pin every dependency with a lockfile so resolution is reproducible.
- No secrets, credentials, or tokens. No real URLs (only example.com or localhost). No absolute paths.
- No personal names, emails, company names, or project identifiers of any kind.
- No binaries and no generated output (node_modules, dist, target, venv, .git, .env).
- Keep it under 200 files and 256KB packed.
{"case":{"caseId":"case:sha256:533797b833e7eaac1bfdf2a5dd8efa2e86fc5a084cd5f341714d9667a495cc72","contract":["limiter := rate.NewLimiter(rate.Limit(10), 1)","limiter.Allow() == true","limiter.Allow() == false","ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond)","limiter.Wait(ctx) == nil"],"goal":"token bucket rate limiting with Wait and Allow","kind":"HOW","packages":["pkg:golang/golang.org/x/time@v0.11.0"],"schemaVersion":1,"symbols":["golang.org/x/time/rate.Limiter.Allow","golang.org/x/time/rate.Limiter.Wait","golang.org/x/time/rate.NewLimiter"]},"contractCommand":["go","test","./..."],"environment":{"arch":"amd64","compiler":"go","compilerVersion":"1.26.6","distro":"ubuntu","ecosystem":"golang","language":"go","languageVersion":"1.26.6","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"go","packageManagerVersion":"1.26.6","schemaVersion":1},"license":"MIT-0","packages":["pkg:golang/golang.org/x/time@v0.11.0"],"schemaVersion":1,"subject":"pkg:golang/golang.org/x/time@v0.11.0","symbols":["golang.org/x/time/rate.Limiter.Allow","golang.org/x/time/rate.Limiter.Wait","golang.org/x/time/rate.NewLimiter"],"verifierAdapter":"golang@1"}
module example.com/ratelimit
go 1.23.0
require golang.org/x/time v0.11.0
golang.org/x/time v0.11.0 h1:/bpjEDfN9tkoN/ryeYHnv5hcMlc8ncjMcM4XBk5NWV0=
golang.org/x/time v0.11.0/go.mod h1:CDIdPxbZBQxdj6cxyCIdrNogrJKMJ7pr37NYpMcMDSg=
package ratelimit
import (
"context"
"golang.org/x/time/rate"
)
// TokenBucket wraps rate.Limiter demonstrating rate limiting with Wait and Allow.
type TokenBucket struct {
limiter *rate.Limiter
}
// NewTokenBucket creates a new TokenBucket limiter with events per second and burst size.
func NewTokenBucket(r rate.Limit, b int) *TokenBucket {
return &TokenBucket{
limiter: rate.NewLimiter(r, b),
}
}
// Allow reports whether an event may happen now.
func (tb *TokenBucket) Allow() bool {
return tb.limiter.Allow()
}
// Wait blocks until the limiter permits one event, or ctx is canceled.
func (tb *TokenBucket) Wait(ctx context.Context) error {
return tb.limiter.Wait(ctx)
}
// Limiter returns the underlying rate.Limiter.
func (tb *TokenBucket) Limiter() *rate.Limiter {
return tb.limiter
}
{
"schemaVersion": 1,
"goal": "token bucket rate limiting with Wait and Allow",
"kind": "HOW",
"packages": [
"pkg:golang/golang.org/x/time@v0.11.0"
],
"symbols": [
"golang.org/x/time/rate.Limiter.Allow",
"golang.org/x/time/rate.Limiter.Wait",
"golang.org/x/time/rate.NewLimiter"
],
"constraints": {
"executionContext": "node"
},
"runtimeConditions": {
"ecosystem": "npm",
"language": "javascript",
"moduleSystem": "cjs",
"packageManager": "npm@10.9.8",
"runtime": "node@22.23.2"
}
}
package test
import (
"context"
"testing"
"time"
"golang.org/x/time/rate"
)
func TestTokenBucketContract(t *testing.T) {
// 1. Create a limiter allowing 10 events per second with burst of 1
limiter := rate.NewLimiter(rate.Limit(10), 1)
if limiter == nil {
t.Fatal("expected non-nil limiter")
}
// 2. Initial token is available: Allow() must return true
if !limiter.Allow() {
t.Fatal("expected first Allow() to be true")
}
// 3. Immediate subsequent Allow() without burst capacity must return false
if limiter.Allow() {
t.Fatal("expected second immediate Allow() to be false")
}
// 4. Wait with a valid context: should succeed once token refills
ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond)
defer cancel()
if err := limiter.Wait(ctx); err != nil {
t.Fatalf("expected Wait() to succeed, got error: %v", err)
}
// 5. Wait with an already-canceled context should return context error
canceledCtx, cancelNow := context.WithCancel(context.Background())
cancelNow()
if err := limiter.Wait(canceledCtx); err == nil {
t.Fatal("expected Wait() on canceled context to fail")
}
}
Seeder d'origine
anonyme