callmeback

package module
v0.0.0-...-755ad65 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2021 License: BSD-3-Clause Imports: 6 Imported by: 0

Documentation

Overview

Package callmeback is a generic server-side "come again in ..." middleware for gRPC.

In the case where a gRPC stream would be nice to provide but impossible to deploy (see 1) this interceptor enables a pool-based unary call replacement for push-based streams.

It adds a trailer duration value indicating to the client the time it is safe to pause for before calling again.

Example

Simple example of server initialization code.

package main

import (
	"context"
	"time"

	"github.com/fenollp/grpc-callmeback-interceptor/go-callmeback"
	"github.com/grpc-ecosystem/go-grpc-middleware"
	"google.golang.org/grpc"
)

type everySecond struct{}

func (*everySecond) PleaseComeAgain(ctx context.Context, c callmeback.Context) (time.Duration, error) {
	return time.Second, nil
}

// Simple example of server initialization code.
func main() {
	// Create unary/stream rateLimiters, based on token bucket here.
	cb := &everySecond{}
	_ = grpc.NewServer(
		grpc_middleware.WithUnaryServerChain(
			callmeback.UnaryServerInterceptor(cb),
		),
	)
}
Output:

Index

Examples

Constants

View Source
const ErrBadTrailer = errStr("bad callmeback trailer")

ErrBadTrailer is a constant error

Variables

View Source
var Trailer = "x-pleasecomeagain"

Trailer names the trailer value passed to the client

Functions

func In

func In(md metadata.MD) (d time.Duration, err error)

In reads and parses the duration trailer or fails with an error.

func UnaryServerInterceptor

func UnaryServerInterceptor(callmebacker CallMeBacker) grpc.UnaryServerInterceptor

UnaryServerInterceptor returns a new unary server interceptor that instructs the client to call again.

Types

type CallMeBacker

type CallMeBacker interface {
	// If PleaseComeAgain returns a non-nil error, the request will have been processed for nothing.
	// If it returns a positive duration, a trailer is added to the response.
	PleaseComeAgain(ctx context.Context, c Context) (time.Duration, error)
}

CallMeBacker defines the interface to instruct client when to call back.

type Context

type Context struct {
	Elapsed    time.Duration
	FullMethod string
}

Context can be used to make more accurate predictions of the duration.

Directories

Path Synopsis
Package buffconn is only a test packge for go-callmeback.
Package buffconn is only a test packge for go-callmeback.

Jump to

Keyboard shortcuts

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