1
0
mirror of https://github.com/Mrs4s/MiraiGo.git synced 2025-05-04 11:07:40 +08:00

client/internal/highway: shorten lock time

This commit is contained in:
wdvxdr 2023-02-19 19:59:54 +08:00
parent 4716f69c1a
commit 3b97ce341b

View File

@ -40,11 +40,12 @@ type Session struct {
SigSession []byte SigSession []byte
SessionKey []byte SessionKey []byte
seq int32
addrMu sync.Mutex addrMu sync.Mutex
idx int idx int
SsoAddr []Addr SsoAddr []Addr
seq int32
idleMu sync.Mutex idleMu sync.Mutex
idleCount int idleCount int
idle *idle idle *idle
@ -209,6 +210,14 @@ func (s *Session) connect(addr Addr) (persistConn, error) {
return pc, nil return pc, nil
} }
func (s *Session) nextAddr() Addr {
s.addrMu.Lock()
defer s.addrMu.Unlock()
addr := s.SsoAddr[s.idx]
s.idx = (s.idx + 1) % len(s.SsoAddr)
return addr
}
func (s *Session) selectConn() (pc persistConn, err error) { func (s *Session) selectConn() (pc persistConn, err error) {
for { // select from idle pc for { // select from idle pc
pc = s.getIdleConn() pc = s.getIdleConn()
@ -222,13 +231,9 @@ func (s *Session) selectConn() (pc persistConn, err error) {
} }
} }
s.addrMu.Lock()
defer s.addrMu.Unlock()
try := 0 try := 0
idx := s.idx
for { for {
addr := s.SsoAddr[idx] addr := s.nextAddr()
idx = (idx + 1) % len(s.SsoAddr) // move next
pc, err = s.connect(addr) pc, err = s.connect(addr)
if err == nil { if err == nil {
break break
@ -238,6 +243,5 @@ func (s *Session) selectConn() (pc persistConn, err error) {
break break
} }
} }
s.idx = idx
return return
} }