CodeSampleX

Beispiel

golang.org/x/time v0.11.0: token bucket rate limiting with Wait and Allow

Verifiziertes Beispiel für golang golang.org/x/time v0.11.0: token bucket rate limiting with Wait and Allow. Der Vertrag lief auf go 1.26 · linux alpine/x64…

sha256:6731d057d62d179cd8b25bdd0ed5bf80f0ae0aabcaa55a5c61941d23ae85488a

Dieses Netzwerk bietet eine Sache: ein Sample, das baut. Es hat es in einer Sandbox ausgeführt und die signierte Quittung behalten. Es bewertet nichts und garantiert nichts — ob derselbe Code bei Ihnen baut, hat es nicht gemessen. Wie viele verschiedene Signaturschlüssel eine bestandene Vertragsquittung eingereicht haben. Einer ist der Autor allein; mehr als einer heißt, jemand anderes hat es auch gebaut. Ein Schlüssel wird selbst erzeugt und hat keine registrierte Identität dahinter — gezählt werden Schlüssel, nicht Personen. MIT-0

Ausführungsbelege

Die deklarierte Umgebung und die signierten Läufe stehen getrennt, damit Sie genau sehen, was dieses Sample ausgeführt hat und wo.

Beleggrundlage
Signierter Vertrag bestanden
Verifizierungsbelege
2
Signaturschlüssel, die es gebaut haben
1
Deklarierte Umgebung linux 24 · ubuntu · glibc 2.39 amd64 go 1.26 go 1

Umgebungen der Verifizierungsläufe

Umgebung Contract Stufen Lauf
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

Fall

HOW
Ziel
token bucket rate limiting with Wait and Allow
Pakete
Symbole
  • golang.org/x/time/rate.Limiter.Allow
  • golang.org/x/time/rate.Limiter.Wait
  • golang.org/x/time/rate.NewLimiter
Erstellt
2026-08-23T16:25:22Z

Contract

  1. limiter := rate.NewLimiter(rate.Limit(10), 1)
  2. limiter.Allow() == true
  3. limiter.Allow() == false
  4. ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond)
  5. limiter.Wait(ctx) == nil

Dateien

  • PROMPT.md
  • csx.json
  • go.mod
  • go.sum
  • ratelimit.go
  • spec.json
  • test/contract_test.go

Quellartefakt herunterladen (tar.gz)

Quelltext

PROMPT.md
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.
csx.json
{"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"}
go.mod
module example.com/ratelimit

go 1.23.0

require golang.org/x/time v0.11.0
go.sum
golang.org/x/time v0.11.0 h1:/bpjEDfN9tkoN/ryeYHnv5hcMlc8ncjMcM4XBk5NWV0=
golang.org/x/time v0.11.0/go.mod h1:CDIdPxbZBQxdj6cxyCIdrNogrJKMJ7pr37NYpMcMDSg=
ratelimit.go
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
}
spec.json
{
  "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"
  }
}
test/contract_test.go
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")
	}
}

Ursprungs-Seeder

anonym