Documentation
¶
Index ¶
- Constants
- Variables
- func BuildGRPCMethods(rawServices []*grpc.ServiceDesc) []string
- type Agent
- func (a *Agent) ConfigureRun(config SidecarAgentConfig)
- func (a *Agent) Deregister(ctx context.Context) error
- func (a *Agent) Drain(ctx context.Context) error
- func (a *Agent) Run(ctx context.Context) error
- func (a *Agent) Shutdown(ctx context.Context) error
- func (a *Agent) Start(ctx context.Context) <-chan error
- func (a *Agent) Status() Status
- type AgentRunError
- type ApiClient
- type Client
- type ConnectionEvent
- type Controller
- func (c *Controller) Deregister(ctx context.Context) error
- func (c *Controller) Drain(ctx context.Context, gracePeriod string) error
- func (c *Controller) ObserveEvent(event ConnectionEvent)
- func (c *Controller) OnConnected(ctx context.Context) error
- func (c *Controller) OnDisconnected()
- func (c *Controller) RecordError(err error)
- func (c *Controller) Status() Status
- type DeregisterRequest
- type DrainRequest
- type ErrorHandler
- type EventSource
- type HttpClient
- type LifecycleRunError
- type RegisterReplayError
- type Runner
- type ServeFunc
- type ServiceNode
- type ServiceOptions
- type ShutdownFunc
- type SidecarAgentConfig
- type Status
- type WatchEventParseError
- type WatchHTTPStatusError
- type WatchSource
Constants ¶
const ( // DefaultAdminBaseURL 表示业务服务默认访问的本机 sidecar-agent 管理地址。 DefaultAdminBaseURL = "http://127.0.0.1:15010" // DefaultWatchPath 表示 sidecar-agent 提供的默认 watch 路径。 DefaultWatchPath = "/watch" // DefaultRequestTimeout 表示 register、drain、deregister 的默认请求超时。 DefaultRequestTimeout = 3 * time.Second // DefaultReconnectInterval 表示 watch 断开后的默认重连间隔。 DefaultReconnectInterval = time.Second )
const ( // AgentRunStageLifecycle 表示 sidecar watch/replay 生命周期阶段。 AgentRunStageLifecycle = "lifecycle" // AgentRunStageServe 表示业务服务阻塞运行阶段。 AgentRunStageServe = "serve" // AgentRunStageShutdown 表示业务服务优雅关闭阶段。 AgentRunStageShutdown = "shutdown" // AgentRunStageAgentShutdown 表示 agent 摘流与注销阶段。 AgentRunStageAgentShutdown = "agent_shutdown" )
const ( // ConnectionEventTypeConnected 表示与本机 sidecar-agent 的 watch 流已建立。 ConnectionEventTypeConnected = "connected" // ConnectionEventTypeHeartbeat 表示 watch 流仍然存活,但不要求业务侧执行 register 重放。 ConnectionEventTypeHeartbeat = "heartbeat" // ConnectionEventTypeDisconnected 表示 watch 流已断开,等待后续重连。 ConnectionEventTypeDisconnected = "disconnected" )
Variables ¶
var ( // ErrWatchURLRequired 表示业务侧未提供本地 `/watch` 地址。 ErrWatchURLRequired = errors.New("watch url is required") // ErrWatchStreamClosed 表示当前 watch 流已由服务端关闭,调用方应进入下一轮重连。 ErrWatchStreamClosed = errors.New("watch stream closed") )
Functions ¶
func BuildGRPCMethods ¶ added in v0.1.0
func BuildGRPCMethods(rawServices []*grpc.ServiceDesc) []string
BuildGRPCMethods 把 gRPC ServiceDesc 集合转换成稳定有序的完整 method path 列表。
Types ¶
type Agent ¶ added in v0.1.0
type Agent struct {
// Node 保存当前业务服务构造出的固定 ServiceNode。
Node *ServiceNode
// Client 保存和 sidecar 管理接口交互的 API client。
Client *ApiClient
// Source 保存 sidecar watch 事件源。
Source *WatchSource
// Controller 保存基于固定节点的控制器。
Controller *Controller
// Runner 保存事件驱动运行器。
Runner *Runner
// contains filtered or unexported fields
}
Agent 是 go-consul/agent 对外唯一主入口,负责组装 watch/replay、注册摘流注销与可选的业务服务托管逻辑。
func New ¶ added in v0.1.0
func New(serviceOptions *ServiceOptions, config SidecarAgentConfig) (*Agent, error)
New 使用 ServiceOptions 与 SidecarAgentConfig 组装一个可直接运行的 agent 主对象。
func (*Agent) ConfigureRun ¶ added in v0.1.0
func (a *Agent) ConfigureRun(config SidecarAgentConfig)
ConfigureRun 允许在创建 Agent 后补充业务 serve/shutdown 与运行期回调配置。
func (*Agent) Deregister ¶ added in v0.1.0
Deregister 对当前服务发起注销。
type AgentRunError ¶ added in v0.1.0
AgentRunError 表示单入口 Agent 在某个阶段失败。
func (*AgentRunError) Error ¶ added in v0.1.0
func (e *AgentRunError) Error() string
Error 返回带阶段信息的错误文本。
func (*AgentRunError) Unwrap ¶ added in v0.1.0
func (e *AgentRunError) Unwrap() error
Unwrap 返回底层错误。
type ApiClient ¶ added in v0.1.0
type ApiClient struct {
// contains filtered or unexported fields
}
ApiClient 提供一个基于 JSON over HTTP 的本地 agent client 实现。
func NewApiClient ¶ added in v0.1.0
func NewApiClient(client *HttpClient) *ApiClient
NewApiClient 创建一个新的本地 HTTP JSON client。
func (*ApiClient) Deregister ¶ added in v0.1.0
func (c *ApiClient) Deregister(ctx context.Context, request DeregisterRequest) error
Deregister 通过本机管理接口发起注销。
type Client ¶
type Client interface {
// Register 负责把当前服务注册到本机 sidecar-agent。
Register(ctx context.Context, request *ServiceNode) error
// Drain 负责把当前服务切换到摘流状态。
Drain(ctx context.Context, request DrainRequest) error
// Deregister 负责把当前服务从本机 sidecar-agent 注销。
Deregister(ctx context.Context, request DeregisterRequest) error
}
Client 抽象本机 sidecar-agent 的最小交互能力。
type ConnectionEvent ¶
type ConnectionEvent struct {
// Type 表示事件类型,例如 connected、heartbeat、disconnected。
Type string
// Connected 表示当前事件是否意味着连接已建立。
Connected bool
// EventId 表示服务端 SSE 事件 ID,便于后续排障与协议增强。
EventId string
// Message 保存服务端事件说明文本。
Message string
// Service 表示当前发出 watch 事件的服务名。
Service string
// Status 表示 sidecar 当前运行态摘要,例如 ready、starting、degraded。
Status string
// LifecycleState 表示 sidecar 当前生命周期阶段。
LifecycleState string
// Ready 表示 sidecar 主链当前是否 ready;nil 表示服务端未提供该信息。
Ready *bool
// GeneratedAt 表示该事件在服务端生成的时间。
GeneratedAt *time.Time
// Err 保存连接断开或处理失败时的上下文错误。
Err error
}
ConnectionEvent 描述本机 agent 连接状态变化事件。
type Controller ¶
type Controller struct {
// contains filtered or unexported fields
}
Controller 负责在业务服务侧持有固定 ServiceNode,并在连接恢复后重放 register。
func NewController ¶
func NewController(client Client, node *ServiceNode) (*Controller, error)
NewController 创建一个新的业务侧 agent 联动控制器。
func (*Controller) Deregister ¶
func (c *Controller) Deregister(ctx context.Context) error
Deregister 使用固定 ServiceNode 发起注销。
func (*Controller) Drain ¶
func (c *Controller) Drain(ctx context.Context, gracePeriod string) error
Drain 使用固定 ServiceNode 发起摘流。
func (*Controller) ObserveEvent ¶ added in v0.0.8
func (c *Controller) ObserveEvent(event ConnectionEvent)
ObserveEvent 记录最近一次收到的 watch 事件,供业务侧调试恢复链路使用。
func (*Controller) OnConnected ¶
func (c *Controller) OnConnected(ctx context.Context) error
OnConnected 在本机 agent 连接建立或恢复时重放 register。
func (*Controller) OnDisconnected ¶
func (c *Controller) OnDisconnected()
OnDisconnected 在本机 agent 连接断开时把控制器状态标记为未连接。
func (*Controller) RecordError ¶ added in v0.0.8
func (c *Controller) RecordError(err error)
RecordError 把最近一次运行错误写入状态快照。
type DeregisterRequest ¶
type DeregisterRequest struct {
// AppId 表示应用标识。
AppId string `json:"app_id"`
// AppInstanceId 表示应用实例标识。
AppInstanceId string `json:"app_instance_id"`
// ServiceName 表示逻辑服务名。
ServiceName string `json:"service_name"`
// ServicePort 表示服务端口。
ServicePort uint `json:"service_port"`
}
DeregisterRequest 表示业务服务向本机 sidecar-agent 发起的注销请求。
type DrainRequest ¶
type DrainRequest struct {
// AppId 表示应用标识。
AppId string `json:"app_id"`
// AppInstanceId 表示应用实例标识。
AppInstanceId string `json:"app_instance_id"`
// ServiceName 表示逻辑服务名。
ServiceName string `json:"service_name"`
// GracePeriod 表示摘流宽限期。
GracePeriod string `json:"grace_period"`
}
DrainRequest 表示业务服务向本机 sidecar-agent 发起的摘流请求。
type EventSource ¶
type EventSource interface {
// Subscribe 返回一个持续输出连接事件的只读通道。
Subscribe(ctx context.Context) (<-chan ConnectionEvent, error)
}
EventSource 抽象本地 agent 连接事件来源。
type HttpClient ¶ added in v0.1.0
type HttpClient struct {
// contains filtered or unexported fields
}
HttpClient 提供一个基于 JSON over HTTP 的本地 agent client 实现。
func NewHttpClient ¶ added in v0.1.0
func NewHttpClient(baseURL string, timeout time.Duration) *HttpClient
NewHttpClient 创建一个新的本地 HTTP JSON client。
type LifecycleRunError ¶ added in v0.0.8
type LifecycleRunError struct {
// Err 表示底层运行失败原因。
Err error
}
LifecycleRunError 表示本地 agent watch/replay 后台循环运行失败。
func (*LifecycleRunError) Error ¶ added in v0.0.8
func (e *LifecycleRunError) Error() string
Error 返回可读错误信息。
func (*LifecycleRunError) Unwrap ¶ added in v0.0.8
func (e *LifecycleRunError) Unwrap() error
Unwrap 返回底层错误。
type RegisterReplayError ¶ added in v0.0.8
type RegisterReplayError struct {
// ServiceName 表示当前重放失败的服务名。
ServiceName string
// ServicePort 表示当前重放失败的服务端口。
ServicePort int
// Err 表示底层 register 调用失败原因。
Err error
}
RegisterReplayError 表示业务侧在连接恢复后重放 register 时失败。
func (*RegisterReplayError) Error ¶ added in v0.0.8
func (e *RegisterReplayError) Error() string
Error 返回带服务上下文的可读错误信息。
func (*RegisterReplayError) Unwrap ¶ added in v0.0.8
func (e *RegisterReplayError) Unwrap() error
Unwrap 返回底层错误,便于调用方做 errors.Is / errors.As。
type Runner ¶
type Runner struct {
// contains filtered or unexported fields
}
Runner 负责把连接事件转换成 register 重放动作。
func NewRunner ¶
func NewRunner(source EventSource, controller *Controller, onError ErrorHandler) (*Runner, error)
NewRunner 创建一个新的连接事件驱动运行器。
type ServiceNode ¶
type ServiceNode struct {
*ServiceOptions
DNS string `json:"dns"`
RunDate string `json:"run_date"`
// ProtoCount 表示业务服务子服务数量。
ProtoCount uint `json:"proto_count"`
// Methods 表示业务服务暴露的方法列表。
Methods []string `json:"methods"`
}
ServiceNode 描述业务服务在裸机场景下的最小注册节点信息。
func NewServiceNode ¶ added in v0.1.0
func NewServiceNode(options *ServiceOptions, serviceRaw []*grpc.ServiceDesc) *ServiceNode
NewServiceNode 基于 ServiceOptions 和 gRPC ServiceDesc 构造固定的业务服务节点描述。
func (*ServiceNode) BuildDeregisterRequest ¶ added in v0.1.0
func (n *ServiceNode) BuildDeregisterRequest() DeregisterRequest
BuildDeregisterRequest 从约定好的 ServiceNode 直接导出 sidecar-agent 注销请求。
func (*ServiceNode) BuildDrainRequest ¶ added in v0.1.0
func (n *ServiceNode) BuildDrainRequest(gracePeriod string) DrainRequest
BuildDrainRequest 从约定好的 ServiceNode 直接导出 sidecar-agent 摘流请求。
type ServiceOptions ¶
type ServiceOptions struct {
// App 应用配置
App app.Config `json:"app"`
// Kernel 内核配置
Kernel kernel.Config `json:"kernel"`
// Service 服务配置
Service service.Config `json:"service"`
// Protocol 表示业务协议, grpc / http。
Protocol string `json:"protocol"`
// ServerPort 服务端口
ServerPort uint `json:"server_port"`
// ManagedPort 管理端口
ManagedPort uint `json:"managed_port"`
}
ServiceOptions 描述业务服务自身的原始输入配置。
func (*ServiceOptions) BuildDNS ¶ added in v0.1.0
func (o *ServiceOptions) BuildDNS() string
BuildDNS 基于当前服务配置拼接 sidecar 注册时使用的统一 DNS 地址。
type SidecarAgentConfig ¶ added in v0.1.0
type SidecarAgentConfig struct {
// BaseURL 表示 sidecar-agent 基础 URL。
BaseURL string `json:"base_url"`
// WatchURL 表示 sidecar-agent 的 SSE watch 地址;为空时由 BaseURL 自动推导。
WatchURL string `json:"watch_url"`
// GracePeriod 表示业务服务优雅下线时使用的默认摘流宽限期。
GracePeriod string `json:"grace_period"`
// RequestTimeout 表示 register、drain、deregister 的请求超时。
RequestTimeout time.Duration `json:"request_timeout"`
// ReconnectInterval 表示 watch 断开后的重连间隔。
ReconnectInterval time.Duration `json:"reconnect_interval"`
// RawServices 表示当前进程注册的 gRPC ServiceDesc 集合,用于构造 methods。
RawServices []*grpc.ServiceDesc `json:"-"`
// OnError 用于统一处理 watch 与 register 重放过程中的异步错误。
OnError ErrorHandler `json:"-"`
// Serve 表示业务服务真正的阻塞运行入口;为空时仅运行 agent watch/replay 主链。
Serve ServeFunc `json:"-"`
// Shutdown 表示业务服务优雅关闭入口;可为空。
Shutdown ShutdownFunc `json:"-"`
}
SidecarAgentConfig 描述业务服务接入本机 sidecar-agent 时需要的运行配置。
func DefaultSidecarAgentConfig ¶ added in v0.1.0
func DefaultSidecarAgentConfig(baseURL string) SidecarAgentConfig
DefaultSidecarAgentConfig 返回一份可直接接入业务服务的默认 sidecar-agent 配置。
type Status ¶
type Status struct {
// Connected 表示当前是否与本机 sidecar-agent 保持连接。
Connected bool
// Registered 表示最近一次 register 是否成功完成。
Registered bool
// LastServiceName 表示最近一次成功注册的服务名。
LastServiceName string
// LastServicePort 表示最近一次成功注册的服务端口。
LastServicePort int
// LastEventType 表示最近一次收到的 watch 事件类型。
LastEventType string
// LastEventId 表示最近一次收到的 SSE 事件 ID。
LastEventId string
// LastEventAt 表示最近一次事件的观测时间。
LastEventAt string
// LastConnectedAt 表示最近一次成功建立或恢复连接的时间。
LastConnectedAt string
// LastDisconnectedAt 表示最近一次断连的时间。
LastDisconnectedAt string
// DisconnectCount 表示运行期间累计收到的断连次数。
DisconnectCount int
// RegisterReplayCount 表示累计成功重放 register 的次数。
RegisterReplayCount int
// RegisterReplayFailureCount 表示累计 register 重放失败次数。
RegisterReplayFailureCount int
// LastErrorKind 表示最近一次错误的分类。
LastErrorKind string
// LastError 表示最近一次错误文本。
LastError string
// LastErrorAt 表示最近一次错误发生时间。
LastErrorAt string
}
Status 描述当前 agent 联动控制器的最新状态。
type WatchEventParseError ¶ added in v0.0.8
type WatchEventParseError struct {
// EventType 表示解析失败的事件类型。
EventType string
// EventId 表示服务端 SSE 事件 ID。
EventId string
// Payload 表示原始 data 内容,便于调试坏帧。
Payload string
// Err 表示底层 JSON 解析错误。
Err error
}
WatchEventParseError 表示 SSE 事件帧存在结构化载荷解析错误。
func (*WatchEventParseError) Error ¶ added in v0.0.8
func (e *WatchEventParseError) Error() string
Error 返回可读错误文本。
func (*WatchEventParseError) Unwrap ¶ added in v0.0.8
func (e *WatchEventParseError) Unwrap() error
Unwrap 返回底层解析错误。
type WatchHTTPStatusError ¶ added in v0.0.8
type WatchHTTPStatusError struct {
// StatusCode 表示 HTTP 状态码。
StatusCode int
// Status 表示原始 HTTP 状态文本。
Status string
}
WatchHTTPStatusError 表示 `/watch` 接口返回了非 200 状态码。
func (*WatchHTTPStatusError) Error ¶ added in v0.0.8
func (e *WatchHTTPStatusError) Error() string
Error 返回可读错误文本。
type WatchSource ¶
type WatchSource struct {
// contains filtered or unexported fields
}
WatchSource 基于 sidecar-agent 的 `/watch` 长连接接口输出连接事件。
func NewWatchSource ¶
func NewWatchSource(watchURL string, reconnectInterval time.Duration) *WatchSource
NewWatchSource 创建一个新的长连接事件源。
func (*WatchSource) Subscribe ¶
func (s *WatchSource) Subscribe(ctx context.Context) (<-chan ConnectionEvent, error)
Subscribe 启动后台重连循环,并把连接状态变化转换成事件流。