Documentation
      ¶
    
    
  
    
  
    Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CloserStore ¶
type CloserStore[T ComparableCloser] struct { // contains filtered or unexported fields }
CloserStore stores ComparableCloser. All implemented methods are safe for concurrent call.
func (*CloserStore[T]) CloseAll ¶
func (m *CloserStore[T]) CloseAll() error
CloseAll call Close() method of all stored closers and delete from the store after closed.
func (*CloserStore[T]) Delete ¶
func (m *CloserStore[T]) Delete(closer T)
Delete deletes the closer from the store.
func (*CloserStore[T]) Length ¶
func (m *CloserStore[T]) Length() int
Length returns the number of closers.
func (*CloserStore[T]) Store ¶
func (m *CloserStore[T]) Store(closer T)
Store stores the given closer to the store. It replaces the existing closer if the same closer is already exist.
type ComparableCloser ¶
type ComparableCloser interface {
	comparable
	Close() error
}
    type ServerRunner ¶
type ServerRunner struct {
	// Server starts a server.
	// Serve must not be nil.
	Serve func() error
	// Shutdown gracefully shutdowns the server.
	// Shutdown must not be nil.
	// It will be called only when the context given to
	// the ServerRunner.Run is done.
	// Otherwise, Shutdown is not called even the Server exited.
	// Typically [http.Server.Shutdown] should be set.
	Shutdown func(context.Context) error
	// Close immediately closes a server.
	// Unlike Shutdown, it should not block.
	// Close, if non-nil, will be called only when shutdown timeout occurred.
	// Typically [net/http.Server.Close] should be set.
	// Note that the [net/http.Server.Shutdown] does not close remaining
	// connection after shutdown timeout occurred but this Runner try to.
	Close func() error
	// ShutdownTimeout is the timeout duration applied
	// for the Shutdown function.
	// For ShutdownTimeout<=0, 30 seconds is used.
	ShutdownTimeout time.Duration
}
    ServerRunner runs a server with an ability of graceful shutdown. See the following usage example.
Example:
svr := &http.Server{Addr: ":8080"}
r := &ServerRunner{
	Serve:           svr.ListenAndServe,
	Shutdown:        svr.Shutdown,
	Close:           svr.Close,
	ShutdownTimeout: 30 * time.Second,
}
sigCtx, cancel := signal.NotifyContext(context.Background(),
	os.Interrupt, syscall.SIGTERM)
defer cancel()
if err := r.Run(sigCtx); err != nil {
	panic(err)
}
func (*ServerRunner) Run ¶
func (r *ServerRunner) Run(sigCtx context.Context) error
Run runs a server. A server will be shutdown when the sigCtx is done. It returns non-nil error if r.Serve returns non-nil error. When a timeout occurred while shutting down, a context.DeadlineExceeded can be returned.