Skip to content

Commit

Permalink
server: close connection when tidb server closed. (#8446)
Browse files Browse the repository at this point in the history
  • Loading branch information
crazycs520 authored and tiancaiamao committed Nov 27, 2018
1 parent cadab30 commit 654964a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
23 changes: 19 additions & 4 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,17 +351,32 @@ func (s *Server) Kill(connectionID uint64, query bool) {
return
}

killConn(conn, query)
}

func killConn(conn *clientConn, query bool) {
if !query {
// Mark the client connection status as WaitShutdown, when the goroutine detect
// this, it will end the dispatch loop and exit.
atomic.StoreInt32(&conn.status, connStatusWaitShutdown)
}

conn.mu.RLock()
cancelFunc := conn.mu.cancelFunc
conn.mu.RUnlock()
if cancelFunc != nil {
cancelFunc()
}
}

if !query {
// Mark the client connection status as WaitShutdown, when the goroutine detect
// this, it will end the dispatch loop and exit.
atomic.StoreInt32(&conn.status, connStatusWaitShutdown)
// KillAllConnections kills all connections when server is not gracefully shutdown.
func (s *Server) KillAllConnections() {
s.rwlock.Lock()
defer s.rwlock.Unlock()
log.Info("[server] kill all connections.")

for _, conn := range s.clients {
killConn(conn, false)
}
}

Expand Down
2 changes: 2 additions & 0 deletions tidb-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,8 @@ func closeDomainAndStorage() {
func cleanup() {
if graceful {
svr.GracefulDown()
} else {
svr.KillAllConnections()
}
closeDomainAndStorage()
}

0 comments on commit 654964a

Please sign in to comment.