Minimize lock time in Client.acceptConnections

This commit is contained in:
Matt Joiner 2018-06-10 10:29:19 +10:00
parent ac6ba9f021
commit 5a53d29c75
1 changed files with 20 additions and 16 deletions

View File

@ -378,15 +378,17 @@ func (cl *Client) rejectAccepted(conn net.Conn) bool {
} }
func (cl *Client) acceptConnections(l net.Listener) { func (cl *Client) acceptConnections(l net.Listener) {
cl.mu.Lock()
defer cl.mu.Unlock()
for { for {
cl.waitAccept()
cl.mu.Unlock()
conn, err := l.Accept() conn, err := l.Accept()
conn = pproffd.WrapNetConn(conn) conn = pproffd.WrapNetConn(conn)
cl.mu.Lock() cl.mu.RLock()
if cl.closed.IsSet() { closed := cl.closed.IsSet()
reject := false
if conn != nil {
reject = cl.rejectAccepted(conn)
}
cl.mu.RUnlock()
if closed {
if conn != nil { if conn != nil {
conn.Close() conn.Close()
} }
@ -398,16 +400,18 @@ func (cl *Client) acceptConnections(l net.Listener) {
// routine just fucked off. // routine just fucked off.
return return
} }
log.Fmsg("accepted %s connection from %s", conn.RemoteAddr().Network(), conn.RemoteAddr()).AddValue(debugLogValue).Log(cl.logger) go func() {
go torrent.Add(fmt.Sprintf("accepted conn remote IP len=%d", len(missinggo.AddrIP(conn.RemoteAddr()))), 1) if reject {
go torrent.Add(fmt.Sprintf("accepted conn network=%s", conn.RemoteAddr().Network()), 1) torrent.Add("rejected accepted connections", 1)
go torrent.Add(fmt.Sprintf("accepted on %s listener", l.Addr().Network()), 1) conn.Close()
if cl.rejectAccepted(conn) { } else {
go torrent.Add("rejected accepted connections", 1) go cl.incomingConnection(conn)
conn.Close() }
} else { log.Fmsg("accepted %s connection from %s", conn.RemoteAddr().Network(), conn.RemoteAddr()).AddValue(debugLogValue).Log(cl.logger)
go cl.incomingConnection(conn) torrent.Add(fmt.Sprintf("accepted conn remote IP len=%d", len(missinggo.AddrIP(conn.RemoteAddr()))), 1)
} torrent.Add(fmt.Sprintf("accepted conn network=%s", conn.RemoteAddr().Network()), 1)
torrent.Add(fmt.Sprintf("accepted on %s listener", l.Addr().Network()), 1)
}()
} }
} }