Documentation ¶
Overview ¶
Package graceful provides graceful start, graceful retry, and graceful stop! It can be used for:
- Gracefully starting servers (http, grpc, etc.) and clients (external services, databases, message queues, etc.).
- Gracefully retrying lost connections to external services, databases, message queues, etc.
- Gracefully stopping servers (http, grpc, etc.) and clients (external services, databases, message queues, etc.).
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func RegisterClient ¶ added in v0.1.1
func RegisterClient(c ...Client)
RegisterClient registers new clients (external services, databases, message queues, etc.).
func RegisterServer ¶ added in v0.1.1
func RegisterServer(s ...Server)
RegisterServer registers new servers (http, grpc, etc.).
func SetGracePeriod ¶ added in v0.1.1
SetGracePeriod sets the timeout for gracefully stopping servers and clients. If the timeout reaches, the process will be terminated ungracefully. The default grace period is 30 seconds.
func SetMaxRetry ¶ added in v0.1.1
func SetMaxRetry(n int)
SetMaxRetry sets the maximum number of retries. When a client connection is lost, the library retries to establish the connection. The default maximum retry is 5.
func StartAndWait ¶ added in v0.1.1
func StartAndWait() int
StartAndWait behaves as follows:
- It tries to connect all clients each in a new goroutine. - If a client fails to connect, it will be automatically retried for a limited number of times with exponential backoff. - If at least one client fails to connect (after retries), a graceful termination will be initiated.
- Once all clients are connected successfully, all servers start listening each in a new goroutine. - If any server errors, a graceful termination will be initiated.
- Then, this method blocks the current goroutine until one of the following conditions happen: - If any of SIGHUP, SIGINT, SIGQUIT, SIGTERM signals is sent, a graceful termination will be initiated. - If any of the above signals is sent for the second time before the graceful termination is completed, the process will exit immediately with an error code.
Types ¶
type Client ¶
Client is the generic interface for a client (external service, database, message queue, etc.).