MCPcopy
hub / github.com/cloudflare/tableflip

github.com/cloudflare/tableflip @v1.2.3 sqlite

repository ↗ · DeepWiki ↗ · release v1.2.3 ↗
146 symbols 500 edges 23 files 49 documented · 34%
README

Graceful process restarts in Go

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. 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

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 example.

Integration with systemd

[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 as well.

The logs of a process using tableflip may go missing due to a bug in journald, 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 and looking for the $JOURNAL_STREAM environment variable.

Extension points exported contracts — how you extend this code

Listener (Interface)
Listener can be shared between processes.
fds.go
PacketConn (Interface)
PacketConn can be shared between processes.
fds.go
Conn (Interface)
Conn can be shared between processes.
fds.go

Core symbols most depended-on inside this repo

newFds
called by 16
fds.go
Stop
called by 16
testing/http_example_test.go
Listen
called by 10
testing/http_example_test.go
Upgrade
called by 9
testing/http_example_test.go
Ready
called by 9
testing/http_example_test.go
Exit
called by 9
testing/http_example_test.go
startChild
called by 8
child.go
copy
called by 7
fds.go

Shape

Method 69
Function 56
Struct 14
Interface 6
TypeAlias 1

Languages

Go100%

Modules by API surface

fds.go38 symbols
upgrader_test.go19 symbols
upgrader.go15 symbols
process_test.go11 symbols
testing/upgrader.go9 symbols
fds_test.go9 symbols
testing/fds.go8 symbols
testing/http_example_test.go7 symbols
process.go7 symbols
child.go7 symbols
child_test.go5 symbols
parent.go3 symbols

Used by 2 indexed graphs manifest dependencies, hub-wide

Dependencies from manifests, versioned

golang.org/x/sysv0.0.0-2021011921285 · 1×

For agents

$ claude mcp add tableflip \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact