示例
golang.org/x/sync v0.21.0: singleflight.Group
已验证示例 — golang golang.org/x/sync v0.21.0: singleflight.Group. contract 在 go 1.26 · linux debian/x64 · docker 上运行并通过: singleflight.Group.Do suppresses…
sha256:7c6a5cfdfba88ad3417c14306e9b18089a0d50df90520862271a864b92f632ae
本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。
提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。
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-19 |
案例
HOW- 目标
- verify golang.org/x/sync/singleflight.Group in pkg:golang/golang.org/x/sync@v0.21.0
- 符号
-
- golang.org/x/sync/singleflight.Group
- 创建时间
- 2026-09-17T19:10:07Z
契约
- singleflight.Group.Do suppresses duplicate concurrent calls for the same key and returns shared result
- singleflight.Group.Do returns function error and marks shared true for concurrent callers
- singleflight.Group.DoChan returns channel delivering Result struct with Val, Err and Shared flags
- singleflight.Group.Forget removes active key so subsequent calls execute function again
文件
- PROMPT.md
- csx.json
- go.mod
- go.sum
- sample.go
- spec.json
- test/contract.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 golang.org/x/sync/singleflight.Group in pkg:golang/golang.org/x/sync@v0.21.0
Kind: HOW
Use EXACTLY these public packages and versions:
- pkg:golang/golang.org/x/sync@v0.21.0
Demonstrate these symbols/APIs:
- golang.org/x/sync/singleflight.Group
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:23a096ba01bba7abc98f1285e5751a1140b27ea1dae3a32859682dd91d11144d","contract":["singleflight.Group.Do suppresses duplicate concurrent calls for the same key and returns shared result","singleflight.Group.Do returns function error and marks shared true for concurrent callers","singleflight.Group.DoChan returns channel delivering Result struct with Val, Err and Shared flags","singleflight.Group.Forget removes active key so subsequent calls execute function again"],"goal":"verify golang.org/x/sync/singleflight.Group in pkg:golang/golang.org/x/sync@v0.21.0","kind":"HOW","packages":["pkg:golang/golang.org/x/sync@v0.21.0"],"schemaVersion":1,"symbols":["golang.org/x/sync/singleflight.Group"]},"contractCommand":["go","run","./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/sync@v0.21.0"],"schemaVersion":1,"subject":"pkg:golang/golang.org/x/sync@v0.21.0","symbols":["golang.org/x/sync/singleflight.Group"],"verifierAdapter":"golang@1"}
module sample
go 1.25.0
require golang.org/x/sync v0.21.0
golang.org/x/sync v0.21.0 h1:HLII4xRRTtCRkxYp4HNFF0Js/Og6q2i++KXbg0gHCwM=
golang.org/x/sync v0.21.0/go.mod h1:9xrNwdLfx4jkKbNva9FpL6vEN7evnE43NNNJQ2LF3+0=
package sample
import (
"sync/atomic"
"golang.org/x/sync/singleflight"
)
// GroupCoordinator wraps singleflight.Group to coordinate duplicate execution suppression.
type GroupCoordinator struct {
group singleflight.Group
calls atomic.Int64
}
// NewGroupCoordinator returns a new GroupCoordinator instance.
func NewGroupCoordinator() *GroupCoordinator {
return &GroupCoordinator{}
}
// Group returns the underlying singleflight.Group instance.
func (c *GroupCoordinator) Group() *singleflight.Group {
return &c.group
}
// Executions returns the total count of executed underlying worker functions.
func (c *GroupCoordinator) Executions() int64 {
return c.calls.Load()
}
// Do wraps singleflight.Group.Do, tracking execution invocations.
func (c *GroupCoordinator) Do(key string, fn func() (any, error)) (any, error, bool) {
return c.group.Do(key, func() (any, error) {
c.calls.Add(1)
return fn()
})
}
// DoChan wraps singleflight.Group.DoChan, returning a channel of singleflight.Result.
func (c *GroupCoordinator) DoChan(key string, fn func() (any, error)) <-chan singleflight.Result {
return c.group.DoChan(key, func() (any, error) {
c.calls.Add(1)
return fn()
})
}
// Forget delegates to singleflight.Group.Forget to invalidate an in-flight key.
func (c *GroupCoordinator) Forget(key string) {
c.group.Forget(key)
}
{
"schemaVersion": 1,
"goal": "verify golang.org/x/sync/singleflight.Group in pkg:golang/golang.org/x/sync@v0.21.0",
"kind": "HOW",
"packages": [
"pkg:golang/golang.org/x/sync@v0.21.0"
],
"symbols": [
"golang.org/x/sync/singleflight.Group"
]
}
package main
import (
"errors"
"fmt"
"os"
"sync"
"time"
"sample"
)
func main() {
coordinator := sample.NewGroupCoordinator()
// Assertion 1: singleflight.Group.Do suppresses duplicate concurrent calls for the same key and returns shared result
{
const callers = 10
entered := make(chan struct{})
release := make(chan struct{})
results := make([]any, callers)
errs := make([]error, callers)
shareds := make([]bool, callers)
var wg sync.WaitGroup
for i := 0; i < callers; i++ {
wg.Add(1)
go func(idx int) {
defer wg.Done()
v, err, shared := coordinator.Do("shared-key", func() (any, error) {
close(entered)
<-release
return "computed-data", nil
})
results[idx] = v
errs[idx] = err
shareds[idx] = shared
}(i)
}
<-entered
time.Sleep(25 * time.Millisecond)
close(release)
wg.Wait()
for i := 0; i < callers; i++ {
if results[i] != "computed-data" {
fmt.Fprintf(os.Stderr, "FAIL: caller %d expected result 'computed-data', got %v\n", i, results[i])
os.Exit(1)
}
if errs[i] != nil {
fmt.Fprintf(os.Stderr, "FAIL: caller %d expected nil error, got %v\n", i, errs[i])
os.Exit(1)
}
}
sharedCount := 0
for _, s := range shareds {
if s {
sharedCount++
}
}
if sharedCount == 0 {
fmt.Fprintf(os.Stderr, "FAIL: expected duplicate callers to have shared=true, got 0\n")
os.Exit(1)
}
if coordinator.Executions() != 1 {
fmt.Fprintf(os.Stderr, "FAIL: expected 1 execution for shared key, got %d\n", coordinator.Executions())
os.Exit(1)
}
}
// Assertion 2: singleflight.Group.Do returns function error and marks shared true for concurrent callers
{
const errCallers = 5
enteredErr := make(chan struct{})
releaseErr := make(chan struct{})
expectedErr := errors.New("simulated upstream failure")
errs := make([]error, errCallers)
shareds := make([]bool, errCallers)
var wg sync.WaitGroup
for i := 0; i < errCallers; i++ {
wg.Add(1)
go func(idx int) {
defer wg.Done()
_, err, shared := coordinator.Do("error-key", func() (any, error) {
close(enteredErr)
<-releaseErr
return nil, expectedErr
})
errs[idx] = err
shareds[idx] = shared
}(i)
}
<-enteredErr
time.Sleep(25 * time.Millisecond)
close(releaseErr)
wg.Wait()
for i := 0; i < errCallers; i++ {
if !errors.Is(errs[i], expectedErr) {
fmt.Fprintf(os.Stderr, "FAIL: caller %d expected error %v, got %v\n", i, expectedErr, errs[i])
os.Exit(1)
}
}
if coordinator.Executions() != 2 {
fmt.Fprintf(os.Stderr, "FAIL: expected 2 total executions, got %d\n", coordinator.Executions())
os.Exit(1)
}
}
// Assertion 3: singleflight.Group.DoChan returns channel delivering Result struct with Val, Err and Shared flags
{
ch := coordinator.DoChan("chan-key", func() (any, error) {
return "async-data", nil
})
select {
case res := <-ch:
if res.Val != "async-data" {
fmt.Fprintf(os.Stderr, "FAIL: expected Result.Val 'async-data', got %v\n", res.Val)
os.Exit(1)
}
if res.Err != nil {
fmt.Fprintf(os.Stderr, "FAIL: expected Result.Err nil, got %v\n", res.Err)
os.Exit(1)
}
case <-time.After(2 * time.Second):
fmt.Fprintf(os.Stderr, "FAIL: timed out waiting on DoChan result channel\n")
os.Exit(1)
}
if coordinator.Executions() != 3 {
fmt.Fprintf(os.Stderr, "FAIL: expected 3 total executions, got %d\n", coordinator.Executions())
os.Exit(1)
}
}
// Assertion 4: singleflight.Group.Forget removes active key so subsequent calls execute function again
{
enteredForget := make(chan struct{})
releaseForget1 := make(chan struct{})
releaseForget2 := make(chan struct{})
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
_, _, _ = coordinator.Do("forget-key", func() (any, error) {
close(enteredForget)
<-releaseForget1
return "first", nil
})
}()
<-enteredForget
// In-flight call is blocked. Forget active key.
coordinator.Forget("forget-key")
secondStarted := make(chan struct{})
wg.Add(1)
go func() {
defer wg.Done()
_, _, _ = coordinator.Do("forget-key", func() (any, error) {
close(secondStarted)
<-releaseForget2
return "second", nil
})
}()
select {
case <-secondStarted:
// Second call started execution independently because the key was forgotten
case <-time.After(2 * time.Second):
fmt.Fprintf(os.Stderr, "FAIL: second call did not start independently after Forget\n")
os.Exit(1)
}
close(releaseForget1)
close(releaseForget2)
wg.Wait()
if coordinator.Executions() != 5 {
fmt.Fprintf(os.Stderr, "FAIL: expected 5 total executions, got %d\n", coordinator.Executions())
os.Exit(1)
}
}
fmt.Println("PASS: all singleflight.Group contract assertions passed")
}
原始种子者
匿名