channel

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Oct 10, 2026 License: AGPL-3.0 Imports: 14 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package channel 提供上游渠道的建模与 HTTP 客户端:基址拼接、鉴权样式、超时、 重试与 SSE 读写。

渠道只负责「怎么把一段字节送到上游、怎么把上游的字节读回来」,不解析协议内容: 方言之间的转换在 relay 包。

Index

Constants

View Source
const (
	// StyleBearer 用 Authorization: Bearer <key>。
	StyleBearer = "bearer"
	// StyleAPIKey 用 x-api-key: <key>。
	StyleAPIKey = "x-api-key"
	// StyleQuery 把 key 放在查询参数上。
	StyleQuery = "query"
	// StyleCustom 用指定名字的请求头。
	StyleCustom = "custom"
	// StyleNone 不鉴权,用于指向本地假上游或已在内网放行的上游。
	StyleNone = "none"
)

鉴权样式的合法取值。

View Source
const DefaultMaxEventBytes = 8 << 20

DefaultMaxEventBytes 是单条 SSE 事件负载的默认上限。上游返回超长事件时按错误 处理,避免无上限地吃内存。

View Source
const DefaultTimeout = 120 * time.Second

DefaultTimeout 是未指定超时时的取值。

View Source
const MaxRetries = 10

MaxRetries 是重试次数的上限。转发是同步路径,重试次数乘以超时会直接变成 客户端等待时长,因此这里给一个上界而不是放任配置。

Variables

View Source
var ErrEventTooLarge = errors.New("channel: 事件负载超过上限")

ErrEventTooLarge 表示单条事件超过上限。

View Source
var ErrMissingCredential = errors.New("channel: 凭据环境变量未设置")

ErrMissingCredential 表示凭据环境变量未设置。

Functions

func CopyRaw

func CopyRaw(dst io.Writer, src io.Reader, flush func()) (int64, error)

CopyRaw 把上游流逐段搬给下游,不做解析,保留原始字节。返回搬运的字节数。

flushEvery 为每次刷新前累计的字节阈值,0 表示每段都刷新。同方言转发走这条路, 未建模字段因此逐字节保留。

Types

type Auth

type Auth struct {
	// Style 取 bearer、x-api-key、query、custom 或 none。
	Style string
	// KeyEnv 是凭据所在的环境变量名。凭据只在创建客户端时从进程环境读一次。
	KeyEnv string
	// Header 是 Style 为 custom 时的请求头名。
	Header string
	// Query 是 Style 为 query 时的查询参数名。
	Query string
}

Auth 是渠道的鉴权方式。

func (Auth) Validate

func (a Auth) Validate() error

Validate 校验鉴权描述。

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client 是一个渠道的 HTTP 客户端。可以并发使用。

func NewClient

func NewClient(spec Spec) (*Client, error)

NewClient 创建渠道客户端。凭据在此时从进程环境读一次,读不到直接报错。

func (*Client) Dialect

func (c *Client) Dialect() dialect.Name

Dialect 返回上游方言。

func (*Client) Name

func (c *Client) Name() string

Name 返回渠道名。

func (*Client) Send

func (c *Client) Send(ctx context.Context, req Request) (*http.Response, error)

Send 发送请求并返回响应。响应体由调用方关闭。

重试只覆盖「请求没成功建立」与「上游返回可重试状态」两类情况:连接错误、 408、429、5xx。非流式请求会带上整体超时,流式请求不加,靠 ResponseHeaderTimeout 与调用方的 context 控制。

func (*Client) Spec

func (c *Client) Spec() Spec

Spec 返回渠道描述。

type Event

type Event struct {
	// Name 是 event 字段,缺省为空。
	Name string
	// Data 是 data 字段拼接后的内容,末尾换行已去掉。
	Data []byte
}

Event 是一条 SSE 事件。

type Reader

type Reader struct {
	// contains filtered or unexported fields
}

Reader 逐个读出 SSE 事件。

func NewReader

func NewReader(reader io.Reader) *Reader

NewReader 创建事件阅读器。

func (*Reader) Next

func (r *Reader) Next() (Event, error)

Next 返回下一条事件。流结束时返回 io.EOF。

解析遵循 SSE 的常用子集:忽略注释行与未知字段;data 行按行拼接,行间补 \n; 空行分发事件;`data: [DONE]` 之类的结束标记原样返回,由调用方判断。

func (*Reader) SetMaxEventBytes

func (r *Reader) SetMaxEventBytes(limit int)

SetMaxEventBytes 调整单条事件上限,非正数表示用默认值。

type Request

type Request struct {
	// Dialect 覆盖渠道的默认方言,用于同一条渠道上按路由切换协议。
	// 为空时用渠道自己的方言。覆盖生效时渠道的 Path 不参与拼接,
	// 因为那个值是按渠道方言写的。
	Dialect dialect.Name
	// Method 是 HTTP 方法,为空时用 POST。
	Method string
	// Model 是需要写进 URL 路径的模型名,只有 Gemini 用得到。
	Model string
	// Stream 决定取流式端点还是非流式端点,只有 Gemini 用得到。
	Stream bool
	// Body 是请求体,会被完整缓存在内存里,因此可以重试。
	Body []byte
	// Header 是附加请求头。
	Header http.Header
}

Request 是一次上游调用。

type Set

type Set struct {
	// contains filtered or unexported fields
}

Set 是按名字索引的渠道客户端集合。

func NewSet

func NewSet(specs []Spec) (*Set, error)

NewSet 按顺序创建渠道客户端集合。任一渠道不合法或凭据缺失都会失败。

func (*Set) Get

func (s *Set) Get(name string) (*Client, bool)

Get 按名字取客户端。

func (*Set) Len

func (s *Set) Len() int

Len 返回渠道数量。

func (*Set) Names

func (s *Set) Names() []string

Names 按创建顺序返回渠道名。

type Spec

type Spec struct {
	Name string
	// Dialect 是上游说话用的方言。
	Dialect dialect.Name
	// BaseURL 是渠道根地址,例如 https://api.openai.com。
	BaseURL string
	// Path 覆盖方言的默认上游路径。BaseURL 已带路径时(例如
	// https://openrouter.ai/api/v1)必须显式给出,取值从该路径之后算起。
	// Gemini 的路径含模型名,不允许覆盖。
	Path string
	// Auth 是鉴权方式。
	Auth Auth
	// Timeout 是单次请求的整体超时。为 0 时用 DefaultTimeout。
	Timeout time.Duration
	// MaxRetries 是失败后的重试次数,0 表示不重试。
	MaxRetries int
	// Headers 是附加到每个请求上的头。
	Headers map[string]string
}

Spec 是一个渠道的完整描述。

func (Spec) Validate

func (s Spec) Validate() error

Validate 校验渠道描述。

type Writer

type Writer struct {
	// contains filtered or unexported fields
}

Writer 按 SSE 格式写出事件,并在每次写出后刷新。

func NewWriter

func NewWriter(writer io.Writer) *Writer

NewWriter 创建事件写出器。底层实现 http.Flusher 时会自动刷新。

func (*Writer) Flush

func (w *Writer) Flush() error

Flush 把缓冲写到底层并调用其 Flush。

func (*Writer) Write

func (w *Writer) Write(event Event) error

Write 写出一条事件并刷新。

func (*Writer) WriteRaw

func (w *Writer) WriteRaw(data []byte) error

WriteRaw 原样写出一行 data 字段(不拆行),用于 [DONE] 之类的标记。

Jump to

Keyboard shortcuts

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