1
0
mirror of https://github.com/Mrs4s/MiraiGo.git synced 2025-05-04 11:07:40 +08:00
why'd you write it in that way?
fixed obviously bad pattern.
fixed possibly race condition in GetGroupListAsync.
This commit is contained in:
wfjsw 2020-08-21 13:22:21 +08:00
parent 8d1e5fb04c
commit 85a81c88b7

View File

@ -75,7 +75,7 @@ type QQClient struct {
highwayApplyUpSeq int32 highwayApplyUpSeq int32
eventHandlers *eventHandlers eventHandlers *eventHandlers
groupListLock *sync.Mutex groupListLock sync.Mutex
msgSvcLock sync.Mutex msgSvcLock sync.Mutex
} }
@ -143,7 +143,6 @@ func NewClientMd5(uin int64, passwordMd5 [16]byte) *QQClient {
highwayApplyUpSeq: 77918, highwayApplyUpSeq: 77918,
ksid: []byte("|454001228437590|A8.2.7.27f6ea96"), ksid: []byte("|454001228437590|A8.2.7.27f6ea96"),
eventHandlers: &eventHandlers{}, eventHandlers: &eventHandlers{},
groupListLock: new(sync.Mutex),
msgSvcCache: utils.NewCache(time.Second * 15), msgSvcCache: utils.NewCache(time.Second * 15),
transCache: utils.NewCache(time.Second * 15), transCache: utils.NewCache(time.Second * 15),
} }
@ -598,20 +597,10 @@ func (c *QQClient) QueryFriendImage(target int64, hash []byte, size int32) (*mes
}, nil }, nil
} }
func (c *QQClient) ReloadGroupList(async ...bool) error { func (c *QQClient) ReloadGroupList() error {
f := false
if len(async) > 0 {
f = async[0]
}
c.groupListLock.Lock() c.groupListLock.Lock()
defer c.groupListLock.Unlock() defer c.groupListLock.Unlock()
list, err := func() ([]*GroupInfo, error) { list, err := c.GetGroupList()
if f {
return c.GetGroupListAsync()
} else {
return c.GetGroupList()
}
}()
if err != nil { if err != nil {
return err return err
} }
@ -625,32 +614,19 @@ func (c *QQClient) GetGroupList() ([]*GroupInfo, error) {
return nil, err return nil, err
} }
r := rsp.([]*GroupInfo) r := rsp.([]*GroupInfo)
wg := sync.WaitGroup{}
wg.Add(len(r))
for _, group := range r { for _, group := range r {
m, err := c.GetGroupMembers(group) go func(g *GroupInfo, wg *sync.WaitGroup) {
if err != nil { defer wg.Done()
continue
}
group.Members = m
}
return r, nil
}
func (c *QQClient) GetGroupListAsync() ([]*GroupInfo, error) {
rsp, err := c.sendAndWait(c.buildGroupListRequestPacket())
if err != nil {
return nil, err
}
r := rsp.([]*GroupInfo)
for _, group := range r {
g := group
go func() {
m, err := c.GetGroupMembers(g) m, err := c.GetGroupMembers(g)
if err != nil { if err != nil {
return return
} }
g.Members = m g.Members = m
}() }(group, &wg)
} }
wg.Wait()
return r, nil return r, nil
} }