未加星标

Integration of a Go service with systemd

字体大小 | |
[系统(linux) 所属分类 系统(linux) | 发布者 店小二05 | 时间 2017 | 作者 红领巾 ] 0人收藏点击收藏

Unlike other programming languages, Go’s runtime doesn’t provide a way to reliably daemonize a service . A system daemon has to supply this functionality. Most distributions ship systemd which would fit the bill. A correct integration with systemd is quite straightforward. There are two interesting aspects: readiness & liveness .

As an example, we will daemonize this service whose goal is to answer requests with nifty 404errors:

package main import ( "log" "net" "net/http" ) func main() { l, err := net.Listen("tcp", ":8081") if err != nil { log.Panicf("cannot listen: %s", err) } http.Serve(l, nil) }

You can build it with go build 404.go .

Here is the service file, 404.service :

[Unit] Description=404 micro-service [Service] Type=notify ExecStart=/usr/bin/404 WatchdogSec=30s Restart=on-failure [Install] WantedBy=multi-user.target

The classic way for an Unix daemon to signal its readiness is to daemonize . Technically, this is done by calling fork(2) twice (which also serves other intents). This is a very common task and the BSD systems, as well as some other C libraries, supply a daemon(3) function for this purpose. Services are expected to daemonize only when they are ready (after reading configuration files and setting up a listening socket, for example). Then, a system can reliably initialize its services with a simple linearscript:

syslogd unbound ntpd -s

Each daemon can rely on the previous one being ready to do its work. The sequence of actions is thefollowing:

syslogd reads its configuration, activates /dev/log , daemonizes . unbound reads its configuration, listens on 127.0.0.1:53 , daemonizes . ntpd reads its configuration, connects to NTP peers, waits for clock to be synchronized, daemonizes .

With systemd , we would use Type=fork in the service file. However, Go’s runtime does not support that. Instead, we use Type=notify . In this case, systemd expects the daemon to signal its readiness with a message to an Unix socket. go-systemd package handles the details forus:

package main import ( "log" "net" "net/http" "github.com/coreos/go-systemd/daemon" ) func main() { l, err := net.Listen("tcp", ":8081") if err != nil { log.Panicf("cannot listen: %s", err) } daemon.SdNotify(false, "READY=1") // http.Serve(l, nil) // }

It’s important to place the notification after net.Listen() (in ): if the notification was sent earlier, a client would get “connection refused” when trying to use the service. When a daemon listens to a socket, connections are queued by the kernel until the daemon is able to accept them (in).

If the service is not run through systemd , the added line is ano-op.

Another interesting feature of systemd is to watch the service and restart it if it happens to crash (thanks to the Restart=on-failure directive). It’s also possible to use a watchdog: the service sends watchdog keep-alives at regular interval . If it fails to do so, systemd will restartit.

We could insert the following code just before http.Serve() call:

go func() { interval, err := daemon.SdWatchdogEnabled(false) if err != nil || interval == 0 { return } for { daemon.SdNotify(false, "WATCHDOG=1") time.Sleep(interval / 3) } }()

However, this doesn’t add much value: the goroutine is unrelated to the core business of the service. If for some reason, the HTTP part gets stuck, the goroutine will happily continue to send keep-alives to systemd .

In our example, we can just do a HTTP query before sending the keep-alive. The internal loop can be replaced with thiscode:

for { _, err := http.Get("http://127.0.0.1:8081") // if err == nil { daemon.SdNotify(false, "WATCHDOG=1") } time.Sleep(interval / 3) }

In , we connect to the service to check if it’s still working. If we get some kind of answer, we send a watchdog keep-alive. If the service is unavailable or if http.Get() gets stuck, systemd will trigger arestart.

There is no universal recipe. However, checks can be split into twogroups:

Before sending a keep-alive, you execute an active check on the components of your service. The keep-alive is sent only if all checks are successful. The checks can be internal (like in the above example) or external (for example, check with a query to thedatabase).

Each component reports its status, telling if it’s alive or not. Before sending a keep-alive, you check the reported status of all components ( passive check ). If some components are late or reported fatal errors, don’t send thekeep-alive.

If possible, recovery from errors (for example, with a backoff retry) and self-healing (for example, by reestablishing a network connection) is always better, but the watchdog is a good tool to handle the worst cases and avoid too complex recoverylogic.

For example, if a component doesn’t know how to recover from an exceptional condition, instead of using panic() , it could signal its situation before dying. Another dedicated component could try to resolve the situation by restarting the faulty component. If it fails to reach an healthy state in time, the watchdog timer will trigger and the whole service will berestarted.

本文系统(linux)相关术语:linux系统 鸟哥的linux私房菜 linux命令大全 linux操作系统

分页:12
转载请注明
本文标题:Integration of a Go service with systemd
本站链接:http://www.codesec.net/view/531867.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 系统(linux) | 评论(0) | 阅读(19)