gsvc

package module
v0.3.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 17, 2025 License: Apache-2.0 Imports: 3 Imported by: 0

README

gsvc

Go GitHub go.mod Go version GitHub tag (latest SemVer) go.dev deps.dev

gsvc is yet another os service library for golang, supporting linux, darwin and windows.

Still WIP. Internal purpose for store-cli.

Features

  • implements the kernel of an OS Service
  • needs your business codes
  • works on main platforms: linux, darwin and windows

Usages


Contributions

Kindly welcome, please issue me first for keeping this repo smaller.

License

under Apache 2.0

Documentation

Index

Constants

View Source
const Version = version

Variables

This section is empty.

Functions

func Closers

func Closers() closerS

Closers returns the container includes all registered closable objects.

The simplest ways is using package level Close function:

func main() {
    defer is.Closers().Close()

    // others statements ...

    is.Closers().RegisterCloseFns(func(){ sth.Close() })

    // more statements...
}

func Signals

func Signals() signalS

Signals returns a signals-helper so that you can catch them, and raise them later.

Typically, its usage is `catcher := is.Signals().Catch(); ...`.

By default, catcher will listen on standard signals set: os.Interrupt, os.Kill, syscall.SIGTERM, syscall.SIGHUP, syscall.SIGINT.

But you can change them with:

is.Signals().Catch(os.Kill, os.Interrupt)

or:

is.Signals().Catch().WithSignals(os.Interrupt, os.Kill)

You should put your long-term codes inside `cb` of WaitFor(cb), and defer call to `closer()` in. The `closer()` is a param of `cb`.

For example:

package main

import (
  "context"
  "fmt"
  "os"
  "sync"

  "github.com/hedzr/env"
  "github.com/hedzr/go-socketlib/net"

  logz "logslog"
)

func main() {
  dbglog.SetLevel(dbglog.DebugLevel)

  server := net.NewServer(":7099")
  defer server.Close()

  ctx, cancel := context.WithCancel(context.Background())
  defer cancel()

  catcher := is.Signals().Catch()
  catcher.WithVerboseFn(func(msg string, args ...any) {
    dbglog.Debug(fmt.Sprintf("[verbose] %s", fmt.Sprintf(msg, args...)))
  }).
  WithOnSignalCaught(func(sig os.Signal, wg *sync.WaitGroup) {
    println()
    dbglog.Debug("signal caught", "sig", sig)
    if err := server.Shutdown(); err != nil {
    	dbglog.Error("server shutdown error", "err", err)
    }
    cancel()
  }).
  WaitFor(func(closer func()) {
    dbglog.Debug("entering looper's loop...")

    server.WithOnShutdown(func(err error) { closer() })
    err := server.StartAndServe(ctx)
    if err != nil {
      dbglog.Fatal("server serve failed", "err", err)
    }
  })
}

Types

This section is empty.

Directories

Path Synopsis
_examples
small command
pkg

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL