补全过滤入口与直接兑换,优化兑换分类和 WSS 建连
- GUI 接线 Exchange 绑定:新增「直接兑换」按钮,结果带 category/message 并进入结果面板 - 补 -max-price/-min-left(CLI 与 GUI 设置),Target 的价格与库存过滤不再悬空 - 删除死代码 sendUpdateUserInfo 及其请求结构体(浏览器流程不含该帧) - ClassifyExchange 改为多路判定:合并 msg/actPreCondition/exchangeInfo 文案,status=200 且有 orderId 视为成功,新增 condition 分类(precondition 门禁) - WSS 建连改为锁外 dial、锁内注册,同 uid 并发调用共享一次握手 - 默认传输层统一为 http(CLI/GUI/README),.gitignore 忽略 .DS_Store - 同步 Wails 依赖升级(2.12.0 -> 2.15.0)并重新生成前端绑定
This commit is contained in:
+49
-63
@@ -35,8 +35,13 @@ type WSSClient struct {
|
||||
Logger func(string)
|
||||
CallTimeout time.Duration
|
||||
|
||||
// dialFunc 仅用于测试注入,为 nil 时走 dialSession。
|
||||
dialFunc func(ctx context.Context, uid int64, cookie string) (*WSSSession, error)
|
||||
|
||||
mu sync.Mutex
|
||||
sessions map[string]*WSSSession
|
||||
// dialing 记录正在建连的 uid,避免并发调用重复握手。
|
||||
dialing map[string]chan struct{}
|
||||
}
|
||||
|
||||
type WSSSession struct {
|
||||
@@ -73,6 +78,7 @@ func NewWSSClient(logger func(string)) *WSSClient {
|
||||
Logger: logger,
|
||||
CallTimeout: 10 * time.Second,
|
||||
sessions: map[string]*WSSSession{},
|
||||
dialing: map[string]chan struct{}{},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -105,26 +111,54 @@ func (c *WSSClient) PrewarmActivity(ctx context.Context, uid int64, cookie strin
|
||||
return err
|
||||
}
|
||||
|
||||
// session 返回 uid 对应的连接,必要时新建。
|
||||
//
|
||||
// 握手与初始化在锁外完成,同一 uid 的并发调用共享同一次建连(single-flight),
|
||||
// 因此多账号预热/抢购时不会因为持有全局锁而串行化。
|
||||
func (c *WSSClient) session(ctx context.Context, uid int64, cookie string) (*WSSSession, error) {
|
||||
key := strconv.FormatInt(uid, 10)
|
||||
c.mu.Lock()
|
||||
if session := c.sessions[key]; session != nil && !session.isClosed() {
|
||||
c.mu.Unlock()
|
||||
return session, nil
|
||||
}
|
||||
c.mu.Unlock()
|
||||
for {
|
||||
c.mu.Lock()
|
||||
if session := c.sessions[key]; session != nil && !session.isClosed() {
|
||||
c.mu.Unlock()
|
||||
return session, nil
|
||||
}
|
||||
dialing := c.dialing[key]
|
||||
if dialing == nil {
|
||||
dialing = make(chan struct{})
|
||||
c.dialing[key] = dialing
|
||||
c.mu.Unlock()
|
||||
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
if session := c.sessions[key]; session != nil && !session.isClosed() {
|
||||
return session, nil
|
||||
session, err := c.dial(ctx, uid, cookie)
|
||||
|
||||
c.mu.Lock()
|
||||
delete(c.dialing, key)
|
||||
if err == nil {
|
||||
c.sessions[key] = session
|
||||
}
|
||||
c.mu.Unlock()
|
||||
close(dialing)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return session, nil
|
||||
}
|
||||
c.mu.Unlock()
|
||||
|
||||
// 已有同 uid 的建连在途,等它结束后复用结果或自行重试。
|
||||
select {
|
||||
case <-dialing:
|
||||
case <-ctx.Done():
|
||||
return nil, ctx.Err()
|
||||
}
|
||||
}
|
||||
session, err := c.dialSession(ctx, uid, cookie)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
func (c *WSSClient) dial(ctx context.Context, uid int64, cookie string) (*WSSSession, error) {
|
||||
if c.dialFunc != nil {
|
||||
return c.dialFunc(ctx, uid, cookie)
|
||||
}
|
||||
c.sessions[key] = session
|
||||
return session, nil
|
||||
return c.dialSession(ctx, uid, cookie)
|
||||
}
|
||||
|
||||
func (c *WSSClient) dialSession(ctx context.Context, uid int64, cookie string) (*WSSSession, error) {
|
||||
@@ -383,15 +417,6 @@ func (s *WSSSession) callGetConfig(ctx context.Context) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *WSSSession) sendUpdateUserInfo(ctx context.Context, launchGUID string) error {
|
||||
req := &wsUpdateUserInfoReq{AppSrc: "HUYA&ZH&2052", GUID: launchGUID}
|
||||
payload, err := encodeWSSStructCommand(WSSCmdUpdateUserInfoReq, req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return s.writePayload(ctx, payload)
|
||||
}
|
||||
|
||||
func (s *WSSSession) sendUpdateUserExps(ctx context.Context) error {
|
||||
payload, err := encodeWSSStructCommand(WSSCmdUpdateUserExpsReq, &wsUpdateUserExpsReq{})
|
||||
if err != nil {
|
||||
@@ -631,45 +656,6 @@ func (r *wsLaunchReq) WriteTo(w *taf.Writer) error {
|
||||
return w.WriteStructEnd()
|
||||
}
|
||||
|
||||
type wsUpdateUserInfoReq struct {
|
||||
AppSrc string
|
||||
GUID string
|
||||
}
|
||||
|
||||
func (r *wsUpdateUserInfoReq) WriteTo(w *taf.Writer) error {
|
||||
if err := w.WriteString(0, r.AppSrc); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := w.WriteString(1, r.GUID); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := w.WriteInt32(2, 0); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := w.WriteInt32(3, 0); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := w.WriteStructBegin(6); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := w.WriteMapStringString(0, nil); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := w.WriteMapStringString(1, nil); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := w.WriteInt32(2, 0); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := w.WriteStructEnd(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := w.WriteMapStringString(7, nil); err != nil {
|
||||
return err
|
||||
}
|
||||
return w.WriteInt32(8, 0)
|
||||
}
|
||||
|
||||
type wsUpdateUserExpsReq struct{}
|
||||
|
||||
func (r *wsUpdateUserExpsReq) WriteTo(w *taf.Writer) error {
|
||||
|
||||
@@ -1,11 +1,15 @@
|
||||
package huya
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"net/url"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"huya-go/internal/taf"
|
||||
)
|
||||
@@ -96,3 +100,63 @@ func TestNewWSSCommandTraceMatchesBrowserShape(t *testing.T) {
|
||||
t.Fatalf("trace suffix = %q, want :0:0", trace)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSessionSharesSingleDialPerUID(t *testing.T) {
|
||||
client := NewWSSClient(nil)
|
||||
dialStarted := make(chan struct{}, 1)
|
||||
release := make(chan struct{})
|
||||
var dials atomic.Int32
|
||||
client.dialFunc = func(ctx context.Context, uid int64, cookie string) (*WSSSession, error) {
|
||||
dials.Add(1)
|
||||
select {
|
||||
case dialStarted <- struct{}{}:
|
||||
default:
|
||||
}
|
||||
<-release
|
||||
return &WSSSession{
|
||||
client: client,
|
||||
uid: uid,
|
||||
cookie: cookie,
|
||||
closed: make(chan struct{}),
|
||||
pending: map[int32]chan rpcResult{},
|
||||
}, nil
|
||||
}
|
||||
|
||||
const callers = 4
|
||||
sessions := make([]*WSSSession, callers)
|
||||
var wg sync.WaitGroup
|
||||
for i := 0; i < callers; i++ {
|
||||
wg.Add(1)
|
||||
go func(i int) {
|
||||
defer wg.Done()
|
||||
session, err := client.session(context.Background(), 42, "yyuid=42")
|
||||
if err != nil {
|
||||
t.Errorf("session: %v", err)
|
||||
return
|
||||
}
|
||||
sessions[i] = session
|
||||
}(i)
|
||||
}
|
||||
|
||||
select {
|
||||
case <-dialStarted:
|
||||
case <-time.After(time.Second):
|
||||
t.Fatal("dial was not started")
|
||||
}
|
||||
// 留出时间让其余调用进入等待分支。
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
close(release)
|
||||
wg.Wait()
|
||||
|
||||
if got := dials.Load(); got != 1 {
|
||||
t.Fatalf("dials=%d, want 1", got)
|
||||
}
|
||||
for i, session := range sessions {
|
||||
if session == nil {
|
||||
t.Fatalf("caller %d got nil session", i)
|
||||
}
|
||||
if session != sessions[0] {
|
||||
t.Fatalf("caller %d got a different session", i)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user