internal

package
v0.0.0-alpha.1 Latest Latest
Warning

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

Go to latest
Published: May 18, 2025 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

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(),
	syscall.SIGTERM, syscall.SIGINT, os.Interrupt)
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.

type UniqueStore

type UniqueStore[T comparable] struct {
	// contains filtered or unexported fields
}

UniqueStore stores a comparable values. All implemented methods are safe for concurrent call.

func (*UniqueStore[T]) Delete

func (m *UniqueStore[T]) Delete(value T)

Delete deletes the value from the store.

func (*UniqueStore[T]) Length

func (m *UniqueStore[T]) Length() int

Length returns the number of values.

func (*UniqueStore[T]) Set

func (m *UniqueStore[T]) Set(value T)

Set sets the given value to the store. The value overwrites the existing value if exists.

func (*UniqueStore[T]) Values

func (m *UniqueStore[T]) Values() iter.Seq[T]

Value returns an iterator for values.

Jump to

Keyboard shortcuts

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