Documentation ¶
Overview ¶
Package service provides simple service framework on top of Module interface. Ready made modules can be found under: github.com/elisasre/go-common/service/module.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ErrPanic = errors.New("panic captured")
Functions ¶
func Run ¶
Run runs svc using following control flow:
- Exec Init() for each module in order. If error is occurred Run returns immediately.
- Exec Run() for each module in own goroutine.
- Wait for any Run() function to return. When that happens move to Stop sequence.
- Exec Stop() for modules in reverse order.
- Wait for all Run() and Stop() calls to return.
- Return all errors or nil
Possible panics inside modules are captured to allow graceful shutdown of other modules. Captured panics are converted into errors and ErrPanic is returned.
Example ¶
package main import ( "fmt" "net/http" "os" "syscall" "time" "github.com/elisasre/go-common/service" "github.com/elisasre/go-common/service/module/httpserver" "github.com/elisasre/go-common/service/module/httpserver/pprof" "github.com/elisasre/go-common/service/module/siglistener" ) func main() { // Send SIGINT after 5 second. go func() { time.Sleep(time.Second * 5) syscall.Kill(syscall.Getpid(), syscall.SIGINT) //nolint: errcheck }() err := service.Run(service.Modules{ siglistener.New(os.Interrupt), httpserver.New( httpserver.WithAddr(":6062"), pprof.WithProfiling(), ), httpserver.New( httpserver.WithServer(&http.Server{ReadHeaderTimeout: time.Second}), httpserver.WithAddr(":6060"), httpserver.WithHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, "Hello") })), ), }) if err != nil { fmt.Println(err) return } fmt.Println("Service exited successfully") }
Output: Service exited successfully
func RunAndExit ¶
func RunAndExit(svc Service)
Types ¶
Directories ¶
Path | Synopsis |
---|---|
module
|
|
httpserver
Package httpserver provides http server as module.
|
Package httpserver provides http server as module. |
httpserver/pprof
Package pprof provides pprof handler options for httpserver module.
|
Package pprof provides pprof handler options for httpserver module. |
httpserver/prom
Package prom provides prometheus metrics handler options for httpserver module.
|
Package prom provides prometheus metrics handler options for httpserver module. |
siglistener
Package siglistener provides signal listening as a module.
|
Package siglistener provides signal listening as a module. |
ticker
Package ticker provides ticker functionality as a module.
|
Package ticker provides ticker functionality as a module. |
Click to show internal directories.
Click to hide internal directories.