作为在kubernetes中扩展pod的一部分,我想确保在关闭之前优雅地为我的http连接提供服务.在那个程度上,我已经在go中实现了这个代码:
package main
import (
"fmt"
"io"
"net/http"
"os"
"os/signal"
"syscall"
"github.com/braintree/manners"
)
func main() {
shutdown := make(chan int)
//create a notification channel to shutdown
sigChan := make(chan os.Signal, 1)
//start the http server
http.HandleFunc("/", hello)
server := manners.NewWithServer(&http.Server{Addr: ":80", Handler: nil})
go func() {
server.ListenAndServe()
shutdown <- 1
}()
//register for interupt (Ctrl+C) and SIGTERM (docker)
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
go func() {
<-sigChan
fmt.Println("Shutting down...")
server.Close()
}()
<-shutdown
}
func hello(w http.ResponseWriter, r *http.Request) {
// time.Sleep(3000 * …Run Code Online (Sandbox Code Playgroud)