service

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2026 License: MIT Imports: 17 Imported by: 1

README

ACSL-GO Service Framework

See API reference Here.

Introduction

ACSL-GO Service Framework is a tiny framework for services. It focuses on the following aspects:

  • Sub-service scheduling, in a common scenario, we may need to run an HTTP server and schedule some timer tasks. ACSL-GO Service Framework provides a simple way to do this.
  • Graceful-shutdown HTTP server, a simple implementation of graceful-shutdown HTTP server as a sub-service (task).
  • Timer task, a simple implementation of timer task as a sub-service (task).

Usage

Integration
import (
    "github.com/acsl-go/service"
)
Create an HTTP server
var httpHandler http.Handler
httpServer := service.HttpServer("http-server", ":8080", httpHandler)

Or, if you work with Gin:

router := gin.New()
httpServer := service.HttpServer("http-server", ":8080", router)
Create a timer task
timerTask := service.Timer(5 * time.Second, func() {
    // do something
})
Run the HTTP server and timer task
service.Run(httpServer, timerTask)
Sample code
package main

import (
    "fmt"
    "time"
	"github.com/acsl-go/service"
	"github.com/gin-gonic/gin"
)

func apiInit(router *gin.Engine) {
    router.GET("/ping", func(c *gin.Context) {
        c.JSON(200, gin.H{
            "message": "pong",
        })
    })
}

func ginTask(name, addr string, initializer func(*gin.Engine)) service.ServiceTask {
	router := gin.New()
	initializer(router)
	return service.HttpServer(name, addr, router)
}

func main() {
	service.Run(
		ginTask("API", ":8080", apiInit),
		service.Timer(5*time.Second, func() {
            fmt.Println("timer task")
        }),
	)
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidTlsKeyPair  = errors.New("invalid TLS key pair")
	ErrPortBindingFailed  = errors.New("failed to bind to the specified port")
	ErrInvalidPrivateKey  = errors.New("invalid private key")
	ErrInvalidCertificate = errors.New("invalid certificate")
	ErrIOFailure          = errors.New("I/O failure")
)
View Source
var (
	ErrPanic      = errors.New("service: panic occurred")
	ErrNilAdapter = errors.New("service: listen returned nil adapter without error")
	ErrRestart    = errors.New("service: restart requested")
	ErrStop       = errors.New("service: stop requested")
)

Functions

func Run

func Run(tasks ...ServiceTaskFunc)

deprecated, use TaskManager instead

func Start added in v0.0.3

func Start()

deprecated, use TaskManager instead

Types

type DefaultService added in v1.0.0

type DefaultService struct{}

func (*DefaultService) OnListenFailed added in v1.0.0

func (*DefaultService) OnListenFailed(err error) time.Duration

func (*DefaultService) OnStopped added in v1.0.0

func (*DefaultService) OnStopped()

func (*DefaultService) OnStopping added in v1.0.0

func (*DefaultService) OnStopping()

type IAdapter added in v1.0.0

type IAdapter interface {
	Close() error
}

type IGRPCService added in v1.0.0

type IGRPCService interface {
	GetNetConfig() *NetServiceConfig
	OnRegisterGRPCServer(*grpc.Server)
	OnListenFailed(error) time.Duration
	OnStopping()
	OnStopped()
}

type IService added in v1.0.0

type IService interface {
	Listen(ctx context.Context) (IAdapter, error)
	Serve(ctx context.Context, conn IAdapter)
	OnListenFailed(error) time.Duration
	OnStopping()
	OnStopped()
}

type ITCPService added in v1.0.0

type ITCPService interface {
	GetAddress() string
	OnNewConnection(context.Context, net.Conn) bool // DO NOT block, start a TCPConnectionTask or handle the connection in a new goroutine if needed, return false to close the connection immediately
	OnListenFailed(error) time.Duration
	OnStopping()
	OnStopped()
}

type NetServiceConfig added in v1.0.0

type NetServiceConfig struct {
	Address string     `json:"address" yaml:"address"` // Listening address, e.g., ":8080"
	TLS     *TLSConfig `json:"tls" yaml:"tls"`         // [Optional] TLS configuration, if provided, the service will use TLS for secure communication
}

func (*NetServiceConfig) ListenTCP added in v1.0.0

func (cfg *NetServiceConfig) ListenTCP() (IAdapter, error)

type ServiceTask

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

func NewGRPCService added in v1.0.0

func NewGRPCService(impl IGRPCService) *ServiceTask

func NewServiceTask added in v1.0.0

func NewServiceTask(delegate IService) *ServiceTask

func NewTCPServiceTask added in v1.0.0

func NewTCPServiceTask(delegate ITCPService) *ServiceTask

func (*ServiceTask) Run added in v1.0.0

func (t *ServiceTask) Run(mgr *TaskManager) *ServiceTask

func (*ServiceTask) Stop added in v1.0.0

func (t *ServiceTask) Stop()

type ServiceTaskFunc added in v1.0.0

type ServiceTaskFunc func(context.Context)

deprecated, use TaskManager instead

func HttpServer

func HttpServer(name, addr string, initRouter func(context.Context) http.Handler) ServiceTaskFunc

deprecated

func HttpsServer added in v0.0.4

func HttpsServer(name, addr, certFile, keyFile string, initRouter func(context.Context) http.Handler) ServiceTaskFunc

deprecated

func Timer

func Timer(interval time.Duration, task func()) ServiceTaskFunc

deprecated

type TLSConfig added in v1.0.0

type TLSConfig struct {
	Cert           string   `json:"cert" yaml:"cert"`                         // [Optional] SSL Certificate content or file path
	Key            string   `json:"key" yaml:"key"`                           // [Optional] SSL Key content or file path
	CAs            []string `json:"ca" yaml:"ca"`                             // [Optional] CA Certificates content or file path for verifying client certificates, only used when SSL is enabled
	ClientAuthType string   `json:"client_auth_type" yaml:"client_auth_type"` // [Optional] Client authentication type, can be "none", "optional", "required", "must"
}

type TaskManager added in v1.0.0

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

func NewTaskManager added in v1.0.0

func NewTaskManager() *TaskManager

func (*TaskManager) Run added in v1.0.0

func (tm *TaskManager) Run(task *ServiceTask) *ServiceTask

func (*TaskManager) RunMultiple added in v1.0.0

func (tm *TaskManager) RunMultiple(tasks ...*ServiceTask)

func (*TaskManager) Stop added in v1.0.0

func (tm *TaskManager) Stop()

func (*TaskManager) Wait added in v1.0.0

func (tm *TaskManager) Wait()

Jump to

Keyboard shortcuts

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