今天做了一个go语音的http server优雅退出的测试实验
package main
import (
"context"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
)
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
t:=time.NewTimer(6 * time.Second)
L: for {
time.Sleep(1*time.Second)
select {
case <- t.C:
fmt.Println("\ntime out===============================")
break L
default:
fmt.Println("i am doing job")
}
}
fmt.Fprintf(w, "Hello World, %v\n", time.Now())
})
s := &http.Server{
Addr: ":8080",
Handler: http.DefaultServeMux,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
go fun