Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
joeig committed Nov 26, 2021
0 parents commit c1e58db
Show file tree
Hide file tree
Showing 9 changed files with 221 additions and 0 deletions.
38 changes: 38 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
c.out
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -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.
25 changes: 25 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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}
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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).
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/joeig/singleshot

go 1.17

require golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
74 changes: 74 additions & 0 deletions singleshot.go
Original file line number Diff line number Diff line change
@@ -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()}, " ")
}
18 changes: 18 additions & 0 deletions singleshot_test.go
Original file line number Diff line number Diff line change
@@ -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),
}
}

0 comments on commit c1e58db

Please sign in to comment.