- plugins/channels/kook: config/adapter/gateway/websocket/send/message/errors — 实现 PlatformAdapter 13方法 - WS 有状态网关: 取gateway地址→连WS→信令0/1/2/3/4/6 + 30s心跳/6s超时 + resume(拼接sn/session_id) + 指数退避重连 - 收事件: 信令0 → UnifiedMessage(word注 channel_type/type/target_id/extra.author→字段) - 发消息: /message/create(频道) + /direct-message/create(私聊,修正正确端点,非user-chat) + Bearer鉴权 - 注册 channels.go + CHANNELS_JSON 多账号 + channelbridge.configured 加 kook(bot_token非空才连) - 框架增强: internal/channel/adapter/wsutil(连接管理/心跳/重连/resume骨架,复用gorilla,KOOK用上) + httpclient(Bearer+envelope归一,KOOK用) - 全仓 go build/vet/test 绿, 零新依赖(复用餐gorilla), 未动telegram/qq/weixin/webhook契约 - §4.6 隔离/黑白名单/第三方边界未破坏 注: KOOK WS/WH 双模式待补(ReceiveMode), 实测需主人提供 CHANNEL_KOOK_TOKEN
55 lines
1.8 KiB
Go
55 lines
1.8 KiB
Go
// Package wsutil provides a small, reusable WebSocket gateway client for
|
|
// stateful IM channels (KOOK, QQ OneBot, and similar). It owns the transport
|
|
// mechanics that those channels share — dialing a gateway, running a read loop,
|
|
// sending an application-level heartbeat, and reconnecting with exponential
|
|
// backoff (optionally splicing resume parameters onto the gateway URL) — while
|
|
// leaving the platform-specific signaling to the adapter via Handler hooks.
|
|
//
|
|
// It depends only on the standard library plus github.com/gorilla/websocket and
|
|
// internal/logger, never on a specific channel adapter.
|
|
package wsutil
|
|
|
|
import "time"
|
|
|
|
// Backoff is a resettable exponential backoff delay scheduler. It yields delays
|
|
// starting at base and doubling up to cap (for example 2s, 4s, 8s, ... capped at
|
|
// 60s), which is the WebSocket gateway reconnect pattern used by stateful IM
|
|
// channels. It is safe for concurrent use by a single goroutine.
|
|
type Backoff struct {
|
|
base time.Duration
|
|
max time.Duration
|
|
attempts int
|
|
}
|
|
|
|
// NewBackoff creates a backoff with the given base and cap. A non-positive base
|
|
// defaults to 1s and a non-positive cap to 60s.
|
|
func NewBackoff(base, cap time.Duration) *Backoff {
|
|
if base <= 0 {
|
|
base = time.Second
|
|
}
|
|
if cap <= 0 {
|
|
cap = 60 * time.Second
|
|
}
|
|
return &Backoff{base: base, max: cap}
|
|
}
|
|
|
|
// Next returns the delay to sleep before the next attempt and advances the
|
|
// internal attempt counter. It never exceeds the configured cap.
|
|
func (b *Backoff) Next() time.Duration {
|
|
var d time.Duration
|
|
if b.attempts >= 60 {
|
|
d = b.max
|
|
} else {
|
|
d = b.base << uint(b.attempts)
|
|
}
|
|
if d > b.max || d <= 0 {
|
|
d = b.max
|
|
}
|
|
b.attempts++
|
|
return d
|
|
}
|
|
|
|
// Reset restores the backoff to its initial state after a successful connect, so
|
|
// a later drop starts the ramp over from the base delay.
|
|
func (b *Backoff) Reset() { b.attempts = 0 }
|