洞察探索如何利用兼容微信生态的小程序容器,实现跨平台开发,助力金融和车联网行业的数字化转型。
898
2022-10-13
KMS:优雅的关闭过程/应用程序
KMS
KMS(Killing Me Softly) is a library that aids in graceful shutdown of a process/application.
Why does this even exist? Aren't there other graceful shutdown librarys?
Sure there are other libraries, but they are focused on handling graceful shutdown a specific portion of code such as graceful shutdown of an http server, but this library allows you to glue together multiple unrelated(or related) services running within the same process/application.
Doesn't Go 1.8 handle graceful shutdown?
Yes and No, it does not yet handle Hijacked connections, like WebSockets, see:
https://github.com/golang/go/issues/4674#issuecomment-257549871https://github.com/golang/go/issues/17721#issuecomment-257572027
but this package does; furthermore this package isn't just for graceful http shutdown but graceful shutdown of anything and everything.
When Go 1.8 comes out I will transparently make changes to let the std lib handle idle connections as it has lower level access to them, but continue to handle Hijacked connections.
Installation
Use go get
go get -u github.com/go-playground/kms
Built In
There are a few built in graceful shutdown helpers using the kms package for:
TCPUnix SocketsHTTP(S) graceful shutdown.
Examples
see here for more
package mainimport ( "net/http" "time" "github.com/go-playground/kms" "github.com/go-playground/kms/kmsnet/kmshttp")func main() { // listen for shutdown signal(s) with timeout, non-blocking kms.ListenTimeout(false, time.Minute*3) http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("Home")) }) kmshttp.ListenAndServe(":3007", nil)}
Package Versioning
I'm jumping on the vendoring bandwagon, you should vendor this package as I will not be creating different version with gopkg.in like allot of my other libraries.
Why? because my time is spread pretty thin maintaining all of the libraries I have + LIFE, it is so freeing not to worry about it and will help me keep pouring out bigger and better things for you the community.
I am open versioning with gopkg.in should anyone request it, but this should be stable going forward.
Licenses
MIT License (MIT), Copyright (c) 2016 Dean Karn
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~