batch

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2019 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package batch helps write efficient graphql resolvers with infrastructure for combining multiple RPCs into single batched RPCs. Batched RPCs are often more efficient that independent RPCs as they reduce the per-request overhead, but are difficult to use in graphql resolvers that only have a limited view of the overall query being computed.

For example, when using a graphql query to fetch a list of users and the group each user is a part of, often the resolver that fetches the user's group is called once for each user, with no direct link to the other users. By default, this means the query will result in the number of users RPCs.

This package's Func makes it easy to combine such independent invocations of a fetch function in a single batched RPC. Independent calls to Func.Invoke get automatically combined into a single call to the user-supplied Func.Many, resulting in only a single RPC with minimal changes to resolver code.

Index

Constants

View Source
const DefaultMaxDuration = 20 * time.Millisecond

DefaultMaxDuration is the default MaxDuration for Func.

View Source
const DefaultWaitInterval = 1 * time.Millisecond

DefaultWaitInterval is the default WaitInterval for Func.

Variables

This section is empty.

Functions

func HasBatching

func HasBatching(ctx context.Context) bool

HasBatching returns if the given context has batching support.

func WithBatching

func WithBatching(ctx context.Context) context.Context

WithBatching adds batching support to the given context.

Types

type Func

type Func struct {
	// Many computes a function for a batch of inputs. For example, a Func
	// might fetch multiple rows from MySQL.
	Many func(ctx context.Context, args []interface{}) ([]interface{}, error)
	// Shard optionally splits different classes of inputs into independent
	// invocations of Many. For example, a Func that fetches rows from a SQL
	// database might shard by table so that each invocation of Many only has to
	// fetch rows from a single table.
	Shard func(arg interface{}) (shard interface{})
	// MaxSize optionally limits the size of a batch. After receiving MaxSize
	// invocations, Many will be invoked even if some goroutines are stil running.
	// Zero, the default, means no limit.
	MaxSize int
	// WaitInterval is the duration of a timer that is reset every time the
	// batch function is invoked. Many will be invoked when either the
	// WaitInterval or MaxDuration expires.
	WaitInterval time.Duration
	// MaxDuration limits the duration of a batch. After waiting for
	// MaxDuration, Many will be invoked even if some goroutines are still
	// running. Defaults to DefaultMaxDuration.
	MaxDuration time.Duration
}

A Func transforms a function that takes a batch of inputs (Func.Many) into a function that takes single inputs (Func.Invoke). Multiple concurrenct invocations of Func.Invoke get combined into a single call to Func.Many.

func (*Func) Invoke

func (f *Func) Invoke(ctx context.Context, arg interface{}) (interface{}, error)

Invoke arranges for the Func's Many to be called with arg as one of its arguments, and returns the corresponding result.

Jump to

Keyboard shortcuts

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