Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/cloudflare/tableflip
Graceful process restarts in Go
https://github.com/cloudflare/tableflip
go golang graceful-restarts
Last synced: 3 days ago
JSON representation
Graceful process restarts in Go
- Host: GitHub
- URL: https://github.com/cloudflare/tableflip
- Owner: cloudflare
- License: bsd-3-clause
- Created: 2018-09-14T16:37:37.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2024-09-25T22:41:58.000Z (3 months ago)
- Last Synced: 2024-11-25T13:06:54.171Z (17 days ago)
- Topics: go, golang, graceful-restarts
- Language: Go
- Homepage:
- Size: 81.1 KB
- Stars: 2,919
- Watchers: 39
- Forks: 148
- Open Issues: 13
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- go-awesome - tableflip - web service upgrade (Open source library / Elegant Upgrade)
- awesome-list - tableflip
- awesome-golang-repositories - tableflip
- my-awesome - cloudflare/tableflip - restarts pushed_at:2024-09 star:2.9k fork:0.1k Graceful process restarts in Go (Go)
README
# Graceful process restarts in Go
[![](https://godoc.org/github.com/cloudflare/tableflip?status.svg)](https://godoc.org/github.com/cloudflare/tableflip)
It is sometimes useful to update the running code and / or configuration of a
network service, without disrupting existing connections. Usually, this is
achieved by starting a new process, somehow transferring clients to it and
then exiting the old process.There are [many ways to implement graceful upgrades](https://blog.cloudflare.com/graceful-upgrades-in-go/).
They vary wildly in the trade-offs they make, and how much control they afford the user. This library
has the following goals:* No old code keeps running after a successful upgrade
* The new process has a grace period for performing initialisation
* Crashing during initialisation is OK
* Only a single upgrade is ever run in parallel**`tableflip` works on Linux and macOS.**
## Using the library
```Go
upg, _ := tableflip.New(tableflip.Options{})
defer upg.Stop()go func() {
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGHUP)
for range sig {
upg.Upgrade()
}
}()// Listen must be called before Ready
ln, _ := upg.Listen("tcp", "localhost:8080")
defer ln.Close()go http.Serve(ln, nil)
if err := upg.Ready(); err != nil {
panic(err)
}<-upg.Exit()
```Please see the more elaborate [graceful shutdown with net/http](http_example_test.go) example.
## Integration with `systemd`
```text
[Unit]
Description=Service using tableflip[Service]
ExecStart=/path/to/binary -some-flag /path/to/pid-file
ExecReload=/bin/kill -HUP $MAINPID
PIDFile=/path/to/pid-file
```See the [documentation](https://godoc.org/github.com/cloudflare/tableflip) as well.
The logs of a process using `tableflip` may go missing due to a [bug in journald](https://github.com/systemd/systemd/issues/13708),
which has been fixed by systemd v244 release. If you are running an older version
of systemd, you can work around this by logging directly to journald, for example
by using [go-systemd/journal](https://godoc.org/github.com/coreos/go-systemd/journal)
and looking for the [$JOURNAL_STREAM](https://www.freedesktop.org/software/systemd/man/systemd.exec.html#$JOURNAL_STREAM)
environment variable.