Gracefully shutdown in HTTP and gRPC apps

This commit is contained in:
Lam Tran
2022-01-06 22:57:05 +07:00
parent 6a032cb080
commit f989bd86b5
6 changed files with 39 additions and 11 deletions

8
.idea/.gitignore generated vendored Normal file
View File

@@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

8
.idea/modules.xml generated Normal file
View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/netmaker.iml" filepath="$PROJECT_DIR$/.idea/netmaker.iml" />
</modules>
</component>
</project>

9
.idea/netmaker.iml generated Normal file
View File

@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="Go" enabled="true" />
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

6
.idea/vcs.xml generated Normal file
View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

View File

@@ -52,20 +52,19 @@ func HandleRESTRequests(wg *sync.WaitGroup) {
} }
}() }()
logger.Log(0, "REST Server successfully started on port ", port, " (REST)") logger.Log(0, "REST Server successfully started on port ", port, " (REST)")
c := make(chan os.Signal)
// Relay os.Interrupt to our channel (os.Interrupt = CTRL+C) // Relay os.Interrupt to our channel (os.Interrupt = CTRL+C)
// Ignore other incoming signals // Ignore other incoming signals
signal.Notify(c, os.Interrupt) ctx, stop := signal.NotifyContext(context.TODO(), os.Interrupt)
defer stop()
// Block main routine until a signal is received // Block main routine until a signal is received
// As long as user doesn't press CTRL+C a message is not passed and our main routine keeps running // As long as user doesn't press CTRL+C a message is not passed and our main routine keeps running
<-c <-ctx.Done()
// After receiving CTRL+C Properly stop the server // After receiving CTRL+C Properly stop the server
logger.Log(0, "Stopping the REST server...") logger.Log(0, "Stopping the REST server...")
srv.Shutdown(context.TODO()) srv.Shutdown(context.TODO())
logger.Log(0, "REST Server closed.") logger.Log(0, "REST Server closed.")
logger.DumpFile(fmt.Sprintf("data/netmaker.log.%s", time.Now().Format(logger.TimeFormatDay))) logger.DumpFile(fmt.Sprintf("data/netmaker.log.%s", time.Now().Format(logger.TimeFormatDay)))
} }

12
main.go
View File

@@ -1,6 +1,7 @@
package main package main
import ( import (
"context"
"fmt" "fmt"
"net" "net"
"os" "os"
@@ -157,21 +158,18 @@ func runGRPC(wg *sync.WaitGroup) {
}() }()
logger.Log(0, "Agent Server successfully started on port ", grpcport, "(gRPC)") logger.Log(0, "Agent Server successfully started on port ", grpcport, "(gRPC)")
// Right way to stop the server using a SHUTDOWN HOOK
// Create a channel to receive OS signals
c := make(chan os.Signal, 1)
// Relay os.Interrupt to our channel (os.Interrupt = CTRL+C) // Relay os.Interrupt to our channel (os.Interrupt = CTRL+C)
// Ignore other incoming signals // Ignore other incoming signals
signal.Notify(c, os.Interrupt) ctx, stop := signal.NotifyContext(context.TODO(), os.Interrupt)
defer stop()
// Block main routine until a signal is received // Block main routine until a signal is received
// As long as user doesn't press CTRL+C a message is not passed and our main routine keeps running // As long as user doesn't press CTRL+C a message is not passed and our main routine keeps running
<-c <-ctx.Done()
// After receiving CTRL+C Properly stop the server // After receiving CTRL+C Properly stop the server
logger.Log(0, "Stopping the Agent server...") logger.Log(0, "Stopping the Agent server...")
s.Stop() s.GracefulStop()
listener.Close() listener.Close()
logger.Log(0, "Agent server closed..") logger.Log(0, "Agent server closed..")
logger.Log(0, "Closed DB connection.") logger.Log(0, "Closed DB connection.")