Fixed shutdown implementation

This commit is contained in:
Denis-Cosmin Nutiu 2017-12-15 21:25:02 +02:00
parent 9bd2625fad
commit 897fd1f80c
2 changed files with 45 additions and 33 deletions

View file

@ -101,9 +101,8 @@ func shutdownHandler() {
}() }()
wg.Wait() wg.Wait()
ShutdownFtpServer() go ShutdownFtpServer()
ShutdownUploadServer() go ShutdownUploadServer()
return return
} }
} }
@ -112,15 +111,15 @@ func shutdownHandler() {
func ShutdownUploadServer() { func ShutdownUploadServer() {
if uploadListener != nil { if uploadListener != nil {
uploadListener.Close() uploadListener.Close()
uploadShutdown <- struct{}{}
} }
uploadShutdown <- struct{}{}
} }
func ShutdownFtpServer() { func ShutdownFtpServer() {
if ftpListener != nil { if ftpListener != nil {
ftpListener.Close() ftpListener.Close()
ftpShutdown <- struct{}{}
} }
ftpShutdown <- struct{}{}
} }
func Init() { func Init() {
@ -169,7 +168,8 @@ func HandleConnection(client Client) {
log.Println(client.Connection().RemoteAddr(), "has disconnected.") log.Println(client.Connection().RemoteAddr(), "has disconnected.")
} }
func StartFtpServer() error { func StartFtpServer(wg *sync.WaitGroup) error {
defer wg.Done()
Addr := viper.GetString("address") Addr := viper.GetString("address")
Port := viper.GetInt("port") Port := viper.GetInt("port")
DirDepth := viper.GetInt("maxDirDepth") DirDepth := viper.GetInt("maxDirDepth")
@ -188,15 +188,13 @@ func StartFtpServer() error {
log.Println("Ftp server running on:", Addr, "port", Port) log.Println("Ftp server running on:", Addr, "port", Port)
for { for {
conn, err := ftpListener.Accept()
// Handle shutdown // Handle shutdown
select { select {
case <-ftpShutdown: case <-ftpShutdown:
return nil goto exit
default: default:
// move on
}
conn, err := ftpListener.Accept()
if err != nil { if err != nil {
log.Print(err) log.Print(err)
continue continue
@ -208,6 +206,9 @@ func StartFtpServer() error {
go HandleConnection(&client) go HandleConnection(&client)
} }
}
exit:
log.Println("Ftp server exited.")
return nil return nil
} }
@ -270,7 +271,9 @@ func HandleUpload(conn net.Conn) {
} }
// StartUploadServer starts the uploading server // StartUploadServer starts the uploading server
func StartUploadServer() error { func StartUploadServer(wg *sync.WaitGroup) error {
defer wg.Done()
var err error
if viper.GetBool("upload.enabled") == false { if viper.GetBool("upload.enabled") == false {
log.Println("Uploading not enabled. To enable modify the config file and restart the server") log.Println("Uploading not enabled. To enable modify the config file and restart the server")
return ErrUploadServerFailure return ErrUploadServerFailure
@ -281,7 +284,7 @@ func StartUploadServer() error {
uploadDirectory = viper.GetString("upload.directory") uploadDirectory = viper.GetString("upload.directory")
uploadTimeout = time.Duration(viper.GetInt("upload.timeout")) uploadTimeout = time.Duration(viper.GetInt("upload.timeout"))
uploadListener, err := net.Listen("tcp", fmt.Sprintf("%s:%d", addr, port)) uploadListener, err = net.Listen("tcp", fmt.Sprintf("%s:%d", addr, port))
if err != nil { if err != nil {
log.Println(err) log.Println(err)
return err return err
@ -301,15 +304,12 @@ func StartUploadServer() error {
log.Println("Upload server running on:", addr, "port", port) log.Println("Upload server running on:", addr, "port", port)
for { for {
conn, err := uploadListener.Accept()
// Handle shutdown // Handle shutdown
select { select {
case <-uploadShutdown: case <-uploadShutdown:
return nil goto exit
default: default:
// move on
}
conn, err := uploadListener.Accept()
if err != nil { if err != nil {
log.Print(err) log.Print(err)
continue continue
@ -317,6 +317,9 @@ func StartUploadServer() error {
go HandleUpload(conn) go HandleUpload(conn)
} }
}
exit:
log.Println("Upload server exited.")
return nil return nil
} }

View file

@ -3,6 +3,9 @@ package main
import ( import (
"flag" "flag"
"log"
"sync"
"github.com/metonimie/simpleFTP/server/server" "github.com/metonimie/simpleFTP/server/server"
) )
@ -10,8 +13,14 @@ func main() {
flag.StringVar(&server.ConfigPath, "config", ".", "Set the location of the config file.") flag.StringVar(&server.ConfigPath, "config", ".", "Set the location of the config file.")
flag.Parse() flag.Parse()
var wg = new(sync.WaitGroup)
server.Init() server.Init()
go server.StartUploadServer() wg.Add(2)
server.StartFtpServer() go server.StartUploadServer(wg)
go server.StartFtpServer(wg)
wg.Wait()
log.Println("bye")
} }