Documentation
¶
Overview ¶
Package interfaces defines small structural interface contracts used throughout keel to describe the lifecycle and identity of services and resources: closing, shutting down, stopping, unsubscribing, pinging, naming, and self-documenting.
Each lifecycle family (Closer, Shutdowner, Stopper, Unsubscriber, Pinger) is declared along four shape axes covering the common Go patterns:
- the unadorned method (no return, no context),
- the variant that returns an error,
- the variant that accepts a context.Context,
- the variant that accepts a context.Context and returns an error.
For every named interface X declared here there is a paired helper IsX(v any) (X, bool) that performs the type assertion. All such helpers are thin wrappers around the generic Is and exist so callers can keep a uniform dispatch style — see the github.com/foomo/keel server's service shutdown loop for the canonical usage.
Index ¶
- func Is[T any](v any) (T, bool)
- type Closer
- type CloserFunc
- type CloserWithContext
- type ErrorCloser
- type ErrorCloserWithContext
- type ErrorPinger
- type ErrorPingerWithContext
- type ErrorShutdowner
- type ErrorShutdownerWithContext
- type ErrorStopper
- type ErrorStopperWithContext
- type ErrorUnsubscriber
- type ErrorUnsubscriberWithContext
- type Namer
- type ReadmeHandler
- type Readmer
- type Shutdowner
- type ShutdownerWithContext
- type Stopper
- type StopperWithContext
- type Unsubscriber
- type UnsubscriberWithContext
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Is ¶ added in v0.27.0
Is reports whether v implements T and returns the asserted value. When v does not implement T the returned value is the zero value of T and ok is false.
Example ¶
ExampleIs shows the generic helper used directly to assert an arbitrary interface — here interfaces.Readmer.
package main
import (
"fmt"
"github.com/foomo/keel/interfaces"
)
func main() {
var v any = interfaces.ReadmeFunc(func() string { return "ok" })
if r, ok := interfaces.Is[interfaces.Readmer](v); ok {
fmt.Println(r.Readme())
}
}
Output: ok
Types ¶
type Closer ¶
type Closer interface {
Close()
}
Closer is implemented by any value that exposes a Close method without a return value. It is the simplest of the four Close shapes; see also ErrorCloser, CloserWithContext and ErrorCloserWithContext.
func IsCloser ¶ added in v0.27.0
IsCloser reports whether v implements Closer and returns the asserted value. The boolean is false when v does not implement Closer.
Example ¶
ExampleIsCloser shows the type-assertion dispatch pattern used to invoke whichever Close shape a service implements.
package main
import (
"context"
"fmt"
"github.com/foomo/keel/interfaces"
)
type closerErrCtx struct{}
func (closerErrCtx) Close(context.Context) error { return nil }
func main() {
var svc any = closerErrCtx{}
if _, ok := interfaces.IsCloser(svc); ok {
fmt.Println("plain Closer")
}
if c, ok := interfaces.IsErrorCloserWithContext(svc); ok {
fmt.Println(c.Close(context.Background()))
}
}
Output: <nil>
type CloserFunc ¶ added in v0.18.0
CloserFunc is an adapter that allows an ordinary function with the signature func(ctx context.Context) error to be used as an ErrorCloserWithContext. It mirrors the pattern of net/http.HandlerFunc.
Example ¶
ExampleCloserFunc adapts a plain func into an interfaces.ErrorCloserWithContext.
package main
import (
"context"
"fmt"
"github.com/foomo/keel/interfaces"
)
func main() {
closer := interfaces.CloserFunc(func(context.Context) error {
fmt.Println("closing")
return nil
})
_ = closer.Close(context.Background())
}
Output: closing
func (CloserFunc) Close ¶ added in v0.18.0
func (f CloserFunc) Close(ctx context.Context) error
Close calls f(ctx) and returns its result, so a CloserFunc satisfies ErrorCloserWithContext.
type CloserWithContext ¶
CloserWithContext is like Closer but its Close method accepts a context.Context so the caller can bound the close operation.
func IsCloserWithContext ¶ added in v0.27.0
func IsCloserWithContext(v any) (CloserWithContext, bool)
IsCloserWithContext reports whether v implements CloserWithContext and returns the asserted value. The boolean is false when v does not implement CloserWithContext.
type ErrorCloser ¶
type ErrorCloser interface {
Close() error
}
ErrorCloser is like Closer but its Close method returns an error.
func IsErrorCloser ¶ added in v0.27.0
func IsErrorCloser(v any) (ErrorCloser, bool)
IsErrorCloser reports whether v implements ErrorCloser and returns the asserted value. The boolean is false when v does not implement ErrorCloser.
type ErrorCloserWithContext ¶
ErrorCloserWithContext combines ErrorCloser and CloserWithContext: its Close method accepts a context.Context and returns an error.
func IsErrorCloserWithContext ¶ added in v0.27.0
func IsErrorCloserWithContext(v any) (ErrorCloserWithContext, bool)
IsErrorCloserWithContext reports whether v implements ErrorCloserWithContext and returns the asserted value. The boolean is false when v does not implement ErrorCloserWithContext.
Example ¶
ExampleIsErrorCloserWithContext asserts for the richest Close shape and calls it with a bounded context.
package main
import (
"context"
"fmt"
"github.com/foomo/keel/interfaces"
)
type closerErrCtx struct{}
func (closerErrCtx) Close(context.Context) error { return nil }
func main() {
var v any = closerErrCtx{}
c, ok := interfaces.IsErrorCloserWithContext(v)
if !ok {
return
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
fmt.Println(c.Close(ctx))
}
Output: <nil>
type ErrorPinger ¶
type ErrorPinger interface {
Ping() error
}
ErrorPinger is implemented by any value whose health can be probed through a Ping method that returns an error on failure. See also ErrorPingerWithContext.
func IsErrorPinger ¶ added in v0.27.0
func IsErrorPinger(v any) (ErrorPinger, bool)
IsErrorPinger reports whether v implements ErrorPinger and returns the asserted value. The boolean is false when v does not implement ErrorPinger.
Example ¶
ExampleIsErrorPinger asserts the health-probe shape.
package main
import (
"errors"
"fmt"
"github.com/foomo/keel/interfaces"
)
type pingerErr struct{ err error }
func (p pingerErr) Ping() error { return p.err }
func main() {
var v any = pingerErr{err: errors.New("unreachable")}
if p, ok := interfaces.IsErrorPinger(v); ok {
fmt.Println(p.Ping())
}
}
Output: unreachable
type ErrorPingerWithContext ¶
ErrorPingerWithContext is like ErrorPinger but its Ping method accepts a context.Context so the caller can bound the probe.
func IsErrorPingerWithContext ¶ added in v0.27.0
func IsErrorPingerWithContext(v any) (ErrorPingerWithContext, bool)
IsErrorPingerWithContext reports whether v implements ErrorPingerWithContext and returns the asserted value. The boolean is false when v does not implement ErrorPingerWithContext.
type ErrorShutdowner ¶
type ErrorShutdowner interface {
Shutdown() error
}
ErrorShutdowner is like Shutdowner but its Shutdown method returns an error.
func IsErrorShutdowner ¶ added in v0.27.0
func IsErrorShutdowner(v any) (ErrorShutdowner, bool)
IsErrorShutdowner reports whether v implements ErrorShutdowner and returns the asserted value. The boolean is false when v does not implement ErrorShutdowner.
type ErrorShutdownerWithContext ¶
ErrorShutdownerWithContext combines ErrorShutdowner and ShutdownerWithContext: its Shutdown method accepts a context.Context and returns an error.
func IsErrorShutdownerWithContext ¶ added in v0.27.0
func IsErrorShutdownerWithContext(v any) (ErrorShutdownerWithContext, bool)
IsErrorShutdownerWithContext reports whether v implements ErrorShutdownerWithContext and returns the asserted value. The boolean is false when v does not implement ErrorShutdownerWithContext.
type ErrorStopper ¶
type ErrorStopper interface {
Stop() error
}
ErrorStopper is like Stopper but its Stop method returns an error.
func IsErrorStopper ¶ added in v0.27.0
func IsErrorStopper(v any) (ErrorStopper, bool)
IsErrorStopper reports whether v implements ErrorStopper and returns the asserted value. The boolean is false when v does not implement ErrorStopper.
Example ¶
ExampleIsErrorStopper asserts the error-returning Stop shape.
package main
import (
"fmt"
"github.com/foomo/keel/interfaces"
)
type stopperErr struct{}
func (stopperErr) Stop() error { return nil }
func main() {
var v any = stopperErr{}
if s, ok := interfaces.IsErrorStopper(v); ok {
fmt.Println(s.Stop())
}
}
Output: <nil>
type ErrorStopperWithContext ¶
ErrorStopperWithContext combines ErrorStopper and StopperWithContext: its Stop method accepts a context.Context and returns an error.
func IsErrorStopperWithContext ¶ added in v0.27.0
func IsErrorStopperWithContext(v any) (ErrorStopperWithContext, bool)
IsErrorStopperWithContext reports whether v implements ErrorStopperWithContext and returns the asserted value. The boolean is false when v does not implement ErrorStopperWithContext.
type ErrorUnsubscriber ¶
type ErrorUnsubscriber interface {
Unsubscribe() error
}
ErrorUnsubscriber is like Unsubscriber but its Unsubscribe method returns an error.
func IsErrorUnsubscriber ¶ added in v0.27.0
func IsErrorUnsubscriber(v any) (ErrorUnsubscriber, bool)
IsErrorUnsubscriber reports whether v implements ErrorUnsubscriber and returns the asserted value. The boolean is false when v does not implement ErrorUnsubscriber.
type ErrorUnsubscriberWithContext ¶
ErrorUnsubscriberWithContext combines ErrorUnsubscriber and UnsubscriberWithContext: its Unsubscribe method accepts a context.Context and returns an error.
func IsErrorUnsubscriberWithContext ¶ added in v0.27.0
func IsErrorUnsubscriberWithContext(v any) (ErrorUnsubscriberWithContext, bool)
IsErrorUnsubscriberWithContext reports whether v implements ErrorUnsubscriberWithContext and returns the asserted value. The boolean is false when v does not implement ErrorUnsubscriberWithContext.
type Namer ¶
type Namer interface {
Name() string
}
Namer is implemented by any value that exposes a stable, human-readable identifier through a Name method. It is typically used for logging, metric labels and registry lookups.
func IsNamer ¶ added in v0.27.0
IsNamer reports whether v implements Namer and returns the asserted value. The boolean is false when v does not implement Namer.
Example ¶
ExampleIsNamer reports whether v carries a stable identifier.
package main
import (
"fmt"
"github.com/foomo/keel/interfaces"
)
type namer struct{ n string }
func (x namer) Name() string { return x.n }
func main() {
var v any = namer{n: "queue"}
if n, ok := interfaces.IsNamer(v); ok {
fmt.Println(n.Name())
}
}
Output: queue
type ReadmeHandler ¶
type ReadmeHandler struct {
Value func() string
}
ReadmeHandler adapts a func() string into a value satisfying Readmer. Use ReadmeFunc to construct one.
func ReadmeFunc ¶
func ReadmeFunc(v func() string) ReadmeHandler
ReadmeFunc returns a ReadmeHandler that calls v whenever Readme is invoked.
Example ¶
ExampleReadmeFunc adapts a func returning documentation into a interfaces.Readmer.
package main
import (
"fmt"
"github.com/foomo/keel/interfaces"
)
func main() {
r := interfaces.ReadmeFunc(func() string { return "service: cache" })
fmt.Println(r.Readme())
}
Output: service: cache
func (ReadmeHandler) Readme ¶
func (r ReadmeHandler) Readme() string
Readme calls the wrapped func and returns its result, so ReadmeHandler satisfies Readmer.
type Readmer ¶
type Readmer interface {
Readme() string
}
Readmer is implemented by any value that can describe itself through a Readme method returning human-readable documentation, typically rendered on a diagnostic HTTP endpoint.
type Shutdowner ¶
type Shutdowner interface {
Shutdown()
}
Shutdowner is implemented by any value that exposes a Shutdown method without a return value. It is the simplest of the four Shutdown shapes; see also ErrorShutdowner, ShutdownerWithContext and ErrorShutdownerWithContext.
func IsShutdowner ¶ added in v0.27.0
func IsShutdowner(v any) (Shutdowner, bool)
IsShutdowner reports whether v implements Shutdowner and returns the asserted value. The boolean is false when v does not implement Shutdowner.
Example ¶
ExampleIsShutdowner asserts the simplest Shutdown shape.
package main
import (
"fmt"
"github.com/foomo/keel/interfaces"
)
type shutdownerPlain struct{ called *bool }
func (s shutdownerPlain) Shutdown() { *s.called = true }
func main() {
called := false
var v any = shutdownerPlain{called: &called}
if s, ok := interfaces.IsShutdowner(v); ok {
s.Shutdown()
fmt.Println(called)
}
}
Output: true
type ShutdownerWithContext ¶
ShutdownerWithContext is like Shutdowner but its Shutdown method accepts a context.Context so the caller can bound the operation.
func IsShutdownerWithContext ¶ added in v0.27.0
func IsShutdownerWithContext(v any) (ShutdownerWithContext, bool)
IsShutdownerWithContext reports whether v implements ShutdownerWithContext and returns the asserted value. The boolean is false when v does not implement ShutdownerWithContext.
type Stopper ¶
type Stopper interface {
Stop()
}
Stopper is implemented by any value that exposes a Stop method without a return value. It is the simplest of the four Stop shapes; see also ErrorStopper, StopperWithContext and ErrorStopperWithContext.
type StopperWithContext ¶
StopperWithContext is like Stopper but its Stop method accepts a context.Context so the caller can bound the operation.
func IsStopperWithContext ¶ added in v0.27.0
func IsStopperWithContext(v any) (StopperWithContext, bool)
IsStopperWithContext reports whether v implements StopperWithContext and returns the asserted value. The boolean is false when v does not implement StopperWithContext.
type Unsubscriber ¶
type Unsubscriber interface {
Unsubscribe()
}
Unsubscriber is implemented by any value that exposes an Unsubscribe method without a return value. It is the simplest of the four Unsubscribe shapes; see also ErrorUnsubscriber, UnsubscriberWithContext and ErrorUnsubscriberWithContext.
func IsUnsubscriber ¶ added in v0.27.0
func IsUnsubscriber(v any) (Unsubscriber, bool)
IsUnsubscriber reports whether v implements Unsubscriber and returns the asserted value. The boolean is false when v does not implement Unsubscriber.
type UnsubscriberWithContext ¶
UnsubscriberWithContext is like Unsubscriber but its Unsubscribe method accepts a context.Context so the caller can bound the operation.
func IsUnsubscriberWithContext ¶ added in v0.27.0
func IsUnsubscriberWithContext(v any) (UnsubscriberWithContext, bool)
IsUnsubscriberWithContext reports whether v implements UnsubscriberWithContext and returns the asserted value. The boolean is false when v does not implement UnsubscriberWithContext.
Example ¶
ExampleIsUnsubscriberWithContext asserts the context-aware Unsubscribe shape.
package main
import (
"context"
"fmt"
"github.com/foomo/keel/interfaces"
)
type unsubscriberCtx struct{ called *bool }
func (u unsubscriberCtx) Unsubscribe(context.Context) { *u.called = true }
func main() {
called := false
var v any = unsubscriberCtx{called: &called}
if u, ok := interfaces.IsUnsubscriberWithContext(v); ok {
u.Unsubscribe(context.Background())
fmt.Println(called)
}
}
Output: true