ling-baseling-base

基础设施

限流 / 熔断 / 重试 / 缓存 / 分布式锁

在线 Playground

在浏览器中直接体验本页相关 API,无需本地安装 Go 环境。

基础设施

生产环境常用的弹性组件,均可独立 go get,按需组合。

模块

推荐组合

对外部 API(如 relay 调用上游 LLM):

retry(瞬时错误) + circuitbreaker(级联保护) + limiter(保护自身)

对热点数据:

cache/lru(L1) + cache/redis(L2) + lock/redis(防击穿)

relay 集成示例

import (
    "time"

    "github.com/LingByte/ling-base/common/circuitbreaker"
    "github.com/LingByte/ling-base/common/retry"
    "github.com/LingByte/ling-base/relay"
)

cb := circuitbreaker.New(circuitbreaker.Config{
    FailureThreshold: 0.5,
    MinRequests:      10,
    RecoveryTimeout:  30 * time.Second,
})

client := relay.New(
    relay.WithProvider(provider),
    relay.WithRetry(
        retry.WithMaxAttempts(3),
        retry.WithExponentialBackoff(100*time.Millisecond, 2*time.Second, 2.0, true),
    ),
    relay.WithCircuitBreaker(cb),
)

相关模块

common/bloom(布隆过滤器)、common/eventbus(进程内事件总线)等见 通用工具 完整列表。

On this page