|
| 1 | +package core |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + "os" |
| 8 | + "os/signal" |
| 9 | + "syscall" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/gin-gonic/gin" |
| 13 | + "go.uber.org/zap" |
| 14 | +) |
| 15 | + |
| 16 | +type server interface { |
| 17 | + ListenAndServe() error |
| 18 | + Shutdown(context.Context) error |
| 19 | +} |
| 20 | + |
| 21 | +// initServer 启动服务并实现优雅关闭 |
| 22 | +func initServer(address string, router *gin.Engine, readTimeout, writeTimeout time.Duration) { |
| 23 | + // 创建服务 |
| 24 | + srv := &http.Server{ |
| 25 | + Addr: address, |
| 26 | + Handler: router, |
| 27 | + ReadTimeout: readTimeout, |
| 28 | + WriteTimeout: writeTimeout, |
| 29 | + MaxHeaderBytes: 1 << 20, |
| 30 | + } |
| 31 | + |
| 32 | + // 在goroutine中启动服务 |
| 33 | + go func() { |
| 34 | + if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed { |
| 35 | + fmt.Printf("listen: %s\n", err) |
| 36 | + zap.L().Error("server启动失败", zap.Error(err)) |
| 37 | + os.Exit(1) |
| 38 | + } |
| 39 | + }() |
| 40 | + |
| 41 | + // 等待中断信号以优雅地关闭服务器 |
| 42 | + quit := make(chan os.Signal, 1) |
| 43 | + // kill (无参数) 默认发送 syscall.SIGTERM |
| 44 | + // kill -2 发送 syscall.SIGINT |
| 45 | + // kill -9 发送 syscall.SIGKILL,但是无法被捕获,所以不需要添加 |
| 46 | + signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM) |
| 47 | + <-quit |
| 48 | + zap.L().Info("关闭服务器...") |
| 49 | + |
| 50 | + // 设置5秒的超时时间 |
| 51 | + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 52 | + defer cancel() |
| 53 | + |
| 54 | + if err := srv.Shutdown(ctx); err != nil { |
| 55 | + zap.L().Fatal("服务器关闭异常", zap.Error(err)) |
| 56 | + } |
| 57 | + |
| 58 | + zap.L().Info("服务器已优雅关闭") |
| 59 | +} |
0 commit comments