server

package
v0.0.0-...-120110f Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2026 License: MIT Imports: 41 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultHookTimeout 默认 hook 执行超时时间
	DefaultHookTimeout = 30 * time.Second
	// DefaultMaxConcurrentHooks 默认最大并发执行的 hook 数量
	DefaultMaxConcurrentHooks = 10
	// DefaultHookAcquireTimeout 默认获取 semaphore 的超时时间
	DefaultHookAcquireTimeout = 5 * time.Second
)

Variables

View Source
var ErrTooManyConcurrentHooks = errors.New("too many concurrent hooks, execution timeout")

ErrTooManyConcurrentHooks 表示在等待时限内没有拿到执行槽位

Functions

func GetAsyncHookWaitGroup

func GetAsyncHookWaitGroup() *sync.WaitGroup

GetAsyncHookWaitGroup 返回异步 hook 的 WaitGroup,用于优雅关闭

func HandleError

func HandleError(w http.ResponseWriter, r *http.Request, err error, requestID, hookID string)

HandleError 统一处理错误并写入HTTP响应

func HandleErrorPlain

func HandleErrorPlain(w http.ResponseWriter, err error, requestID, hookID string)

HandleErrorPlain 处理错误并返回纯文本响应(用于向后兼容)

func HandleErrorWithCustomMessage

func HandleErrorWithCustomMessage(w http.ResponseWriter, err error, requestID, hookID, customMessage string, statusCode int)

HandleErrorWithCustomMessage 处理错误并使用自定义消息

func IsClientError

func IsClientError(err error) bool

IsClientError 检查错误是否为客户端错误

func IsServerError

func IsServerError(err error) bool

IsServerError 检查错误是否为服务器错误

func IsTimeoutError

func IsTimeoutError(err error) bool

IsTimeoutError 检查错误是否为超时错误

Types

type ErrorResponse

type ErrorResponse struct {
	Error     string `json:"error"`
	Message   string `json:"message,omitempty"`
	RequestID string `json:"request_id,omitempty"`
	HookID    string `json:"hook_id,omitempty"`
}

ErrorResponse 标准错误响应格式

type ErrorType

type ErrorType string

ErrorType 定义错误类型

const (
	// ErrorTypeClient 客户端错误(4xx)
	ErrorTypeClient ErrorType = "client"
	// ErrorTypeServer 服务器错误(5xx)
	ErrorTypeServer ErrorType = "server"
	// ErrorTypeTimeout 超时错误(408/504)
	ErrorTypeTimeout ErrorType = "timeout"
)

type HTTPError

type HTTPError struct {
	Type      ErrorType
	Status    int
	Message   string
	Err       error
	RequestID string
	HookID    string
}

HTTPError 封装HTTP错误信息

func ClassifyError

func ClassifyError(err error, requestID, hookID string) *HTTPError

ClassifyError 根据错误类型分类错误

func NewHTTPError

func NewHTTPError(errType ErrorType, status int, message string, err error) *HTTPError

NewHTTPError 创建新的HTTP错误

func (*HTTPError) Error

func (e *HTTPError) Error() string

func (*HTTPError) WithHookID

func (e *HTTPError) WithHookID(hookID string) *HTTPError

WithHookID 设置Hook ID

func (*HTTPError) WithRequestID

func (e *HTTPError) WithRequestID(requestID string) *HTTPError

WithRequestID 设置请求ID

type HookExecutor

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

HookExecutor 管理 hook 执行的并发控制和超时

func NewHookExecutor

func NewHookExecutor(maxConcurrent int, defaultTimeout time.Duration) *HookExecutor

NewHookExecutor 已废弃,请使用 NewHookExecutorWithFunc 此函数会 panic,因为现在 handleHook 需要 appFlags 参数

func NewHookExecutorWithFunc

func NewHookExecutorWithFunc(maxConcurrent int, defaultTimeout time.Duration, executorFunc func(ctx context.Context, h *hook.Hook, r *hook.Request, w http.ResponseWriter) (string, error)) *HookExecutor

NewHookExecutorWithFunc 创建新的 HookExecutor 实例,允许自定义执行函数(主要用于测试)

func (*HookExecutor) Acquire

func (he *HookExecutor) Acquire(ctx context.Context, acquireTimeout time.Duration) (func(), error)

Acquire 获取一个执行槽位,返回的 release 必须且只能调用一次。

func (*HookExecutor) Execute

func (he *HookExecutor) Execute(ctx context.Context, h *hook.Hook, r *hook.Request, w http.ResponseWriter, acquireTimeout time.Duration) (string, error)

Execute 执行 hook,带并发控制和超时

func (*HookExecutor) ExecuteAcquired

func (he *HookExecutor) ExecuteAcquired(ctx context.Context, h *hook.Hook, r *hook.Request, w http.ResponseWriter) (string, error)

ExecuteAcquired 在调用方已持有执行槽位的前提下执行 hook(带超时)。

func (*HookExecutor) GetDefaultTimeout

func (he *HookExecutor) GetDefaultTimeout() time.Duration

GetDefaultTimeout 获取默认超时时间(用于测试)

func (*HookExecutor) GetMaxConcurrent

func (he *HookExecutor) GetMaxConcurrent() int

GetMaxConcurrent 获取最大并发数(用于测试)

type Server

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

Server 管理 HTTP 服务器和优雅关闭

func Launch

func Launch(appFlags flags.AppFlags, addr string, ln net.Listener) *Server

Launch 启动 HTTP 服务器并返回 Server 实例(基于 fiber.App)

func (*Server) IsShuttingDown

func (s *Server) IsShuttingDown() bool

IsShuttingDown 检查服务器是否正在关闭

func (*Server) Shutdown

func (s *Server) Shutdown(ctx context.Context) error

Shutdown 优雅关闭服务器:先等异步 hook WaitGroup,再关闭 Fiber

Jump to

Keyboard shortcuts

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