service

package module
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Jul 4, 2025 License: MIT Imports: 20 Imported by: 0

README

About service

This is a package I use for supporting service daemons. It can be optionally use systemd on GNU/Linux systems if the systemd build tag is applied, and it supports generic service daemon execution on non-SystemD systems, including other (BSD) generic posix platforms. The service daemon can also be used for starting system services from anyservice or srvany on Microsoft Windows. It provides interfaces for both accessing service features and logging.

This package also includes some data types and functionality I commonly use in developing go services. A task queue dispatcher is provided to enable running requests in a go service component in it's own thread context dispatching functions with arguments thru a lambda. Extended version of the golang time and duration types, with missing json marshallers and optimized for generic service use, are also provided.

Dependencies

Service is a Go module that requires Go 1.19 or later and can be installed from an import of gitlab.com/tychosoft/service. To use systemd, the build tag must be specified in the service application, and it will then build systemd support from github.com/coreos/go-systemd. Our own service applications automatically vendor these as external dependencies to not require remote access to use.

Participation

This project is offered as free (as in freedom) software for public use and has a public home page at https://gitlab.com/tychosoft/service which has an issue tracker where people can submit public bug reports, and a wiki for hosting project documentation. Because of the way importing works in Go you cannot easily relocate packages, so we directly host the repo publicly on gitlab.

Patches may be submitted and attached to an issue in the issue tracker. Support requests and other kinds of inquiries may also be sent privately thru email to [email protected]. Other details about participation may be found in CONTRIBUTING.md..

Testing

We do have go unit tests in this package. You can run them from go test ./... or by using the ``test'' make target. New functionality should include a new unit test if possible.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Clamp added in v1.2.1

func Clamp[T ~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~float32 | ~float64 | ~string](value, min, max T) T

func Debug

func Debug(level int, args ...interface{})

func Debugf added in v1.1.2

func Debugf(level int, format string, args ...interface{})

func Error

func Error(args ...interface{})

Log errors

func Errorf added in v1.1.2

func Errorf(format string, args ...interface{})

func Fail

func Fail(code int, args ...interface{})

Log failure and exit

func Failf added in v1.1.2

func Failf(code int, format string, args ...interface{})

func Info

func Info(args ...interface{})

Log info

func Infof added in v1.1.2

func Infof(format string, args ...interface{})

func IsDebug

func IsDebug() bool

func Live

func Live(args ...interface{}) error

func Logger

func Logger(level int, path string)

func LoggerRestart

func LoggerRestart()

Reset Logger such as from sighup

func Notice

func Notice(args ...interface{})

Log notices

func Noticef added in v1.1.2

func Noticef(format string, args ...interface{})

func Output

func Output(level int, args ...interface{})

Verbose output

func Outputf added in v1.1.2

func Outputf(level int, format string, args ...interface{})

func Range added in v1.2.1

func Range[T ~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~float32 | ~float64 | ~string](value, min, max T) bool

func Reload

func Reload(args ...interface{}) error

func Status

func Status(string) error

func Stop

func Stop(args ...interface{}) error

func Warn

func Warn(args ...interface{})

Log warnings

func Warnf added in v1.1.2

func Warnf(format string, args ...interface{})

func Watchdog

func Watchdog() error

Types

type Algo added in v1.3.0

type Algo int
const (
	SHA256 Algo = iota
	SHA512
	SHA3_256
	SHA3_512
	SHA2_256 = SHA256
	SHA2_512 = SHA512
)

type Duration added in v1.1.0

type Duration time.Duration

service.Duration allows json marshalling and prefers second intervals

func NewDuration added in v1.1.0

func NewDuration(seconds int) Duration

We convert from int to duration by seconds...

func (Duration) MarshalJSON added in v1.1.0

func (d Duration) MarshalJSON() ([]byte, error)

Marshall as duration string

func (Duration) Parse added in v1.1.0

func (d Duration) Parse(text string) (Duration, error)

Parse duration

func (Duration) String added in v1.1.0

func (d Duration) String() string

Duration as string

func (*Duration) UnmarshalJSON added in v1.1.0

func (d *Duration) UnmarshalJSON(b []byte) error

Unmarshal from float or string form

type Future added in v1.2.2

type Future[T any] struct {
	// contains filtered or unexported fields
}

func MakeFuture added in v1.2.2

func MakeFuture[T any]() *Future[T]

func (*Future[T]) Get added in v1.2.2

func (f *Future[T]) Get() T

func (*Future[T]) Set added in v1.2.2

func (f *Future[T]) Set(value T)

type Hash added in v1.3.0

type Hash struct {
	// contains filtered or unexported fields
}

func NewHash added in v1.3.0

func NewHash(a Algo) (*Hash, error)

func (*Hash) ToBits added in v1.3.0

func (h *Hash) ToBits(s string, bits uint8) (uint64, error)

func (*Hash) ToU32 added in v1.3.0

func (h *Hash) ToU32(s string) (uint32, error)

func (*Hash) ToU64 added in v1.3.0

func (h *Hash) ToU64(s string) (uint64, error)

type Ring64 added in v1.3.0

type Ring64 struct {
	// contains filtered or unexported fields
}

func NewRing64 added in v1.3.0

func NewRing64(hash *Hash, vnodes int) *Ring64

func (*Ring64) Count added in v1.3.0

func (r *Ring64) Count() int

func (*Ring64) Get added in v1.3.0

func (r *Ring64) Get(key string) (string, error)

func (*Ring64) Insert added in v1.3.0

func (r *Ring64) Insert(node string) error

func (*Ring64) Remove added in v1.3.0

func (r *Ring64) Remove(node string) error

type ShutdownStrategy added in v1.1.0

type ShutdownStrategy context.CancelFunc

type TaskPool added in v1.3.0

type TaskPool struct {
	Size int
	// contains filtered or unexported fields
}

TaskPool struct to manage tasks

func NewTaskPool added in v1.3.0

func NewTaskPool(base context.Context, poolSize int, bufSize int) *TaskPool

NewTaskQueue creates a new TaskPool with cancel and starts processing tasks

func (*TaskPool) Context added in v1.3.0

func (pool *TaskPool) Context() context.Context

Get context of task queue (default is background)

func (*TaskPool) Dispatch added in v1.3.0

func (pool *TaskPool) Dispatch(fn func(args ...interface{}), args ...interface{}) bool

Dispatch adds a task to the queue

func (*TaskPool) Done added in v1.3.0

func (pool *TaskPool) Done() <-chan struct{}

Get the done channel of the task scheduler

func (*TaskPool) Shutdown added in v1.3.0

func (pool *TaskPool) Shutdown()

func (*TaskPool) TryDispatch added in v1.3.0

func (pool *TaskPool) TryDispatch(fn func(args ...interface{}), args ...interface{}) bool

Try to dispatch a task to the queue

type TaskQueue added in v1.1.0

type TaskQueue struct {
	// contains filtered or unexported fields
}

TaskQueue struct to manage tasks

func NewTaskQueue added in v1.1.0

func NewTaskQueue(bufferSize int, timeoutStrategy TimeoutStrategy, shutdownStrategy context.CancelFunc) *TaskQueue

NewTaskQueue creates a new TaskQueue and starts processing tasks

func NewTaskQueueFromContext added in v1.2.2

func NewTaskQueueFromContext(base context.Context, bufferSize int, timeoutStrategy TimeoutStrategy) *TaskQueue

NewTaskQueue creates a new TaskQueue with cancel and starts processing tasks

func (*TaskQueue) Context added in v1.2.2

func (tq *TaskQueue) Context() context.Context

Get context of task queue (default is background)

func (*TaskQueue) Dispatch added in v1.1.0

func (tq *TaskQueue) Dispatch(fn func(args ...interface{}), args ...interface{}) bool

Dispatch adds a task to the queue

func (*TaskQueue) Done added in v1.2.2

func (tq *TaskQueue) Done() <-chan struct{}

Get the done channel of the task scheduler

func (*TaskQueue) Notify added in v1.2.0

func (tq *TaskQueue) Notify() bool

Notify is a dummy task to bump the channel

func (*TaskQueue) Reset added in v1.1.2

func (tq *TaskQueue) Reset(timer time.Duration) bool

reschedule timer

func (*TaskQueue) SetShutdown added in v1.1.0

func (tq *TaskQueue) SetShutdown(handler context.CancelFunc)

func (*TaskQueue) SetTimeout added in v1.1.0

func (tq *TaskQueue) SetTimeout(handler TimeoutStrategy)

func (*TaskQueue) Shutdown added in v1.1.0

func (tq *TaskQueue) Shutdown()

Shutdown stops the queue and waits for all tasks to complete

func (*TaskQueue) TryDispatch added in v1.2.1

func (tq *TaskQueue) TryDispatch(fn func(args ...interface{}), args ...interface{}) bool

Try to dispatch a task to the queue

type Time added in v1.1.0

type Time time.Time

service.Time reports utc and truncates to nearest second

func Now added in v1.1.0

func Now() Time

Get current time

func (Time) Local added in v1.1.0

func (t Time) Local() string

Local time format such as for logs

func (Time) MarshalJSON added in v1.1.0

func (t Time) MarshalJSON() ([]byte, error)

Marshall as time string (zulu time) nearest second

func (*Time) Parse added in v1.1.0

func (t *Time) Parse(text string) (Time, error)

Parse Time

func (Time) Seconds added in v1.1.0

func (t Time) Seconds() int64

Return timestamp as seconds

func (Time) String added in v1.1.0

func (t Time) String() string

Time as zulu time string

func (*Time) UnmarshalJSON added in v1.1.0

func (t *Time) UnmarshalJSON(b []byte) error

Unmarshal from float or string form

type TimeoutStrategy added in v1.1.0

type TimeoutStrategy func() time.Duration

TimeoutStrategy is a function type that returns a duration

type Timer added in v1.2.0

type Timer struct {
	// contains filtered or unexported fields
}

func NewTimer added in v1.2.0

func NewTimer() *Timer

NewTimer creates a new timer

func NewTimerWithCancel added in v1.2.2

func NewTimerWithCancel(base context.Context) (*Timer, func())

NewTimer creates a new timer with cancel

func (*Timer) At added in v1.2.0

func (t *Timer) At(expires time.Time, fn func(...interface{}), args ...interface{}) uint64

At adds a new task to the Timer

func (*Timer) Cancel added in v1.2.0

func (t *Timer) Cancel(id uint64) bool

func (*Timer) Context added in v1.2.2

func (t *Timer) Context() context.Context

func (*Timer) Done added in v1.2.2

func (t *Timer) Done() <-chan struct{}

func (*Timer) Find added in v1.2.0

func (t *Timer) Find(id uint64) time.Time

func (*Timer) Periodic added in v1.2.0

func (t *Timer) Periodic(period time.Duration, fn func(...interface{}), args ...interface{}) uint64

Periodic adds a periodic task to the Timer

func (*Timer) Stop added in v1.2.0

func (t *Timer) Stop()

Stop stops the TimerQueue worker

type TimerQueue added in v1.2.0

type TimerQueue []*timerItem

TaskQueue is a priority queue of tasks based on their execution time

func (TimerQueue) Len added in v1.2.0

func (tq TimerQueue) Len() int

func (TimerQueue) Less added in v1.2.0

func (tq TimerQueue) Less(i, j int) bool

func (*TimerQueue) Pop added in v1.2.0

func (tq *TimerQueue) Pop() interface{}

func (*TimerQueue) Push added in v1.2.0

func (tq *TimerQueue) Push(x interface{})

func (TimerQueue) Swap added in v1.2.0

func (tq TimerQueue) Swap(i, j int)

Jump to

Keyboard shortcuts

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