샘플
golang.org/x/time v0.15.0: rate.Limiter
검증된 샘플 — golang golang.org/x/time v0.15.0: rate.Limiter. go 1.26 · linux debian/x64 · docker에서 contract를 실행해 통과했습니다: rate.NewLimiter initializes a token…
sha256:1d179cb929b228f5c7db040df324284f7af3bda7e35ec5800551e4b5379f2638
이 네트워크가 제공하는 것은 하나입니다. 빌드되는 샘플. 샌드박스에서 돌리고 서명된 영수증을 보관합니다. 등급을 매기지 않고 무엇도 보증하지 않습니다 — 같은 코드가 당신 환경에서 빌드되는지는 측정한 적이 없습니다.
통과한 계약 영수증을 낸 서로 다른 서명 키의 수입니다. 하나면 작성자 혼자이고, 둘 이상이면 다른 사람도 빌드했다는 뜻입니다. 키는 스스로 만드는 것이고 뒤에 등록된 신원이 없으므로, 세는 것은 사람이 아니라 키입니다.
MIT-0
실행 증거
선언된 환경과 서명된 실행을 분리해 두었습니다. 이 샘플이 무엇을 어디서 실행했는지 그대로 볼 수 있습니다.
- 증거 기준
- 서명된 컨트랙트 통과
- 검증 영수증
- 1
- 빌드한 서명 키
- 1
선언된 환경
linux 24 · ubuntu · glibc 2.39 x64 go
검증 실행 환경
| 환경 | 컨트랙트 | 단계 | 실행일 |
|---|---|---|---|
| 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-01 |
케이스
HOW- 목표
- verify rate.Limiter in pkg:golang/golang.org/x/time@v0.15.0
- 심벌
-
- rate.Limiter
- 생성일
- 2026-09-01T10:41:08Z
컨트랙트
- rate.NewLimiter initializes a token bucket limiter with Limit and burst size, accessible via Limit() and Burst().
- rate.Limiter.Allow and AllowN consume available tokens immediately, returning true when tokens are available and false when burst capacity is exhausted.
- rate.Limiter.Reserve and ReserveN return a Reservation with OK() and positive Delay when tokens are temporarily unavailable within burst limits, or OK() false with InfDuration delay when requested tokens exceed burst.
- rate.Limiter.Wait and WaitN block until tokens are available, returning nil on success, immediate error if requested tokens exceed burst, or context error when context is cancelled.
- Dynamic configuration updates via SetLimit and SetBurst take effect immediately, and Tokens() reports available tokens.
파일
- PROMPT.md
- csx.json
- go.mod
- go.sum
- ratelimit.go
- spec.json
- test/contract_test.go
소스
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: verify rate.Limiter in pkg:golang/golang.org/x/time@v0.15.0
Kind: HOW
Use EXACTLY these public packages and versions:
- pkg:golang/golang.org/x/time@v0.15.0
Demonstrate these symbols/APIs:
- rate.Limiter
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:222ed3114cf28f718fd92feb8cc51388ae4edfcdf54a132d3900738e8f63e2ae","contract":["rate.NewLimiter initializes a token bucket limiter with Limit and burst size, accessible via Limit() and Burst().","rate.Limiter.Allow and AllowN consume available tokens immediately, returning true when tokens are available and false when burst capacity is exhausted.","rate.Limiter.Reserve and ReserveN return a Reservation with OK() and positive Delay when tokens are temporarily unavailable within burst limits, or OK() false with InfDuration delay when requested tokens exceed burst.","rate.Limiter.Wait and WaitN block until tokens are available, returning nil on success, immediate error if requested tokens exceed burst, or context error when context is cancelled.","Dynamic configuration updates via SetLimit and SetBurst take effect immediately, and Tokens() reports available tokens."],"goal":"verify rate.Limiter in pkg:golang/golang.org/x/time@v0.15.0","kind":"HOW","packages":["pkg:golang/golang.org/x/time@v0.15.0"],"schemaVersion":1,"symbols":["rate.Limiter"]},"contractCommand":["go","test","./..."],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"golang","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"go","schemaVersion":1},"license":"MIT-0","packages":["pkg:golang/golang.org/x/time@v0.15.0"],"schemaVersion":1,"subject":"pkg:golang/golang.org/x/time@v0.15.0","symbols":["rate.Limiter"],"verifierAdapter":"golang@1"}
module example.com/ratelimit
go 1.25.0
require golang.org/x/time v0.15.0
golang.org/x/time v0.15.0 h1:bbrp8t3bGUeFOx08pvsMYRTCVSMk89u4tKbNOZbp88U=
golang.org/x/time v0.15.0/go.mod h1:Y4YMaQmXwGQZoFaVFk4YpCt4FLQMYKZe9oeV/f4MSno=
package ratelimit
import (
"context"
"time"
"golang.org/x/time/rate"
)
// Limiter manages rate limiting operations wrapping rate.Limiter.
type Limiter struct {
limiter *rate.Limiter
}
// New creates a new Limiter with a rate limit and burst size.
func New(r rate.Limit, b int) *Limiter {
return &Limiter{
limiter: rate.NewLimiter(r, b),
}
}
// Allow reports whether an event may happen now.
func (l *Limiter) Allow() bool {
return l.limiter.Allow()
}
// AllowN reports whether n events may happen now.
func (l *Limiter) AllowN(now time.Time, n int) bool {
return l.limiter.AllowN(now, n)
}
// Wait blocks until the limiter permits one event, or ctx is canceled.
func (l *Limiter) Wait(ctx context.Context) error {
return l.limiter.Wait(ctx)
}
// WaitN blocks until the limiter permits n events, or ctx is canceled.
func (l *Limiter) WaitN(ctx context.Context, n int) error {
return l.limiter.WaitN(ctx, n)
}
// Reserve returns a Reservation for 1 event.
func (l *Limiter) Reserve() *rate.Reservation {
return l.limiter.Reserve()
}
// ReserveN returns a Reservation for n events.
func (l *Limiter) ReserveN(now time.Time, n int) *rate.Reservation {
return l.limiter.ReserveN(now, n)
}
// Limit returns the current maximum rate.
func (l *Limiter) Limit() rate.Limit {
return l.limiter.Limit()
}
// Burst returns the current maximum burst size.
func (l *Limiter) Burst() int {
return l.limiter.Burst()
}
// SetLimit sets a new Limit.
func (l *Limiter) SetLimit(newLimit rate.Limit) {
l.limiter.SetLimit(newLimit)
}
// SetBurst sets a new burst size.
func (l *Limiter) SetBurst(newBurst int) {
l.limiter.SetBurst(newBurst)
}
// Tokens returns the number of tokens currently in the bucket.
func (l *Limiter) Tokens() float64 {
return l.limiter.Tokens()
}
{
"schemaVersion": 1,
"goal": "verify rate.Limiter in pkg:golang/golang.org/x/time@v0.15.0",
"kind": "HOW",
"packages": [
"pkg:golang/golang.org/x/time@v0.15.0"
],
"symbols": [
"rate.Limiter"
]
}
package test
import (
"context"
"testing"
"time"
"golang.org/x/time/rate"
"example.com/ratelimit"
)
func TestLimiterInitialization(t *testing.T) {
// 1. rate.NewLimiter initializes a token bucket limiter with Limit and burst size
limit := rate.Limit(10)
burst := 5
lim := ratelimit.New(limit, burst)
if got := lim.Limit(); got != limit {
t.Fatalf("expected Limit() %v, got %v", limit, got)
}
if got := lim.Burst(); got != burst {
t.Fatalf("expected Burst() %d, got %d", burst, got)
}
}
func TestLimiterAllow(t *testing.T) {
// 2. rate.Limiter.Allow and AllowN consume available tokens immediately
burst := 3
lim := ratelimit.New(rate.Limit(1), burst)
for i := 0; i < burst; i++ {
if !lim.Allow() {
t.Fatalf("expected Allow() call %d to succeed", i+1)
}
}
if lim.Allow() {
t.Fatal("expected Allow() to fail when burst is exhausted")
}
if lim.AllowN(time.Now(), burst+1) {
t.Fatal("expected AllowN with n > burst to return false")
}
}
func TestLimiterReserve(t *testing.T) {
// 3. rate.Limiter.Reserve and ReserveN return Reservation with OK and Delay
now := time.Now()
lim := ratelimit.New(rate.Limit(10), 2)
if !lim.AllowN(now, 2) {
t.Fatal("expected AllowN to consume initial tokens")
}
res := lim.ReserveN(now, 1)
if !res.OK() {
t.Fatal("expected ReserveN to be OK within burst capacity")
}
if d := res.DelayFrom(now); d <= 0 {
t.Fatalf("expected positive delay for empty bucket, got %v", d)
}
res.CancelAt(now)
oversized := lim.ReserveN(now, 10)
if oversized.OK() {
t.Fatal("expected ReserveN with n > burst to return OK() == false")
}
if oversized.Delay() != rate.InfDuration {
t.Fatalf("expected InfDuration delay for oversized reservation, got %v", oversized.Delay())
}
}
func TestLimiterWait(t *testing.T) {
// 4. rate.Limiter.Wait and WaitN block until tokens are available or context is cancelled
lim := ratelimit.New(rate.Limit(10), 2)
ctx := context.Background()
if err := lim.Wait(ctx); err != nil {
t.Fatalf("expected Wait() to succeed, got %v", err)
}
if err := lim.WaitN(ctx, 10); err == nil {
t.Fatal("expected WaitN with n > burst to fail immediately")
}
cancelCtx, cancel := context.WithCancel(ctx)
cancel()
if err := lim.Wait(cancelCtx); err == nil {
t.Fatal("expected Wait() on cancelled context to fail")
}
}
func TestLimiterReconfiguration(t *testing.T) {
// 5. Dynamic configuration updates via SetLimit and SetBurst take effect immediately
lim := ratelimit.New(rate.Limit(5), 2)
lim.SetLimit(rate.Limit(20))
if got := lim.Limit(); got != rate.Limit(20) {
t.Fatalf("expected Limit() 20, got %v", got)
}
lim.SetBurst(8)
if got := lim.Burst(); got != 8 {
t.Fatalf("expected Burst() 8, got %d", got)
}
tokens := lim.Tokens()
if tokens < 0 || tokens > 8 {
t.Fatalf("expected tokens between 0 and 8, got %v", tokens)
}
}
오리진 시더
익명