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:
parent
8d1e5fb04c
commit
85a81c88b7
@ -75,7 +75,7 @@ type QQClient struct {
|
||||
highwayApplyUpSeq int32
|
||||
eventHandlers *eventHandlers
|
||||
|
||||
groupListLock *sync.Mutex
|
||||
groupListLock sync.Mutex
|
||||
msgSvcLock sync.Mutex
|
||||
}
|
||||
|
||||
@ -143,7 +143,6 @@ func NewClientMd5(uin int64, passwordMd5 [16]byte) *QQClient {
|
||||
highwayApplyUpSeq: 77918,
|
||||
ksid: []byte("|454001228437590|A8.2.7.27f6ea96"),
|
||||
eventHandlers: &eventHandlers{},
|
||||
groupListLock: new(sync.Mutex),
|
||||
msgSvcCache: 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
|
||||
}
|
||||
|
||||
func (c *QQClient) ReloadGroupList(async ...bool) error {
|
||||
f := false
|
||||
if len(async) > 0 {
|
||||
f = async[0]
|
||||
}
|
||||
func (c *QQClient) ReloadGroupList() error {
|
||||
c.groupListLock.Lock()
|
||||
defer c.groupListLock.Unlock()
|
||||
list, err := func() ([]*GroupInfo, error) {
|
||||
if f {
|
||||
return c.GetGroupListAsync()
|
||||
} else {
|
||||
return c.GetGroupList()
|
||||
}
|
||||
}()
|
||||
list, err := c.GetGroupList()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -625,32 +614,19 @@ func (c *QQClient) GetGroupList() ([]*GroupInfo, error) {
|
||||
return nil, err
|
||||
}
|
||||
r := rsp.([]*GroupInfo)
|
||||
wg := sync.WaitGroup{}
|
||||
wg.Add(len(r))
|
||||
for _, group := range r {
|
||||
m, err := c.GetGroupMembers(group)
|
||||
if err != nil {
|
||||
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() {
|
||||
go func(g *GroupInfo, wg *sync.WaitGroup) {
|
||||
defer wg.Done()
|
||||
m, err := c.GetGroupMembers(g)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
g.Members = m
|
||||
}()
|
||||
}(group, &wg)
|
||||
}
|
||||
wg.Wait()
|
||||
return r, nil
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user