https://github.com/nanmu42/go-http-server-gracefully-shutdown-done-right
Golang http.Server安全退出:容易被误用的Shutdown()方法
https://github.com/nanmu42/go-http-server-gracefully-shutdown-done-right
api-design golang graceful-shutdown
Last synced: 5 months ago
JSON representation
Golang http.Server安全退出:容易被误用的Shutdown()方法
- Host: GitHub
- URL: https://github.com/nanmu42/go-http-server-gracefully-shutdown-done-right
- Owner: nanmu42
- License: mit
- Created: 2021-09-15T05:43:51.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2021-09-22T16:22:09.000Z (about 4 years ago)
- Last Synced: 2025-03-31T20:44:30.650Z (7 months ago)
- Topics: api-design, golang, graceful-shutdown
- Language: Go
- Homepage: https://nanmu.me/zh-cn/posts/2021/go-http-server-shudown-done-right/
- Size: 7.81 KB
- Stars: 6
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Golang http.Server安全退出:容易被误用的Shutdown()方法
这个仓库展示了一种正确的(希望是)和一种错误的安全退出`http.Server`的方法。
详情可以参考我写的这篇文章:https://nanmu.me/zh-cn/posts/2021/go-http-server-shudown-done-right/
## 运行示例
如果你有现成的Go环境,可以直接安装:
```bash
# 一种正确的安全退出姿势
go install github.com/nanmu42/Go-HTTP-Server-Gracefully-Shutdown-Done-Right/better-way@latest
# 一种不正确的安全退出姿势
go install github.com/nanmu42/Go-HTTP-Server-Gracefully-Shutdown-Done-Right/wrong-way@latest
```然后就可以启动和访问了:
```bash
$ better-way
2021/09/23 00:16:03 listening on port 3100...
``````bash
$ wrong-way
2021/09/23 00:19:00 listening on port 3000...
```启动后可以用浏览器进行访问,在访问的同时退出服务试试看效果。如果手速不够快,可以`-h`看下选项。
Windows和Mac没有测试,由于Syscall的不同,可能需要改监听的信号名称才能编译。
## License
MIT