8000 修改在循环中使用defer释放锁 by zdt3476 · Pull Request #13 · funny/fastway · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

修改在循环中使用defer释放锁 #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 28 additions & 19 deletions go/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,21 +268,28 @@ func (g *Gateway) acceptVirtualConn(pair [2]*link.Session, session *link.Session
for i := 0; i < 2; i++ {
state := pair[i].State.(*gwState)

state.Lock()
defer state.Unlock()
if state.disposed {
return false
}
checkOk := func () bool {
state.Lock()
defer state.Unlock()
if state.disposed {
return false
}

if pair[i] == session && maxConn != 0 && len(state.virtualConns) >= maxConn {
return false
}
if pair[i] == session && maxConn != 0 && len(state.virtualConns) >= maxConn {
return false
}

if _, exists := state.virtualConns[connID]; exists {
panic("Virtual Connection Already Exists")
}
if _, exists := state.virtualConns[connID]; exists {
panic("Virtual Connection Already Exists")
}

state.virtualConns[connID] = struct{}{}
state.virtualConns[connID] = struct{}{}

return true
}
if !checkOk() {
return false
}
}

g.addVirtualConn(connID, pair)
Expand All @@ -306,12 +313,14 @@ func (g *Gateway) closeVirtualConn(connID uint32) {

for i := 0; i < 2; i++ {
state := pair[i].State.(*gwState)
state.Lock()
defer state.Unlock()
if state.disposed {
continue
}
delete(state.virtualConns, connID)
g.send(pair[i], g.encodeCloseCmd(connID))
func() {
state.Lock()
defer state.Unlock()
if state.disposed {
return
}
delete(state.virtualConns, connID)
g.send(pair[i], g.encodeCloseCmd(connID))
}()
}
}
0