singleshot

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2021 License: MIT Imports: 6 Imported by: 0

README

Singleshot

Singleshot provides an http.RoundTripper which deduplicates similar HTTP requests.

Build Status Go Report Card PkgGoDev

If two similar HTTP requests are supposed to be sent concurrently, the first one will actually be sent to the server, while the second one waits until the first one was fulfilled completely. The second request will never be sent to the server, but returns a copy of the response of the first request.

Req 1 -----------------> Resp 1
             Req 2 ----> Resp 1'
                                Req 3 -------------> Resp 3

Usage

import (
	"github.com/joeig/singleshot"
)

_ = http.Client{
	Transport: singleshot.NewTransport(http.DefaultTransport),
}

Notes

  • Always apply proper timeouts or use requests with contexts, otherwise one request which is timing out may stop subsequent requests from being retried.
  • Requests are considered deduplicatable, if they share the same HTTP method and request URI. Furthermore, the method has to be GET and the request must not be a range request. The body is ignored according to RFC 2616, section 9.3.

Documentation

See GoDoc.

Documentation

Overview

Package singleshot provides an http.RoundTripper which deduplicates similar HTTP requests.

If two similar HTTP requests are supposed to be sent concurrently, the first one will actually be sent to the server, while the second one waits until the first one was fulfilled completely. The second request will never be sent to the server, but returns a copy of the response of the first request.

Req 1 -----------------> Resp 1
             Req 2 ----> Resp 1'
                                Req 3 -------------> Resp 3

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Transport

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

Transport is safe for concurrent use.

func NewTransport

func NewTransport(transport http.RoundTripper) *Transport

NewTransport creates a new instance of singleshot.Transport.

Example
package main

import (
	"net/http"

	"github.com/joeig/singleshot"
)

func main() {
	_ = http.Client{
		Transport: singleshot.NewTransport(http.DefaultTransport),
	}
}
Output:

func (*Transport) RoundTrip

func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip deduplicates similar subsequential HTTP requests, if the first request of a kind has not been completely fulfilled yet.

Only "GET" requests (excluding "range" requests) are deduplicated, other requests are passed. Request are considered similar, if they share the same method and request URI (see RFC 2616, section 9.3).

Always apply proper timeouts or use requests with contexts, otherwise one request which is timing out may stop subsequent requests from being retried.

While the response body is a valid io.ReadCloser, the transfer itself has finished.

Jump to

Keyboard shortcuts

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