active

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 19, 2026 License: MIT Imports: 2 Imported by: ? modules, 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

Index

Constants

This section is empty.

Variables

View Source
var Global = &ActiveStore{items: make(map[string]*ActiveRequest)}

Global 全局活跃请求存储实例

Functions

func BroadcastComplete

func BroadcastComplete(log *LogRecordData)

BroadcastComplete 广播一条 complete 事件,携带完整 DB 日志记录。 应在 RecordConsumeLog 成功写入后调用。

func StartCleanupLoop

func StartCleanupLoop(interval time.Duration, maxAge time.Duration)

StartCleanupLoop 启动后台协程,定期清理超时的孤儿条目

func Subscribe

func Subscribe() chan RequestEvent

Subscribe registers a new subscriber channel. Buffer size 64 to tolerate brief subscriber slowness; EventSource reconnect will naturally resync if events are still dropped.

func Unsubscribe

func Unsubscribe(ch chan RequestEvent)

Unsubscribe removes and closes a subscriber channel

Types

type ActiveRequest

type ActiveRequest struct {
	RequestID        string `json:"request_id"`
	UserID           int    `json:"user_id"`
	TokenName        string `json:"token_name"`
	ModelName        string `json:"model_name"`
	ChannelID        int    `json:"channel"`
	ChannelName      string `json:"channel_name"`
	Group            string `json:"group"`
	IsStream         bool   `json:"is_stream"`
	StartedAt        int64  `json:"started_at"`
	ElapsedMs        int64  `json:"elapsed_ms"`
	FirstTokenMs     int64  `json:"first_token_ms"` // 首字耗时(TTFT,ms),首字写出后由 ttftWriter 实时更新
	RelayMode        int    `json:"relay_mode"`
	RequestBody      string `json:"request_body"`
	RequestHeader    string `json:"request_header"`
	HasRequestBody   bool   `json:"has_request_body"`
	HasRequestHeader bool   `json:"has_request_header"`
	Username         string `json:"username,omitempty"`
}

ActiveRequest 表示一个正在转发中的请求

type ActiveStore

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

ActiveStore 并发安全的活跃请求存储

func (*ActiveStore) Add

func (s *ActiveStore) Add(req *ActiveRequest)

Add 添加一个活跃请求。广播在锁内完成以保证 start 事件严格在 Remove 之前送达。

func (*ActiveStore) CleanupStale

func (s *ActiveStore) CleanupStale(maxAge int64) int

CleanupStale 移除超过 maxAge 毫秒的孤儿条目,并广播 end 事件通知 SSE 订阅者

func (*ActiveStore) Get

func (s *ActiveStore) Get(requestID string) *ActiveRequest

Get 获取指定请求 ID 的活跃请求

func (*ActiveStore) List

func (s *ActiveStore) List() []ActiveRequest

List 返回所有活跃请求的副本列表,动态计算实时耗时。 返回的是值副本(非指针),调用方可安全地并发修改/序列化。

func (*ActiveStore) Remove

func (s *ActiveStore) Remove(requestID string)

Remove 移除指定请求 ID 的活跃请求。end 事件仅携带 request_id 以缩减 SSE 传输量。

func (*ActiveStore) Update

func (s *ActiveStore) Update(requestID string, upd func(*ActiveRequest))

Update 更新指定请求 ID 的活跃请求信息。广播在锁内完成以保证顺序。

type LogRecordData

type LogRecordData struct {
	Id               int    `json:"id"`
	UserId           int    `json:"user_id"`
	CreatedAt        int64  `json:"created_at"`
	Content          string `json:"content"`
	Username         string `json:"username"`
	TokenName        string `json:"token_name"`
	ModelName        string `json:"model_name"`
	Quota            int    `json:"quota"`
	PromptTokens     int    `json:"prompt_tokens"`
	CompletionTokens int    `json:"completion_tokens"`
	CachedTokens     int    `json:"cached_tokens"`
	ChannelId        int    `json:"channel"`
	RequestId        string `json:"request_id"`
	ElapsedTime      int64  `json:"elapsed_time"`
	FirstTokenTime   int64  `json:"first_token_time"`
	IsStream         bool   `json:"is_stream"`
	ChannelName      string `json:"channel_name"`
	HasRequestBody   bool   `json:"has_request_body"`
	HasResponseBody  bool   `json:"has_response_body"`
	HasRequestHeader bool   `json:"has_request_header"`
}

LogRecordData DB 写入成功后推送给前端的完整日志记录,用于 SSE complete 事件。

type RequestEvent

type RequestEvent struct {
	Type      string         `json:"type"`                 // "start", "update", "end", "complete"
	RequestID string         `json:"request_id,omitempty"` // 仅 end 事件需要
	Data      *ActiveRequest `json:"data,omitempty"`       // start/update 携带(指针才能 omitempty)
	Log       *LogRecordData `json:"log,omitempty"`        // 仅 complete 事件携带
}

RequestEvent is a broadcast event for SSE subscribers

Jump to

Keyboard shortcuts

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