hedge

package
v0.3.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 8, 2026 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package hedge 实现对冲请求(hedged / backup requests):一个操作发出后,若在 delay 内还没返回, 就并发补发一个副本(最多 maxHedge 个额外副本),取最先成功返回者,随后取消其余。 出自 Google《The Tail at Scale》——用少量额外请求换取尾延迟(P99/P999)的大幅下降, 适合读多、可重试、幂等的下游调用(RPC、存储、缓存回源)。

边界(机制而非策略):delay/maxHedge、哪些操作值得对冲、是否幂等都由调用方定;本包只负责 "定时补发 + 取先成功 + 取消其余"。非幂等写操作请勿使用。纯标准库。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Do

func Do[T any](ctx context.Context, delay time.Duration, maxHedge int, fn func(ctx context.Context, attempt int) (T, error)) (T, error)

Do 执行 fn 并按需对冲。primary(attempt=0)立即发出;此后每过 delay,若仍无成功结果且尚未达到 上限,就再补发一个副本(attempt 递增),最多 maxHedge 个额外副本(总计 maxHedge+1 个)。 返回最先成功(err==nil)的结果;若全部失败,返回最后一个错误。所有副本共享一个可取消的 ctx, 有结果或 Do 返回时其余副本会被取消——fn 应在收到 ctx.Done 时尽快返回。

delay<=0 表示不等待、一次性并发发出全部副本(纯请求竞速)。maxHedge<=0 表示不对冲(只发一次)。

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL