sync
This commit is contained in:
422
cover.html
Normal file
422
cover.html
Normal file
@@ -0,0 +1,422 @@
|
||||
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
||||
<title>potency: Go Coverage Report</title>
|
||||
<style>
|
||||
body {
|
||||
background: black;
|
||||
color: rgb(80, 80, 80);
|
||||
}
|
||||
body, pre, #legend span {
|
||||
font-family: Menlo, monospace;
|
||||
font-weight: bold;
|
||||
}
|
||||
#topbar {
|
||||
background: black;
|
||||
position: fixed;
|
||||
top: 0; left: 0; right: 0;
|
||||
height: 42px;
|
||||
border-bottom: 1px solid rgb(80, 80, 80);
|
||||
}
|
||||
#content {
|
||||
margin-top: 50px;
|
||||
}
|
||||
#nav, #legend {
|
||||
float: left;
|
||||
margin-left: 10px;
|
||||
}
|
||||
#legend {
|
||||
margin-top: 12px;
|
||||
}
|
||||
#nav {
|
||||
margin-top: 10px;
|
||||
}
|
||||
#legend span {
|
||||
margin: 0 5px;
|
||||
}
|
||||
.cov0 { color: rgb(192, 0, 0) }
|
||||
.cov1 { color: rgb(128, 128, 128) }
|
||||
.cov2 { color: rgb(116, 140, 131) }
|
||||
.cov3 { color: rgb(104, 152, 134) }
|
||||
.cov4 { color: rgb(92, 164, 137) }
|
||||
.cov5 { color: rgb(80, 176, 140) }
|
||||
.cov6 { color: rgb(68, 188, 143) }
|
||||
.cov7 { color: rgb(56, 200, 146) }
|
||||
.cov8 { color: rgb(44, 212, 149) }
|
||||
.cov9 { color: rgb(32, 224, 152) }
|
||||
.cov10 { color: rgb(20, 236, 155) }
|
||||
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div id="topbar">
|
||||
<div id="nav">
|
||||
<select id="files">
|
||||
|
||||
<option value="file0">github.com/gopatchy/potency/bodyintercept.go (80.0%)</option>
|
||||
|
||||
<option value="file1">github.com/gopatchy/potency/potency.go (92.4%)</option>
|
||||
|
||||
<option value="file2">github.com/gopatchy/potency/responsewriterintercept.go (66.7%)</option>
|
||||
|
||||
</select>
|
||||
</div>
|
||||
<div id="legend">
|
||||
<span>not tracked</span>
|
||||
|
||||
<span class="cov0">no coverage</span>
|
||||
<span class="cov1">low coverage</span>
|
||||
<span class="cov2">*</span>
|
||||
<span class="cov3">*</span>
|
||||
<span class="cov4">*</span>
|
||||
<span class="cov5">*</span>
|
||||
<span class="cov6">*</span>
|
||||
<span class="cov7">*</span>
|
||||
<span class="cov8">*</span>
|
||||
<span class="cov9">*</span>
|
||||
<span class="cov10">high coverage</span>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
<div id="content">
|
||||
|
||||
<pre class="file" id="file0" style="display: none">package potency
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"hash"
|
||||
"io"
|
||||
)
|
||||
|
||||
type bodyIntercept struct {
|
||||
source io.ReadCloser
|
||||
sha256 hash.Hash
|
||||
}
|
||||
|
||||
func newBodyIntercept(source io.ReadCloser) *bodyIntercept <span class="cov10" title="6">{
|
||||
return &bodyIntercept{
|
||||
source: source,
|
||||
sha256: sha256.New(),
|
||||
}
|
||||
}</span>
|
||||
|
||||
func (bi *bodyIntercept) Read(p []byte) (int, error) <span class="cov10" title="6">{
|
||||
numBytes, err := bi.source.Read(p)
|
||||
bi.sha256.Write(p[:numBytes])
|
||||
|
||||
return numBytes, err
|
||||
}</span>
|
||||
|
||||
func (bi *bodyIntercept) Close() error <span class="cov0" title="0">{
|
||||
return bi.source.Close()
|
||||
}</span>
|
||||
</pre>
|
||||
|
||||
<pre class="file" id="file1" style="display: none">package potency
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/sha256"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/gopatchy/jsrest"
|
||||
)
|
||||
|
||||
type Potency struct {
|
||||
handler http.Handler
|
||||
|
||||
lifetime time.Duration
|
||||
|
||||
cache map[string]*savedResult
|
||||
cacheOldest *savedResult
|
||||
cacheNewest *savedResult
|
||||
cacheMu sync.RWMutex
|
||||
|
||||
inProgress map[string]bool
|
||||
inProgressMu sync.Mutex
|
||||
}
|
||||
|
||||
type savedResult struct {
|
||||
key string
|
||||
|
||||
method string
|
||||
url string
|
||||
requestHeader http.Header
|
||||
sha256 []byte
|
||||
|
||||
statusCode int
|
||||
responseHeader http.Header
|
||||
responseBody []byte
|
||||
|
||||
added time.Time
|
||||
newer *savedResult
|
||||
}
|
||||
|
||||
var (
|
||||
ErrConflict = errors.New("conflict")
|
||||
ErrMismatch = errors.New("idempotency mismatch")
|
||||
ErrBodyMismatch = fmt.Errorf("request body mismatch: %w", ErrMismatch)
|
||||
ErrMethodMismatch = fmt.Errorf("HTTP method mismatch: %w", ErrMismatch)
|
||||
ErrURLMismatch = fmt.Errorf("URL mismatch: %w", ErrMismatch)
|
||||
ErrHeaderMismatch = fmt.Errorf("Header mismatch: %w", ErrMismatch)
|
||||
ErrInvalidKey = errors.New("invalid Idempotency-Key")
|
||||
|
||||
criticalHeaders = []string{
|
||||
"Accept",
|
||||
"Authorization",
|
||||
}
|
||||
)
|
||||
|
||||
func NewPotency(handler http.Handler) *Potency <span class="cov4" title="3">{
|
||||
return &Potency{
|
||||
handler: handler,
|
||||
lifetime: 6 * time.Hour,
|
||||
cache: map[string]*savedResult{},
|
||||
inProgress: map[string]bool{},
|
||||
}
|
||||
}</span>
|
||||
|
||||
func (p *Potency) ServeHTTP(w http.ResponseWriter, r *http.Request) <span class="cov10" title="14">{
|
||||
val := r.Header.Get("Idempotency-Key")
|
||||
if val == "" </span><span class="cov0" title="0">{
|
||||
p.handler.ServeHTTP(w, r)
|
||||
return
|
||||
}</span>
|
||||
|
||||
<span class="cov10" title="14">err := p.serveHTTP(w, r, val)
|
||||
if err != nil </span><span class="cov6" title="5">{
|
||||
jsrest.WriteError(w, err)
|
||||
}</span>
|
||||
}
|
||||
|
||||
func (p *Potency) SetLifetime(lifetime time.Duration) <span class="cov1" title="1">{
|
||||
p.cacheMu.Lock()
|
||||
defer p.cacheMu.Unlock()
|
||||
|
||||
p.lifetime = lifetime
|
||||
}</span>
|
||||
|
||||
func (p *Potency) NumCached() int <span class="cov5" title="4">{
|
||||
p.cacheMu.RLock()
|
||||
defer p.cacheMu.RUnlock()
|
||||
|
||||
return len(p.cache)
|
||||
}</span>
|
||||
|
||||
func (p *Potency) serveHTTP(w http.ResponseWriter, r *http.Request, val string) error <span class="cov10" title="14">{
|
||||
if len(val) < 2 || !strings.HasPrefix(val, `"`) || !strings.HasSuffix(val, `"`) </span><span class="cov0" title="0">{
|
||||
return jsrest.Errorf(jsrest.ErrBadRequest, "%s (%w)", val, ErrInvalidKey)
|
||||
}</span>
|
||||
|
||||
<span class="cov10" title="14">key := val[1 : len(val)-1]
|
||||
|
||||
saved := p.read(key)
|
||||
|
||||
if saved != nil </span><span class="cov8" title="8">{
|
||||
if r.Method != saved.method </span><span class="cov1" title="1">{
|
||||
return jsrest.Errorf(jsrest.ErrBadRequest, "%s (%w)", r.Method, ErrMethodMismatch)
|
||||
}</span>
|
||||
|
||||
<span class="cov7" title="7">if r.URL.String() != saved.url </span><span class="cov1" title="1">{
|
||||
return jsrest.Errorf(jsrest.ErrBadRequest, "%s (%w)", r.URL.String(), ErrURLMismatch)
|
||||
}</span>
|
||||
|
||||
<span class="cov7" title="6">for _, h := range criticalHeaders </span><span class="cov9" title="11">{
|
||||
if saved.requestHeader.Get(h) != r.Header.Get(h) </span><span class="cov3" title="2">{
|
||||
return jsrest.Errorf(jsrest.ErrBadRequest, "%s: %s (%w)", h, r.Header.Get(h), ErrHeaderMismatch)
|
||||
}</span>
|
||||
}
|
||||
|
||||
<span class="cov5" title="4">h := sha256.New()
|
||||
|
||||
_, err := io.Copy(h, r.Body)
|
||||
if err != nil </span><span class="cov0" title="0">{
|
||||
return jsrest.Errorf(jsrest.ErrBadRequest, "hash request body failed (%w)", err)
|
||||
}</span>
|
||||
|
||||
<span class="cov5" title="4">sha256 := h.Sum(nil)
|
||||
if !bytes.Equal(sha256, saved.sha256) </span><span class="cov1" title="1">{
|
||||
return jsrest.Errorf(jsrest.ErrBadRequest, "%s vs %s (%w)", sha256, saved.sha256, ErrBodyMismatch)
|
||||
}</span>
|
||||
|
||||
<span class="cov4" title="3">for key, vals := range saved.responseHeader </span><span class="cov4" title="3">{
|
||||
w.Header().Set(key, vals[0])
|
||||
}</span>
|
||||
|
||||
<span class="cov4" title="3">w.WriteHeader(saved.statusCode)
|
||||
_, _ = w.Write(saved.responseBody)
|
||||
|
||||
return nil</span>
|
||||
}
|
||||
|
||||
// Store miss, proceed to normal execution with interception
|
||||
<span class="cov7" title="6">err := p.lockKey(key)
|
||||
if err != nil </span><span class="cov0" title="0">{
|
||||
return jsrest.Errorf(jsrest.ErrConflict, "%s", key)
|
||||
}</span>
|
||||
|
||||
<span class="cov7" title="6">defer p.unlockKey(key)
|
||||
|
||||
requestHeader := http.Header{}
|
||||
for _, h := range criticalHeaders </span><span class="cov9" title="12">{
|
||||
requestHeader.Set(h, r.Header.Get(h))
|
||||
}</span>
|
||||
|
||||
<span class="cov7" title="6">bi := newBodyIntercept(r.Body)
|
||||
r.Body = bi
|
||||
|
||||
rwi := newResponseWriterIntercept(w)
|
||||
w = rwi
|
||||
|
||||
p.handler.ServeHTTP(w, r)
|
||||
|
||||
save := &savedResult{
|
||||
key: key,
|
||||
|
||||
method: r.Method,
|
||||
url: r.URL.String(),
|
||||
requestHeader: requestHeader,
|
||||
sha256: bi.sha256.Sum(nil),
|
||||
|
||||
statusCode: rwi.statusCode,
|
||||
responseHeader: rwi.Header(),
|
||||
responseBody: rwi.buf.Bytes(),
|
||||
}
|
||||
|
||||
p.write(save)
|
||||
|
||||
return nil</span>
|
||||
}
|
||||
|
||||
func (p *Potency) lockKey(key string) error <span class="cov7" title="6">{
|
||||
p.inProgressMu.Lock()
|
||||
defer p.inProgressMu.Unlock()
|
||||
|
||||
if p.inProgress[key] </span><span class="cov0" title="0">{
|
||||
return ErrConflict
|
||||
}</span>
|
||||
|
||||
<span class="cov7" title="6">p.inProgress[key] = true
|
||||
|
||||
return nil</span>
|
||||
}
|
||||
|
||||
func (p *Potency) unlockKey(key string) <span class="cov7" title="6">{
|
||||
p.inProgressMu.Lock()
|
||||
defer p.inProgressMu.Unlock()
|
||||
|
||||
delete(p.inProgress, key)
|
||||
}</span>
|
||||
|
||||
func (p *Potency) read(key string) *savedResult <span class="cov10" title="14">{
|
||||
p.cacheMu.RLock()
|
||||
defer p.cacheMu.RUnlock()
|
||||
|
||||
return p.cache[key]
|
||||
}</span>
|
||||
|
||||
func (p *Potency) write(sr *savedResult) <span class="cov7" title="6">{
|
||||
p.cacheMu.Lock()
|
||||
defer p.cacheMu.Unlock()
|
||||
|
||||
sr.added = time.Now()
|
||||
|
||||
p.cache[sr.key] = sr
|
||||
|
||||
if p.cacheNewest != nil </span><span class="cov4" title="3">{
|
||||
p.cacheNewest.newer = sr
|
||||
}</span>
|
||||
|
||||
<span class="cov7" title="6">p.cacheNewest = sr
|
||||
|
||||
if p.cacheOldest == nil </span><span class="cov4" title="3">{
|
||||
p.cacheOldest = sr
|
||||
}</span>
|
||||
|
||||
<span class="cov7" title="6">p.removeExpired()</span>
|
||||
}
|
||||
|
||||
func (p *Potency) removeExpired() <span class="cov7" title="6">{
|
||||
cutoff := time.Now().Add(-1 * p.lifetime)
|
||||
|
||||
for iter := p.cacheOldest; iter != nil && iter.added.Before(cutoff); iter = iter.newer </span><span class="cov3" title="2">{
|
||||
delete(p.cache, iter.key)
|
||||
p.cacheOldest = iter
|
||||
}</span>
|
||||
}
|
||||
</pre>
|
||||
|
||||
<pre class="file" id="file2" style="display: none">package potency
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type responseWriterIntercept struct {
|
||||
dest http.ResponseWriter
|
||||
buf bytes.Buffer
|
||||
statusCode int
|
||||
}
|
||||
|
||||
func newResponseWriterIntercept(dest http.ResponseWriter) *responseWriterIntercept <span class="cov7" title="6">{
|
||||
return &responseWriterIntercept{
|
||||
dest: dest,
|
||||
buf: bytes.Buffer{},
|
||||
statusCode: http.StatusOK,
|
||||
}
|
||||
}</span>
|
||||
|
||||
func (rwi *responseWriterIntercept) Header() http.Header <span class="cov10" title="12">{
|
||||
return rwi.dest.Header()
|
||||
}</span>
|
||||
|
||||
func (rwi *responseWriterIntercept) Write(data []byte) (int, error) <span class="cov7" title="6">{
|
||||
rwi.buf.Write(data)
|
||||
return rwi.dest.Write(data)
|
||||
}</span>
|
||||
|
||||
func (rwi *responseWriterIntercept) WriteHeader(statusCode int) <span class="cov0" title="0">{
|
||||
rwi.statusCode = statusCode
|
||||
rwi.dest.WriteHeader(statusCode)
|
||||
}</span>
|
||||
</pre>
|
||||
|
||||
</div>
|
||||
</body>
|
||||
<script>
|
||||
(function() {
|
||||
var files = document.getElementById('files');
|
||||
var visible;
|
||||
files.addEventListener('change', onChange, false);
|
||||
function select(part) {
|
||||
if (visible)
|
||||
visible.style.display = 'none';
|
||||
visible = document.getElementById(part);
|
||||
if (!visible)
|
||||
return;
|
||||
files.value = part;
|
||||
visible.style.display = 'block';
|
||||
location.hash = part;
|
||||
}
|
||||
function onChange() {
|
||||
select(files.value);
|
||||
window.scrollTo(0, 0);
|
||||
}
|
||||
if (location.hash != "") {
|
||||
select(location.hash.substr(1));
|
||||
}
|
||||
if (!visible) {
|
||||
select("file0");
|
||||
}
|
||||
})();
|
||||
</script>
|
||||
</html>
|
||||
49
cover.out
Normal file
49
cover.out
Normal file
@@ -0,0 +1,49 @@
|
||||
mode: atomic
|
||||
github.com/gopatchy/potency/bodyintercept.go:14.60,19.2 1 6
|
||||
github.com/gopatchy/potency/bodyintercept.go:21.54,26.2 3 6
|
||||
github.com/gopatchy/potency/bodyintercept.go:28.40,30.2 1 0
|
||||
github.com/gopatchy/potency/potency.go:62.48,69.2 1 3
|
||||
github.com/gopatchy/potency/potency.go:71.69,73.15 2 14
|
||||
github.com/gopatchy/potency/potency.go:73.15,76.3 2 0
|
||||
github.com/gopatchy/potency/potency.go:78.2,79.16 2 14
|
||||
github.com/gopatchy/potency/potency.go:79.16,81.3 1 5
|
||||
github.com/gopatchy/potency/potency.go:84.55,89.2 3 1
|
||||
github.com/gopatchy/potency/potency.go:91.35,96.2 3 4
|
||||
github.com/gopatchy/potency/potency.go:98.87,99.82 1 14
|
||||
github.com/gopatchy/potency/potency.go:99.82,101.3 1 0
|
||||
github.com/gopatchy/potency/potency.go:103.2,107.18 3 14
|
||||
github.com/gopatchy/potency/potency.go:107.18,108.31 1 8
|
||||
github.com/gopatchy/potency/potency.go:108.31,110.4 1 1
|
||||
github.com/gopatchy/potency/potency.go:112.3,112.34 1 7
|
||||
github.com/gopatchy/potency/potency.go:112.34,114.4 1 1
|
||||
github.com/gopatchy/potency/potency.go:116.3,116.37 1 6
|
||||
github.com/gopatchy/potency/potency.go:116.37,117.53 1 11
|
||||
github.com/gopatchy/potency/potency.go:117.53,119.5 1 2
|
||||
github.com/gopatchy/potency/potency.go:122.3,125.17 3 4
|
||||
github.com/gopatchy/potency/potency.go:125.17,127.4 1 0
|
||||
github.com/gopatchy/potency/potency.go:129.3,130.41 2 4
|
||||
github.com/gopatchy/potency/potency.go:130.41,132.4 1 1
|
||||
github.com/gopatchy/potency/potency.go:134.3,134.47 1 3
|
||||
github.com/gopatchy/potency/potency.go:134.47,136.4 1 3
|
||||
github.com/gopatchy/potency/potency.go:138.3,141.13 3 3
|
||||
github.com/gopatchy/potency/potency.go:145.2,146.16 2 6
|
||||
github.com/gopatchy/potency/potency.go:146.16,148.3 1 0
|
||||
github.com/gopatchy/potency/potency.go:150.2,153.36 3 6
|
||||
github.com/gopatchy/potency/potency.go:153.36,155.3 1 12
|
||||
github.com/gopatchy/potency/potency.go:157.2,180.12 8 6
|
||||
github.com/gopatchy/potency/potency.go:183.45,187.23 3 6
|
||||
github.com/gopatchy/potency/potency.go:187.23,189.3 1 0
|
||||
github.com/gopatchy/potency/potency.go:191.2,193.12 2 6
|
||||
github.com/gopatchy/potency/potency.go:196.41,201.2 3 6
|
||||
github.com/gopatchy/potency/potency.go:203.49,208.2 3 14
|
||||
github.com/gopatchy/potency/potency.go:210.42,218.26 5 6
|
||||
github.com/gopatchy/potency/potency.go:218.26,220.3 1 3
|
||||
github.com/gopatchy/potency/potency.go:222.2,224.26 2 6
|
||||
github.com/gopatchy/potency/potency.go:224.26,226.3 1 3
|
||||
github.com/gopatchy/potency/potency.go:228.2,228.19 1 6
|
||||
github.com/gopatchy/potency/potency.go:231.35,234.89 2 6
|
||||
github.com/gopatchy/potency/potency.go:234.89,237.3 2 2
|
||||
github.com/gopatchy/potency/responsewriterintercept.go:14.84,20.2 1 6
|
||||
github.com/gopatchy/potency/responsewriterintercept.go:22.58,24.2 1 12
|
||||
github.com/gopatchy/potency/responsewriterintercept.go:26.69,29.2 2 6
|
||||
github.com/gopatchy/potency/responsewriterintercept.go:31.65,34.2 2 0
|
||||
Reference in New Issue
Block a user