Skip to content

Commit

Permalink
Merge pull request #1 from yezzey-gp/systemd-notidications
Browse files Browse the repository at this point in the history
Add systemd notidications support
  • Loading branch information
reshke authored Nov 13, 2023
2 parents 3796f98 + cbb50e0 commit 02f124c
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
16 changes: 16 additions & 0 deletions config/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,20 @@ type Instance struct {

LogPath string `json:"log_path" toml:"log_path" yaml:"log_path"`
SocketPath string `json:"socket_path" toml:"socket_path" yaml:"socket_path"`

SystemdNotificationsDebug bool `json:"sd_notifications_debug" toml:"sd_notifications_debug" yaml:"sd_notifications_debug"`
systemdSocketPath string
}

func (i *Instance) ReadSystemdSocketPath() {
path := os.Getenv("NOTIFY_SOCKET")
if path != "" {
i.systemdSocketPath = path
}
}

func (i *Instance) GetSystemdSocketPath() string {
return i.systemdSocketPath
}

var cfgInstance Instance
Expand Down Expand Up @@ -66,6 +80,8 @@ func LoadInstanceConfig(cfgPath string) error {
return err
}

cfg.ReadSystemdSocketPath()

log.Println("Running config:", string(configBytes))
cfgInstance = cfg
return nil
Expand Down
15 changes: 15 additions & 0 deletions pkg/core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import (
"os"
"os/signal"
"syscall"
"time"

"github.com/yezzey-gp/yproxy/config"
"github.com/yezzey-gp/yproxy/pkg/crypt"
"github.com/yezzey-gp/yproxy/pkg/proc"
"github.com/yezzey-gp/yproxy/pkg/sdnotifier"
"github.com/yezzey-gp/yproxy/pkg/storage"
"github.com/yezzey-gp/yproxy/pkg/ylogger"
)
Expand Down Expand Up @@ -65,6 +67,19 @@ func (i *Instance) Run(instanceCnf *config.Instance) error {

cr := crypt.NewCrypto(&instanceCnf.CryptoCnf)

notifier, err := sdnotifier.NewNotifier(instanceCnf.GetSystemdSocketPath(), instanceCnf.SystemdNotificationsDebug)
if err != nil {
ylogger.Zero.Error().Err(err).Msg("failed to initialize systemd notifier")
}
notifier.Ready()

go func() {
for {
notifier.Notify()
time.Sleep(sdnotifier.Timeout)
}
}()

go func() {
<-ctx.Done()
os.Exit(0)
Expand Down
66 changes: 66 additions & 0 deletions pkg/sdnotifier/notifier.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package sdnotifier

import (
"fmt"
"net"
"time"

"github.com/yezzey-gp/yproxy/pkg/ylogger"
)

/*
#include <time.h>
static unsigned long long get_nsecs(void)
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (unsigned long long)ts.tv_sec * 1000000000UL + ts.tv_nsec;
}
*/
import "C"

const Timeout = 4 * time.Second

type Notifier struct {
sock net.Conn
debug bool
}

func NewNotifier(ns string, debug bool) (*Notifier, error) {
sock, err := net.Dial("unixgram", ns)
if err != nil {
sock = nil
if debug {
return &Notifier{sock: nil, debug: debug}, err
}
}

return &Notifier{sock: sock, debug: debug}, nil
}

func (n *Notifier) Reloading() error {
return n.send([]byte(fmt.Sprintf("RELOADING=1\nMONOTONIC_USEC=%d\n", uint64(C.get_nsecs()))))
}

func (n *Notifier) Ready() error {
return n.send([]byte("READY=1\n"))
}

func (n *Notifier) Notify() error {
return n.send([]byte("WATCHDOG=1\n"))
}

func (n *Notifier) send(msg []byte) error {
ylogger.Zero.Debug().Msg(fmt.Sprintf("sending systemd notification: %s", msg))

if n.sock == nil {
return nil
}

_, err := n.sock.Write(msg)

if n.debug {
return err
}
return nil
}

0 comments on commit 02f124c

Please sign in to comment.