Documentation

Overview

Package rpcretry automatically retries when some RPCs end in error. RPC sometimes fails rarely and this may be able to recover simply by retrying.

Non idempotency operations (Commit, Rollback, and Next) are not automatically retried.

By default, it retries up to 3 times. First wait 100 milliseconds, then wait exponentially back off. This value can be changed by option.

Example (HowToUse)
package main

import (
	"context"
	"time"

	"go.mercari.io/datastore/clouddatastore"
	"go.mercari.io/datastore/dsmiddleware/rpcretry"
	"go.mercari.io/datastore/internal/testutils"
)

func main() {
	ctx := context.Background()
	client, err := clouddatastore.FromContext(ctx)
	if err != nil {
		panic(err)
	}
	defer client.Close()
	defer testutils.CleanUpAllEntities(ctx, client)

	mw := rpcretry.New(
		rpcretry.WithRetryLimit(5),
		rpcretry.WithMinBackoffDuration(10*time.Millisecond),
		rpcretry.WithMaxBackoffDuration(150*time.Microsecond),
		// rpcretry.WithMaxDoublings(2),
	)
	client.AppendMiddleware(mw)
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(opts ...RetryOption) datastore.Middleware

New automatically RPC retry middleware creates & returns.

Types

type RetryOption

type RetryOption interface {
	Apply(*retryHandler)
}

A RetryOption is an retry option for a retry middleware.

func WithLogf

func WithLogf(logf func(ctx context.Context, format string, args ...interface{})) RetryOption

WithLogf creates a ClientOption that uses the specified logger.

Deprecated: use WithLogger instead.

func WithLogger

func WithLogger(logf func(ctx context.Context, format string, args ...interface{})) RetryOption

WithLogger creates a ClientOption that uses the specified logger.

func WithMaxBackoffDuration

func WithMaxBackoffDuration(d time.Duration) RetryOption

WithMaxBackoffDuration specified maximum duratiuon of retry backoff.

func WithMaxDoublings

func WithMaxDoublings(maxDoublings int) RetryOption

WithMaxDoublings specifies how many times the waiting time should be doubled.

func WithMinBackoffDuration

func WithMinBackoffDuration(d time.Duration) RetryOption

WithMinBackoffDuration specified minimal duration of retry backoff.

func WithRetryLimit

func WithRetryLimit(limit int) RetryOption

WithRetryLimit provides retry limit when RPC failed.