flextime

package
v1.0.5 Latest Latest
Warning

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

Go to latest
Published: Aug 20, 2021 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package flextime provides adaptive timeouts when retrying AWS SDK requests.

Out of the box, the AWS SDK for Go only supports the static timeout values available on the Go standard HTTP client (http.Client from package net/http). These timeout values cannot be changed per request attempt (initial attempt and retry). Install a flextime timeout function on an AWS SDK session or client to vary timeouts across request attempts.

To set an initial low timeout, and back off to successively higher timeout values, use a sequence:

f := flextime.Sequence(350*time.Millisecond, 1*time.Second, 2*time.Second)
s := session.Must(session.NewSession())
err := flextime.OnSession(s, f)   // Install timeout sequence all clients with session
if err != nil {
	// Handle error
}
ddb := dynamodb.New(s)            // New DynamoDB client with session, will use f
loc := locationservice.New(s)     // New Amazon Location Service client with session, will use f

To add install a timeout function on a specific client instance:

c := sqs.New(s)
err := flextime.OnClient(c.Client, f) // Install timeout function f on new SQS client
if err != nil {
	// Handle error
}

To roll your own timeout function:

func myTimeoutFunc(r *request.Request, int n) time.Duration {
	return ...
}

func main() {
	var err error
	err = flextime.OnSession(s, myTimeoutFunc)         // Install for all clients with session...
	if err != nil {
		// Handle error
	}
	err = flextime.OnClient(ddb.Client, myTimeoutFunc) // ...or install for specific client only
	if err != nil {
		// Handle error
	}
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func OnClient

func OnClient(c *client.Client, f TimeoutFunc) error

OnClient sets the TimeoutFunc used to compute timeouts for the given AWS SDK client. The previous TimeoutFunc on the session, if any, is replaced.

func OnSession

func OnSession(s *session.Session, f TimeoutFunc) error

OnSession sets the TimeoutFunc used to compute timeouts for all clients created using the given AWS SDK session. The previous TimeoutFunc on the session, if any, is replaced.

Types

type TimeoutFunc

type TimeoutFunc func(r *request.Request, n int) time.Duration

A TimeoutFunc computes a timeout for an AWS SDK request attempt based on the request state and the number n of timeouts that have occurred while executing previous attempts.

A positive return value sets a timeout of that duration on the next request attempt. A zero return value means no timeout.

func Sequence

func Sequence(usual time.Duration, after ...time.Duration) TimeoutFunc

Sequence constructs a timeout function that varies the next timeout value if the previous attempt timed out.

Use Sequence if you find the remote service often exhibits one-off slow response times that can be cured by quickly timing out and retrying, but you also need to protect your application (and the remote service) from retry storms and failure if the remote service goes through a burst of slowness where most response times during the burst are slower than your usual quick timeout.

Parameter usual represents the timeout value the function will return for an initial attempt and for any retry where the immediately preceding attempt did not time out.

Parameter after contains timeout values the function will return if the previous attempt timed out. If this was the first timeout of the execution, after[0] is returned; if the second, after[1], and so on. If more attempts have timed out within the client request than after has elements, then the last element of after is returned.

Consider the following timeout function:

f := Sequence(200*time.Millisecond, time.Second, 10*time.Second)

The function f will use 200 milliseconds as the usual timeout but if the preceding attempt timed out and was the first timeout of the client request, it will use 1 second; and if the previous attempt timed out and was not the first attempt, it will use 10 seconds.

Jump to

Keyboard shortcuts

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