gokit

package module
v0.0.0-...-ff4d915 Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2022 License: MIT Imports: 2 Imported by: 2

README

WIP: in-place replacement for go-kit with minimum dependencies

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrMissingValue = log.ErrMissingValue

ErrMissingValue is appended to keyvals slices with odd length to substitute the missing value.

Functions

func Nop

func Nop(context.Context, interface{}) (interface{}, error)

Nop is an endpoint that does nothing and returns a nil error. Useful for tests.

Types

type Endpoint

type Endpoint func(ctx context.Context, request interface{}) (response interface{}, err error)

Endpoint is the fundamental building block of servers and clients. It represents a single RPC method.

type ErrorHandler

type ErrorHandler interface {
	Handle(ctx context.Context, err error)
}

ErrorHandler receives a transport error to be processed for diagnostic purposes. Usually this means logging the error.

type ErrorHandlerFunc

type ErrorHandlerFunc func(ctx context.Context, err error)

The ErrorHandlerFunc type is an adapter to allow the use of ordinary function as ErrorHandler. If f is a function with the appropriate signature, ErrorHandlerFunc(f) is a ErrorHandler that calls f.

func (ErrorHandlerFunc) Handle

func (f ErrorHandlerFunc) Handle(ctx context.Context, err error)

Handle calls f(ctx, err).

type Failer

type Failer interface {
	Failed() error
}

Failer may be implemented by Go kit response types that contain business logic error details. If Failed returns a non-nil error, the Go kit transport layer may interpret this as a business logic error, and may encode it differently than a regular, successful response.

It's not necessary for your response types to implement Failer, but it may help for more sophisticated use cases. The addsvc example shows how Failer should be used by a complete application.

type LogErrorHandler

type LogErrorHandler struct {
	// contains filtered or unexported fields
}

LogErrorHandler is a transport error handler implementation which logs an error.

func NewLogErrorHandler

func NewLogErrorHandler(logger log.Logger) *LogErrorHandler

func (*LogErrorHandler) Handle

func (h *LogErrorHandler) Handle(ctx context.Context, err error)

type Logger

type Logger = log.Logger

Logger is the fundamental interface for all log operations. Log creates a log event from keyvals, a variadic sequence of alternating keys and values. Implementations must be safe for concurrent use by multiple goroutines. In particular, any implementation of Logger that appends to keyvals or modifies or retains any of its elements must make a copy first.

func With

func With(logger Logger, keyvals ...interface{}) Logger

With returns a new contextual logger with keyvals prepended to those passed to calls to Log. If logger is also a contextual logger created by With, WithPrefix, or WithSuffix, keyvals is appended to the existing context.

The returned Logger replaces all value elements (odd indexes) containing a Valuer with their generated value for each call to its Log method.

func WithPrefix

func WithPrefix(logger Logger, keyvals ...interface{}) Logger

WithPrefix returns a new contextual logger with keyvals prepended to those passed to calls to Log. If logger is also a contextual logger created by With, WithPrefix, or WithSuffix, keyvals is prepended to the existing context.

The returned Logger replaces all value elements (odd indexes) containing a Valuer with their generated value for each call to its Log method.

func WithSuffix

func WithSuffix(logger Logger, keyvals ...interface{}) Logger

WithSuffix returns a new contextual logger with keyvals appended to those passed to calls to Log. If logger is also a contextual logger created by With, WithPrefix, or WithSuffix, keyvals is appended to the existing context.

The returned Logger replaces all value elements (odd indexes) containing a Valuer with their generated value for each call to its Log method.

type LoggerFunc

type LoggerFunc = log.LoggerFunc

LoggerFunc is an adapter to allow use of ordinary functions as Loggers. If f is a function with the appropriate signature, LoggerFunc(f) is a Logger object that calls f.

type Middleware

type Middleware func(Endpoint) Endpoint

Middleware is a chainable behavior modifier for endpoints.

func Chain

func Chain(outer Middleware, others ...Middleware) Middleware

Chain is a helper function for composing middlewares. Requests will traverse them in the order they're declared. That is, the first middleware is treated as the outermost middleware.

Example
package main

import (
	"context"
	"fmt"
)

func main() {
	e := Chain(
		annotate("first"),
		annotate("second"),
		annotate("third"),
	)(myEndpoint)

	if _, err := e(ctx, req); err != nil {
		panic(err)
	}

}

var (
	ctx = context.Background()
	req = struct{}{}
)

func annotate(s string) Middleware {
	return func(next Endpoint) Endpoint {
		return func(ctx context.Context, request interface{}) (interface{}, error) {
			fmt.Println(s, "pre")
			defer fmt.Println(s, "post")
			return next(ctx, request)
		}
	}
}

func myEndpoint(context.Context, interface{}) (interface{}, error) {
	fmt.Println("my endpoint!")
	return struct{}{}, nil
}
Output:
first pre
second pre
third pre
my endpoint!
third post
second post
first post

Jump to

Keyboard shortcuts

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