Added gracefully shutdown for clients

Added the gracefully shutdown for all client connections to release all
these resources.
This commit is contained in:
Thorsten Sommer 2015-01-02 18:45:38 +01:00
parent 6ae3b46fc1
commit 316b4a6c54
2 changed files with 26 additions and 3 deletions

View File

@ -68,9 +68,30 @@ func forward(localConn net.Conn, config *ssh.ClientConfig) {
// Fine, the connections are up and ready :-) // Fine, the connections are up and ready :-)
log.Printf("The remote end-point %s is connected.\n", remoteAddrString) log.Printf("The remote end-point %s is connected.\n", remoteAddrString)
// To be able to close down both transfer threads, we create a channel:
quit := make(chan bool)
// Create the transfers to/from both sides (two new threads are created for this): // Create the transfers to/from both sides (two new threads are created for this):
go transfer(localConn, sshConn, `Local => Remote`) go transfer(localConn, sshConn, `Local => Remote`, quit)
go transfer(sshConn, localConn, `Remote => Local`) go transfer(sshConn, localConn, `Remote => Local`, quit)
// Wait and look if any of the two transfer theads are down:
isRunning := true
for isRunning {
select {
case <-quit:
log.Println(`At least one transfer was stopped.`)
isRunning = false
break
}
}
// Now, close all the channels and therefore, force the other / second thread to go down:
log.Println(`Close now all connections.`)
sshConn.Close()
localConn.Close()
sshClientConnection.Close()
return return
} }
} }

View File

@ -6,7 +6,7 @@ import (
) )
// The transfer function. // The transfer function.
func transfer(fromReader io.Reader, toWriter io.Writer, name string) { func transfer(fromReader io.Reader, toWriter io.Writer, name string, quit chan bool) {
log.Printf("%s transfer started.", name) log.Printf("%s transfer started.", name)
@ -22,4 +22,6 @@ func transfer(fromReader io.Reader, toWriter io.Writer, name string) {
} else { } else {
log.Printf("%s transfer closed.\n", name) log.Printf("%s transfer closed.\n", name)
} }
quit <- true
} }