Added gracefully shutdown for clients
Added the gracefully shutdown for all client connections to release all these resources.
This commit is contained in:
		
							parent
							
								
									6ae3b46fc1
								
							
						
					
					
						commit
						316b4a6c54
					
				
							
								
								
									
										25
									
								
								Forward.go
									
									
									
									
									
								
							
							
						
						
									
										25
									
								
								Forward.go
									
									
									
									
									
								
							@ -68,9 +68,30 @@ func forward(localConn net.Conn, config *ssh.ClientConfig) {
 | 
			
		||||
			// Fine, the connections are up and ready :-)
 | 
			
		||||
			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):
 | 
			
		||||
			go transfer(localConn, sshConn, `Local => Remote`)
 | 
			
		||||
			go transfer(sshConn, localConn, `Remote => Local`)
 | 
			
		||||
			go transfer(localConn, sshConn, `Local => Remote`, quit)
 | 
			
		||||
			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
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
@ -6,7 +6,7 @@ import (
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// 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)
 | 
			
		||||
 | 
			
		||||
@ -22,4 +22,6 @@ func transfer(fromReader io.Reader, toWriter io.Writer, name string) {
 | 
			
		||||
	} else {
 | 
			
		||||
		log.Printf("%s transfer closed.\n", name)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	quit <- true
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user