-
Notifications
You must be signed in to change notification settings - Fork 26
/
cacheRecorder.go
106 lines (88 loc) · 1.86 KB
/
cacheRecorder.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package main
import (
"crypto/sha256"
"fmt"
"net/http"
"sync"
"unsafe"
)
// cacheRecorder is a thread-safe implementation of http.ResponseWriter
type cacheRecorder struct {
mu sync.Mutex
item cacheItem
done bool
}
type cacheItem struct {
expiration int
eTag string
code int
header http.Header
body []byte
}
func newCacheRecorder() *cacheRecorder {
return &cacheRecorder{
item: cacheItem{
code: http.StatusOK,
header: make(http.Header),
},
}
}
func (c *cacheRecorder) finish() *cacheItem {
c.mu.Lock()
defer c.mu.Unlock()
if c.done {
return &c.item
}
c.done = true
c.item.eTag = c.item.header.Get("ETag")
if c.item.eTag == "" {
c.item.eTag = fmt.Sprintf("%x", sha256.Sum256(c.item.body))
}
return &c.item
}
// Header implements http.ResponseWriter.
func (c *cacheRecorder) Header() http.Header {
c.mu.Lock()
defer c.mu.Unlock()
if c.done {
return nil
}
return c.item.header
}
// Write implements http.ResponseWriter.
func (c *cacheRecorder) Write(buf []byte) (int, error) {
c.mu.Lock()
defer c.mu.Unlock()
if c.done {
return 0, fmt.Errorf("write after finish")
}
c.item.body = append(c.item.body, buf...)
return len(buf), nil
}
// WriteString implements io.StringWriter.
func (c *cacheRecorder) WriteString(str string) (int, error) {
return c.Write([]byte(str))
}
// WriteHeader implements http.ResponseWriter.
func (c *cacheRecorder) WriteHeader(code int) {
c.mu.Lock()
defer c.mu.Unlock()
if c.done {
return
}
c.item.code = code
}
func (ci *cacheItem) cost() int64 {
size := int64(unsafe.Sizeof(*ci)) // Base struct size
// Add sizes of variable-length fields
size += int64(len(ci.eTag))
size += int64(len(ci.body))
// Calculate header size
for key, values := range ci.header {
size += int64(len(key))
for _, value := range values {
size += int64(len(value))
}
}
return size
}