session

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2026 License: MIT Imports: 2 Imported by: 0 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

Overview

Package session 的空实现。

Package session 管理 cookie 池,是网易云登录态的一等公民。

网易云风控按账号/Cookie 维度,不按接口维度——所以 GetAvailable 的参数是 AuthRequirement enum 而非字符串。登录类接口(captcha/login/qrcode)是创建 新 session 的源头,走 Save 路径,不经 GetAvailable。

SessionStore 接口由 engine 层依赖(依赖倒置),运行时层注入 Redis 实现。

Index

Constants

This section is empty.

Variables

View Source
var ErrNoAvailableSession = errors.New("所有 session 均不可用")

ErrNoAvailableSession 是全部 session 都不可用时的错误。

Functions

This section is empty.

Types

type AuthRequirement

type AuthRequirement int

AuthRequirement 表达一次调用需要哪种登录态,驱动 cookie 池选取。

const (
	// AuthAnonymous 用共享匿名 cookie 池(网易云大部分查询接口可用)。
	AuthAnonymous AuthRequirement = iota
	// AuthLoggedIn 用已登录 cookie 池(需登录态的接口,含健康度/权重选取)。
	AuthLoggedIn
	// AuthOptional 有登录态就用、没有就匿名:调用方经 ctx 带了 cookie 则携带,否则按匿名请求。
	// 用于结果随账号变化、但匿名也有意义的接口(如播放直链:VIP 账号拿完整音源,匿名只有免费歌)。
	// 不取会话池——池的账号选取与按账号隔离缓存在接入会话池时再定。
	AuthOptional
)

type NoopStore

type NoopStore struct{}

NoopStore 是 SessionStore 的空实现,不做任何存储。 地基阶段未接入 Redis 时用,GetAvailable 返回未授权错误。

func (NoopStore) GetAvailable

GetAvailable 永远返回未授权(无可用 session)。

func (NoopStore) ListAll

func (NoopStore) ListAll(context.Context) ([]string, error)

ListAll 返回空切片。

func (NoopStore) ReportFailure

func (NoopStore) ReportFailure(string, error)

ReportFailure 不做任何事。

func (NoopStore) ReportSuccess

func (NoopStore) ReportSuccess(string)

ReportSuccess 不做任何事。

func (NoopStore) Save

Save 不做任何事。

type Session

type Session struct {
	// UserID 是网易云用户 ID。
	UserID string
	// Cookie 是登录态 Cookie 字符串。
	Cookie string
}

Session 是一个登录态凭证。

type SessionStore

type SessionStore interface {
	// GetAvailable 按登录态需求选取一个可用 session。
	// 全部不可用时返回 ErrNoAvailableSession。
	GetAvailable(ctx context.Context, req AuthRequirement) (*Session, error)

	// ReportSuccess 上报某 session 调用成功(用于健康度统计)。
	ReportSuccess(sessionID string)

	// ReportFailure 上报某 session 调用失败(用于风控/降权)。
	ReportFailure(sessionID string, err error)

	// Save 保存新 session(登录成功后写入 cookie 池)。
	Save(ctx context.Context, s *Session) error

	// ListAll 列出全部 session 的 userID(worker 健康检查用)。
	ListAll(ctx context.Context) ([]string, error)
}

SessionStore 管理 cookie 池,提供选取和创建两种路径。

选取路径:engine 调 GetAvailable 按 AuthRequirement 取一个可用 session, 调用结果通过 ReportSuccess/ReportFailure 反馈,驱动健康度/权重。 创建路径:登录类接口调 Save 写入新 session,不经 GetAvailable。

Jump to

Keyboard shortcuts

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