services

package module
v0.0.0-...-2ca447b Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2026 License: MIT Imports: 31 Imported by: 27 modules, 63 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

common-services

Collection of commonly used services at webtor.io

Probe

Generates standard liveness and readiness probe endpoints for kubernetes

Serve

Runs simultaneously multiple services in goroutines

GracefulServer

Drains an http.Server on SIGTERM: stops accepting, waits for in-flight requests up to --shutdown-timeout / WEB_SHUTDOWN_TIMEOUT (default 20s), then cuts what is still open and waits up to 2s more for the handlers of the cut connections to return. Closing only the listener lets the process exit mid-response, and the ingress answers 502. Set terminationGracePeriodSeconds to at least the preStop sleep + the timeout + 2s, plus whatever the rest of the shutdown needs.

Connections cut at the timeout are closed with a TCP reset (SO_LINGER 0), not a FIN. A FIN waits behind the unsent tail of the send buffer; the pod's network is gone before it leaves, and the client (torrent-http-proxy, ingress) notices only by keepalive, 150s with Go's defaults. Connections whose request finished during the drain close normally.

Close returns only after the handlers are done, cut ones included, except handlers of hijacked connections (WebSockets) and handlers still running 2s after the cut (logged as a warning). It must run before the dependencies the handlers use are closed, so call it right after cs.Serve (serve.Serve()) returns instead of leaving it to defer order:

app.Flags = cs.RegisterShutdownFlags(app.Flags)

// in the web service
gs := cs.NewGracefulServer(cs.ShutdownTimeout(c))
// Serve:  return gs.Serve(&http.Server{Handler: mux}, ln)
// Close:  gs.Close()

// in run()
err := serve.Serve()
web.Close() // drain before the deferred Redis/NATS/... closes

Without RegisterShutdownFlags the timeout is 0 and every in-flight request is cut at once; NewGracefulServer logs a warning at startup when that happens.

Example usage

package main

import (
	cs "github.com/webtor-io/common-services"
	log "github.com/sirupsen/logrus"
	"github.com/urfave/cli"
	s "github.com/webtor-io/torrent-http-proxy/services"
)

func configure(app *cli.App) {
	app.Flags = []cli.Flag{}

	s.RegisterWebFlags(app)
	cs.RegisterProbeFlags(app)

	app.Action = run
}

func run(c *cli.Context) error {
	// Setting ProbeService
	probe := cs.NewProbe(c)
	defer probe.Close()

	// Setting WebService
	web := s.NewWeb(c)
	defer web.Close()

	// Setting ServeService
	serve := cs.NewServe(probe, web)

	// And SERVE!
	err := serve.Serve()
	if err != nil {
		log.WithError(err).Error("Got serve error")
	}
	return err
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MakePGMigrationCMD

func MakePGMigrationCMD() cli.Command

func RegisterNATSFlags

func RegisterNATSFlags(f []cli.Flag) []cli.Flag

func RegisterPGFlags

func RegisterPGFlags(f []cli.Flag) []cli.Flag

func RegisterPprofFlags

func RegisterPprofFlags(f []cli.Flag) []cli.Flag

func RegisterProbeFlags

func RegisterProbeFlags(f []cli.Flag) []cli.Flag

RegisterProbeFlags registers cli flags for Probe

func RegisterPromFlags

func RegisterPromFlags(f []cli.Flag) []cli.Flag

func RegisterRedisClientFlags

func RegisterRedisClientFlags(f []cli.Flag) []cli.Flag

RegisterRedisClientFlags registers cli flags for RedisClient

func RegisterS3ClientFlags

func RegisterS3ClientFlags(f []cli.Flag) []cli.Flag

RegisterS3ClientFlags registers cli flags for S3 client

func RegisterShutdownFlags

func RegisterShutdownFlags(f []cli.Flag) []cli.Flag

RegisterShutdownFlags registers cli flags for GracefulServer

func ShutdownTimeout

func ShutdownTimeout(c *cli.Context) time.Duration

ShutdownTimeout returns the drain timeout for NewGracefulServer. It returns 0, which cuts in-flight requests at once, when RegisterShutdownFlags was not applied to this context's flags; NewGracefulServer warns about that.

Types

type GracefulServer

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

GracefulServer drains an http.Server on shutdown. Closing only the listener lets the process exit mid-response on SIGTERM, and the ingress answers 502 on every request a terminating pod is still serving.

Close must run before the dependencies the handlers use (Redis, NATS, DB clients) are closed. Defers run in reverse, so either defer Close after all of them or call it explicitly once cs.Serve returns. Close returns only after the handlers are done, including those of streams it cut at the timeout, so their deferred work does not run into closed dependencies. Two exceptions: handlers of hijacked connections (WebSockets), and cut handlers still running 2s after the cut, which Close logs and leaves behind.

The timeout is the one given to NewGracefulServer, usually ShutdownTimeout(c) (WEB_SHUTDOWN_TIMEOUT). It is meant for the service's main web server: the log lines say "web". One GracefulServer serves one http.Server; use NewGracefulServer.

func NewGracefulServer

func NewGracefulServer(timeout time.Duration) *GracefulServer

NewGracefulServer initializes GracefulServer that waits up to timeout for in-flight requests on Close

func (*GracefulServer) Close

func (s *GracefulServer) Close()

Close stops accepting, closes idle keep-alive connections and waits for in-flight requests up to the timeout, then cuts whatever is still open (long-lived streams) with a TCP reset and waits up to 2s for the cut handlers to return. Hijacked connections (WebSockets) are neither waited for nor cut, as with http.Server.Shutdown. It is safe to call more than once and before Serve; every call returns only after the drain is over.

func (*GracefulServer) Serve

func (s *GracefulServer) Serve(srv *http.Server, ln net.Listener) error

Serve serves srv on ln until Close. It returns nil after Close, including when Close already ran: then ln is closed and nothing is served. It chains srv.ConnState to track in-flight requests.

type NATS

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

func NewNATS

func NewNATS(c *cli.Context) *NATS

func (*NATS) Close

func (s *NATS) Close()

func (*NATS) Get

func (s *NATS) Get() *nats.Conn

type NetnsTCPCollector

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

NetnsTCPCollector exports the TCP counters of the network namespace the process runs in, read from /proc/self/net/{netstat,snmp} on every scrape. In a pod that is the pod's own namespace. node-exporter only reads the host's, and the kernel keeps these counters per namespace, so without this the drops and memory-pressure episodes of torrent-http-proxy and the seeders are invisible to Prometheus. On 2026-09-25 they were the cause of playback stalls: 16 MiB receive queues pushed the nodes into TCP memory pressure 59-83% of the time and the proxies dropped 50-180 segments/s.

Processes sharing a namespace (host network, a single-container deployment) report the same numbers.

func NewNetnsTCPCollector

func NewNetnsTCPCollector(dir string) (*NetnsTCPCollector, error)

NewNetnsTCPCollector reads the counters under dir (/proc/self/net in production). It fails when they cannot be read, so a platform without them is reported once at start-up instead of on every scrape.

func (*NetnsTCPCollector) Collect

func (c *NetnsTCPCollector) Collect(ch chan<- prometheus.Metric)

func (*NetnsTCPCollector) Describe

func (c *NetnsTCPCollector) Describe(ch chan<- *prometheus.Desc)

type PG

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

func NewPG

func NewPG(c *cli.Context) *PG

func (*PG) Close

func (s *PG) Close()

func (*PG) Get

func (s *PG) Get() *pg.DB

type PGMigration

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

func NewPGMigration

func NewPGMigration(db *PG) *PGMigration

func (*PGMigration) Run

func (s *PGMigration) Run(a ...string) error

type Pprof

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

func NewPprof

func NewPprof(c *cli.Context) *Pprof

func (*Pprof) Close

func (s *Pprof) Close()

func (*Pprof) Serve

func (s *Pprof) Serve() error

type Probe

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

Probe provides simple HTTP-service for Kubernetes liveness and readiness checking

func NewProbe

func NewProbe(c *cli.Context) *Probe

NewProbe initializes new Probe instance

func (*Probe) Close

func (s *Probe) Close()

Close closes Probe web service

func (*Probe) Serve

func (s *Probe) Serve() error

Serve serves Probe web service

type Prom

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

func NewProm

func NewProm(c *cli.Context) *Prom

func (*Prom) Close

func (s *Prom) Close()

func (*Prom) Serve

func (s *Prom) Serve() error

type RedisClient

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

RedisClient makes Redis Client from cli and environment variables Automatically hanldles Sentinel configuration

func NewRedisClient

func NewRedisClient(c *cli.Context) *RedisClient

NewRedisClient initializes RedisClient

func (*RedisClient) Close

func (s *RedisClient) Close()

Close closes RedisClient

func (*RedisClient) Get

Get gets redis.UniversalCleint

type S3Client

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

S3Client makes AWS SDK S3 Client from cli and environment variables

func NewS3Client

func NewS3Client(c *cli.Context, cl *http.Client) *S3Client

NewS3Client initializes S3Client

func (*S3Client) Get

func (s *S3Client) Get() *s3.S3

Get get AWS SDK S3 Client

type Servable

type Servable interface {
	Serve() error
}

Servable serves something

type Serve

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

Serve serves multible Servables at ones, handles errors and system signals

func NewServe

func NewServe(s ...Servable) *Serve

NewServe initializes Serve

func (*Serve) Serve

func (s *Serve) Serve() error

Serve serves multiple Servables

Jump to

Keyboard shortcuts

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