cookie

package
v0.0.0-...-f5d7f2c Latest Latest
Warning

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

Go to latest
Published: Feb 2, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Get

func Get(v any) ([]*http.Cookie, error)

Get 从结构体中获取 Cookie 列表,当传入参数为空、非结构体或其指针时返回错误

func GetJSON

func GetJSON(v any) ([]byte, error)

GetJSON 从结构体中获取 Cookie 列表并以 JSON 格式返回

func GetString

func GetString(v any) string

GetString 从结构体中获取 Cookie 列表并以字符串格式返回

func MustGet

func MustGet(v any) []*http.Cookie

MustGet 从结构体中获取 Cookie 列表,确定传入参数为结构体或其指针时可使用此函数

func Set

func Set(v any, cookies []*http.Cookie) error

Set 将 Cookie 列表设置到结构体中,当传入参数为空、非结构体或其指针时返回错误

func SetJSON

func SetJSON(v any, data []byte) error

SetJSON 将 JSON 格式的 Cookie 列表设置到结构体中

Types

type ForcedRefresher

type ForcedRefresher func(context.Context, http.CookieJar) error

ForcedRefresher 强制刷新器,每次检测时都认定 http.CookieJar 已失效,并将自身用于刷新

func (ForcedRefresher) IsValid

func (ForcedRefresher) Refresh

func (f ForcedRefresher) Refresh(ctx context.Context, jar http.CookieJar) error

type KeepaliveCookieJar

type KeepaliveCookieJar struct {
	// 任意 http.CookieJar 实例
	http.CookieJar

	// 刷新器
	Refresher Refresher

	// 错误上报
	OnError func(error)
	// contains filtered or unexported fields
}

KeepaliveCookieJar 是支持自动保活的 http.CookieJar

func Keepalive

func Keepalive(jar http.CookieJar, refresher Refresher, delayer req.Delayer, delay ...time.Duration) *KeepaliveCookieJar

Keepalive 立即开始保活 http.CookieJar

func KeepaliveWithContext

func KeepaliveWithContext(ctx context.Context, jar http.CookieJar, refresher Refresher, delayer req.Delayer, delay ...time.Duration) *KeepaliveCookieJar

KeepaliveWithContext 携带上下文立即开始保活 http.CookieJar

func (*KeepaliveCookieJar) Keepalive

func (k *KeepaliveCookieJar) Keepalive(ctx context.Context, delayer req.Delayer, delay ...time.Duration)

Keepalive 保活 http.CookieJar ,延迟器决定每次保活的间隔, 延时决定是否预先进行一次检测,传入正数则在延时后检测,不传入则立即检测,传入非正数则不预先检测

func (*KeepaliveCookieJar) Run

func (k *KeepaliveCookieJar) Run()

Run 检测 http.CookieJar 是否有效

func (*KeepaliveCookieJar) SinceLastVerified

func (k *KeepaliveCookieJar) SinceLastVerified() time.Duration

SinceLastVerified 获取距离上次验证成功时过去的时间

func (*KeepaliveCookieJar) State

func (k *KeepaliveCookieJar) State() State

State 获取 http.CookieJar 的验证状态

func (*KeepaliveCookieJar) StopKeepalive

func (k *KeepaliveCookieJar) StopKeepalive()

StopKeepalive 主动取消保活 http.CookieJar

func (*KeepaliveCookieJar) Verify

func (k *KeepaliveCookieJar) Verify(ctx context.Context)

Verify 携带上下文检测 http.CookieJar 是否有效

type Pool

type Pool struct {
	// 检测 http.CookieJar 是否有效的时间间隔
	Refresh time.Duration

	// 错误上报
	OnError func(error)
	// contains filtered or unexported fields
}

Pool 是自动保活的 http.CookieJar 的池,可以获取随机已验证的实例

func (*Pool) Add

func (p *Pool) Add(jar http.CookieJar, refresher Refresher, delay ...time.Duration) *KeepaliveCookieJar

Add 添加 http.CookieJar 并且开始保活

func (*Pool) Get

func (p *Pool) Get(index ...int) []*KeepaliveCookieJar

Get 获取有效下标的 http.CookieJar ,不指定下标时返回全部 http.CookieJar

func (*Pool) Random

func (p *Pool) Random() *KeepaliveCookieJar

Random 获取随机已验证的 http.CookieJar

func (*Pool) Stop

func (p *Pool) Stop()

Stop 停止所有 http.CookieJar 保活

type Refresher

type Refresher interface {
	// IsValid 检测 http.CookieJar 是否有效
	IsValid(context.Context, http.CookieJar) (bool, error)

	// Refresh 刷新 http.CookieJar
	Refresh(context.Context, http.CookieJar) error
}

Refresher 刷新器,用于检测和刷新 http.CookieJar

type State

type State int32

State 是 http.CookieJar 的验证状态

const (
	StateUnverified State = iota // 未验证
	StateVerifying               // 验证中
	StateVerified                // 已验证
	StateInvalid                 // 已失效
)

func Verify

func Verify(ctx context.Context, refresher Refresher, jar http.CookieJar) (State, error)

Verify 检测 http.CookieJar 是否有效,如果已经失效会进行一次刷新

Jump to

Keyboard shortcuts

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