v0.4.0:窗口调度/限流分类/可观测性,抢购改每账号独立循环,移除 CLI 只保留 GUI
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
package huya
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"huya-go/internal/wup"
|
||||
)
|
||||
|
||||
// ErrProtocolResponse 表示服务端响应的结构与协议约定不符(典型是缺少 tRsp/tResp)。
|
||||
//
|
||||
// 这类错误既不是网络错误也不是业务失败:它说明服务端回了一个解不出来的包
|
||||
// (风控拦截、错误信封、被截断的帧都可能)。单独标记出来是为了让上层能把它
|
||||
// 与普通失败分开统计(见 sniper.CategoryProtocol),并在报错里带上 WUP 诊断信息。
|
||||
var ErrProtocolResponse = errors.New("协议响应异常")
|
||||
|
||||
// rawFrameHeadBytes 是 debug 日志里打印的原始帧前缀长度。
|
||||
const rawFrameHeadBytes = 32
|
||||
|
||||
// protocolResponseError 组装「缺少 tRsp/tResp」错误,并带上 WUP 响应诊断。
|
||||
//
|
||||
// 过去这里只有一句文案,服务端返回的 iRet/status/context 全被丢掉,
|
||||
// 事后只能重放抓包(2026-10-16 pc-3 同一账号连续 13 次命中,无法定性)。
|
||||
func protocolResponseError(service, method string, resp *wup.Response) error {
|
||||
if resp == nil {
|
||||
return fmt.Errorf("%w: %s.%s 响应缺少 tRsp/tResp", ErrProtocolResponse, service, method)
|
||||
}
|
||||
return fmt.Errorf("%w: %s.%s 响应缺少 tRsp/tResp(%s)", ErrProtocolResponse, service, method, resp.Diagnose())
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package huya
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"huya-go/internal/wup"
|
||||
)
|
||||
|
||||
// TestProtocolResponseErrorCarriesDiagnostics 验证「缺少 tRsp/tResp」错误会带上 WUP 诊断信息,
|
||||
// 并且可以用 errors.Is 判定——上层据此把它单独归类(sniper.CategoryProtocol),
|
||||
// 而不是混进普通失败里(2026-10-16 pc-3 同一账号连续 13 次命中该错误)。
|
||||
func TestProtocolResponseErrorCarriesDiagnostics(t *testing.T) {
|
||||
resp := &wup.Response{
|
||||
Message: -1,
|
||||
Status: map[string]string{"STATUS_RESULT_CODE": "-1"},
|
||||
NewData: map[string][]byte{"tErr": {0x0c}},
|
||||
}
|
||||
err := protocolResponseError(ServantWebActUI, FuncExchangePrize, resp)
|
||||
if !errors.Is(err, ErrProtocolResponse) {
|
||||
t.Fatalf("错误应可用 errors.Is(ErrProtocolResponse) 判定: %v", err)
|
||||
}
|
||||
text := err.Error()
|
||||
for _, want := range []string{
|
||||
"响应缺少 tRsp/tResp",
|
||||
"iRet=-1",
|
||||
"STATUS_RESULT_CODE=-1",
|
||||
"keys={tErr(1B)}",
|
||||
} {
|
||||
if !strings.Contains(text, want) {
|
||||
t.Fatalf("错误信息 %q 缺少 %q", text, want)
|
||||
}
|
||||
}
|
||||
|
||||
empty := protocolResponseError(ServantWebActUI, FuncExchangePrize, nil)
|
||||
if !errors.Is(empty, ErrProtocolResponse) || !strings.Contains(empty.Error(), "响应缺少 tRsp/tResp") {
|
||||
t.Fatalf("空响应错误=%v", empty)
|
||||
}
|
||||
if errors.Is(errors.New("timeout"), ErrProtocolResponse) {
|
||||
t.Fatal("普通错误不该被判定为协议错误")
|
||||
}
|
||||
}
|
||||
@@ -204,6 +204,9 @@ func (c *HTTPClient) CallRPC(ctx context.Context, service, method string, reqStr
|
||||
if httpResp.StatusCode < 200 || httpResp.StatusCode >= 300 {
|
||||
return nil, fmt.Errorf("http status %d: %s", httpResp.StatusCode, string(respBody[:min(len(respBody), 200)]))
|
||||
}
|
||||
// debug 级打印原始帧前缀:协议异常(缺少 tRsp)时这是唯一的现场证据。
|
||||
c.logf("[HTTP] 响应 %s.%s status=%d %dB head=%s",
|
||||
service, method, httpResp.StatusCode, len(respBody), wup.HexHead(respBody, rawFrameHeadBytes))
|
||||
|
||||
var resp wup.Response
|
||||
if err := resp.Decode(respBody); err != nil {
|
||||
@@ -215,7 +218,7 @@ func (c *HTTPClient) CallRPC(ctx context.Context, service, method string, reqStr
|
||||
return nil, err
|
||||
}
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("%s.%s 响应缺少 tRsp/tResp", service, method)
|
||||
return nil, protocolResponseError(service, method, &resp)
|
||||
}
|
||||
}
|
||||
return &resp, nil
|
||||
|
||||
@@ -448,9 +448,13 @@ func (s *WSSSession) readLoop() {
|
||||
case WSSCmdWupRsp:
|
||||
var resp wup.Response
|
||||
if err := resp.Decode(cmd.Data); err != nil {
|
||||
s.client.logf("[WSS] 解码 WUP 响应失败: %v", err)
|
||||
s.client.logf("[WSS] 解码 WUP 响应失败: %v (cmd=%d data=%dB head=%s)",
|
||||
err, cmd.CmdType, len(cmd.Data), wup.HexHead(cmd.Data, rawFrameHeadBytes))
|
||||
continue
|
||||
}
|
||||
// debug 级打印原始帧前缀:协议异常(缺少 tRsp)时这是唯一的现场证据。
|
||||
s.client.logf("[WSS] 收到 WUP 响应 requestID=%d data=%dB head=%s",
|
||||
resp.RequestID, len(cmd.Data), wup.HexHead(cmd.Data, rawFrameHeadBytes))
|
||||
s.resolveRequest(resp.RequestID, rpcResult{resp: &resp})
|
||||
case WSSCmdHeartBeatAck, WSSCmdHeartBeatRsp:
|
||||
// 心跳响应无需处理。
|
||||
@@ -712,7 +716,7 @@ func (s *WSSSession) callRPC(ctx context.Context, service, method string, reqStr
|
||||
return nil, err
|
||||
}
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("%s.%s 响应缺少 tRsp/tResp", service, method)
|
||||
return nil, protocolResponseError(service, method, resp)
|
||||
}
|
||||
}
|
||||
return resp, nil
|
||||
|
||||
Reference in New Issue
Block a user