From c1e58db2200721d84792e49e38926d6776e8704e Mon Sep 17 00:00:00 2001 From: Johannes Eiglsperger Date: Fri, 26 Nov 2021 21:19:05 +0100 Subject: [PATCH] Initial commit --- .github/workflows/tests.yml | 38 +++++++++++++++++++ .gitignore | 1 + LICENSE | 21 +++++++++++ Makefile | 25 +++++++++++++ README.md | 37 +++++++++++++++++++ go.mod | 5 +++ go.sum | 2 + singleshot.go | 74 +++++++++++++++++++++++++++++++++++++ singleshot_test.go | 18 +++++++++ 9 files changed, 221 insertions(+) create mode 100644 .github/workflows/tests.yml create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 Makefile create mode 100644 README.md create mode 100644 go.mod create mode 100644 go.sum create mode 100644 singleshot.go create mode 100644 singleshot_test.go diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml new file mode 100644 index 0000000..5388642 --- /dev/null +++ b/.github/workflows/tests.yml @@ -0,0 +1,38 @@ +--- +name: Tests +on: [push, pull_request] +jobs: + lint: + name: Lint + runs-on: ubuntu-latest + steps: + - name: Set up Go ${{ matrix.goVer }} + uses: actions/setup-go@v1 + with: + go-version: 1.17 + - name: Check out code into the Go module directory + uses: actions/checkout@v1 + - name: GolangCI-Lint + run: | + curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $(go env GOPATH)/bin v1.21.0 + $(go env GOPATH)/bin/golangci-lint --version + $(go env GOPATH)/bin/golangci-lint run + - name: Staticcheck + run: | + GO111MODULE=off go get -u honnef.co/go/tools/cmd/staticcheck + $(go env GOPATH)/bin/staticcheck ./... + test: + name: Test + runs-on: ubuntu-latest + strategy: + matrix: + goVer: [1.15, 1.16, 1.17] + steps: + - name: Set up Go ${{ matrix.goVer }} + uses: actions/setup-go@v1 + with: + go-version: ${{ matrix.goVer }} + - name: Check out code into the Go module directory + uses: actions/checkout@v1 + - name: Make + run: make diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f400925 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +c.out \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..83f33c9 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2021 Johannes Eiglsperger + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..bdd02dc --- /dev/null +++ b/Makefile @@ -0,0 +1,25 @@ +GOCMD=go +GOTEST=$(GOCMD) test +GOCOVER=$(GOCMD) tool cover +GOFMT=gofmt + +.DEFAULT_GOAL := all + +.PHONY: all +all: check-fmt test coverage + +.PHONY: test +test: + $(GOTEST) -v ./... -covermode=count -coverprofile=c.out + +.PHONY: coverage +coverage: + $(GOCOVER) -func=c.out + +.PHONY: check-fmt +check-fmt: + $(GOFMT) -d ${GOFILES} + +.PHONY: fmt +fmt: + $(GOFMT) -w ${GOFILES} diff --git a/README.md b/README.md new file mode 100644 index 0000000..0e0f1c3 --- /dev/null +++ b/README.md @@ -0,0 +1,37 @@ +# Singleshot + +Singleshot provides an `http.RoundTripper` which deduplicates similar HTTP requests. + +[![Build Status](https://github.com/joeig/singleshot/workflows/Tests/badge.svg)](https://github.com/joeig/singleshot/actions) +[![Go Report Card](https://goreportcard.com/badge/github.com/joeig/singleshot)](https://goreportcard.com/report/github.com/joeig/singleshot) +[![PkgGoDev](https://pkg.go.dev/badge/github.com/joeig/singleshot)](https://pkg.go.dev/github.com/joeig/singleshot) + +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. + +```text +Req 1 -----------------> Resp 1 + Req 2 ----> Resp 1' + Req 3 -------------> Resp 3 +``` + +## Usage + +```go +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](https://www.rfc-editor.org/rfc/rfc2616#section-9.3). + +## Documentation + +See [GoDoc](https://godoc.org/github.com/joeig/singleshot). diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..810f69f --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module github.com/joeig/singleshot + +go 1.17 + +require golang.org/x/sync v0.0.0-20210220032951-036812b2e83c diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..5c00efd --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ= +golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= diff --git a/singleshot.go b/singleshot.go new file mode 100644 index 0000000..2e2b025 --- /dev/null +++ b/singleshot.go @@ -0,0 +1,74 @@ +// 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 +package singleshot + +import ( + "bufio" + "bytes" + "net/http" + "net/http/httputil" + "strings" + + "golang.org/x/sync/singleflight" +) + +// Transport is safe for concurrent use. +type Transport struct { + transport http.RoundTripper + requestGroup singleflight.Group +} + +// NewTransport creates a new instance of singleshot.Transport. +func NewTransport(transport http.RoundTripper) *Transport { + return &Transport{ + transport: transport, + requestGroup: singleflight.Group{}, + } +} + +// 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. +func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error) { + if !isDeduplicatable(req) { + return t.transport.RoundTrip(req) + } + + respBytes, err, _ := t.requestGroup.Do(groupKey(req), func() (interface{}, error) { + resp, err := t.transport.RoundTrip(req) + if err != nil { + return nil, err + } + + return httputil.DumpResponse(resp, true) + }) + + if err != nil { + return nil, err + } + + b := bytes.NewBuffer(respBytes.([]byte)) + return http.ReadResponse(bufio.NewReader(b), req) +} + +func isDeduplicatable(req *http.Request) bool { + const rangeHeader = "range" + return req.Method == http.MethodGet && req.Header.Get(rangeHeader) == "" +} + +func groupKey(req *http.Request) string { + return strings.Join([]string{req.Method, req.URL.String()}, " ") +} diff --git a/singleshot_test.go b/singleshot_test.go new file mode 100644 index 0000000..5c05958 --- /dev/null +++ b/singleshot_test.go @@ -0,0 +1,18 @@ +package singleshot_test + +import ( + "net/http" + "testing" + + "github.com/joeig/singleshot" +) + +func TestNewTransport(t *testing.T) { + var _ http.RoundTripper = (*singleshot.Transport)(nil) +} + +func ExampleNewTransport() { + _ = http.Client{ + Transport: singleshot.NewTransport(http.DefaultTransport), + } +}