forked from prashanthpai/sqlcache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
interceptor.go
216 lines (184 loc) · 5.55 KB
/
interceptor.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package sqlcache
import (
"context"
"database/sql/driver"
"fmt"
"sync/atomic"
"time"
"github.com/ngrok/sqlmw"
"github.com/prashanthpai/sqlcache/cache"
)
// Config is the configuration passed to NewInterceptor for creating new
// Interceptor instances.
type Config struct {
// Cache must be set to a type that implements the cache.Cacher interface
// which abstracts the backend cache implementation. This is a required
// field and cannot be nil.
Cache cache.Cacher
// OnError is called whenever methods of cache.Cacher interface or HashFunc
// returns error. Since sqlcache package does not log any failures, you can
// use this hook to log errors or even choose to disable/bypass sqlcache.
OnError func(error)
// HashFunc can be optionally set to provide a custom hashing function. By
// default sqlcache uses mitchellh/hashstructure which internally uses FNV.
// If hash collision is a concern to you, consider using NoopHash.
HashFunc func(query string, args []driver.NamedValue) (string, error)
}
// Interceptor is a ngrok/sqlmw interceptor that caches SQL queries and
// their responses.
type Interceptor struct {
c cache.Cacher
hashFunc func(query string, args []driver.NamedValue) (string, error)
onErr func(error)
stats Stats
disabled bool
sqlmw.NullInterceptor
}
// NewInterceptor returns a new instance of sqlcache interceptor initialised
// with the provided config.
func NewInterceptor(config *Config) (*Interceptor, error) {
if config == nil {
return nil, fmt.Errorf("config can't be nil")
}
if config.Cache == nil {
return nil, fmt.Errorf("cache must be set in Config")
}
if config.HashFunc == nil {
config.HashFunc = defaultHashFunc
}
return &Interceptor{
config.Cache,
config.HashFunc,
config.OnError,
Stats{},
false,
sqlmw.NullInterceptor{},
}, nil
}
// Driver returns the supplied driver.Driver with a new object that has
// all of its calls intercepted by the sqlcache.Interceptor. Any DB call
// without a context passed will not be intercepted.
func (i *Interceptor) Driver(d driver.Driver) driver.Driver {
return sqlmw.Driver(d, i)
}
// Enable enables the interceptor. Interceptor instance is enabled by default
// on creation.
func (i *Interceptor) Enable() {
i.disabled = false
}
// Disable disables the interceptor resulting in cache bypass. All queries
// would go directly to the SQL backend.
func (i *Interceptor) Disable() {
i.disabled = true
}
// StmtQueryContext intecepts database/sql's stmt.QueryContext calls from a prepared statement.
func (i *Interceptor) StmtQueryContext(ctx context.Context, conn driver.StmtQueryContext, query string, args []driver.NamedValue) (context.Context, driver.Rows, error) {
if i.disabled {
rows, err := conn.QueryContext(ctx, args)
return ctx, rows, err
}
attrs := getAttrs(query)
if attrs == nil {
rows, err := conn.QueryContext(ctx, args)
return ctx, rows, err
}
hash, err := i.hashFunc(query, args)
if err != nil {
atomic.AddUint64(&i.stats.Errors, 1)
if i.onErr != nil {
i.onErr(fmt.Errorf("HashFunc failed: %w", err))
}
rows, err := conn.QueryContext(ctx, args)
return ctx, rows, err
}
if cached := i.checkCache(ctx, hash); cached != nil {
return ctx, cached, nil
}
rows, err := conn.QueryContext(ctx, args)
if err != nil {
return ctx, rows, err
}
cacheSetter := func(item *cache.Item) {
err := i.c.Set(ctx, hash, item, time.Duration(attrs.ttl)*time.Second)
if err != nil {
atomic.AddUint64(&i.stats.Errors, 1)
if i.onErr != nil {
i.onErr(fmt.Errorf("Cache.Set failed: %w", err))
}
}
}
rows = newRowsRecorder(cacheSetter, rows, attrs.maxRows)
return ctx, rows, err
}
// ConnQueryContext intecepts database/sql's DB.QueryContext Conn.QueryContext calls.
func (i *Interceptor) ConnQueryContext(ctx context.Context, conn driver.QueryerContext, query string, args []driver.NamedValue) (context.Context, driver.Rows, error) {
if i.disabled {
rows, err := conn.QueryContext(ctx, query, args)
return ctx, rows, err
}
attrs := getAttrs(query)
if attrs == nil {
rows, err := conn.QueryContext(ctx, query, args)
return ctx, rows, err
}
hash, err := i.hashFunc(query, args)
if err != nil {
atomic.AddUint64(&i.stats.Errors, 1)
if i.onErr != nil {
i.onErr(fmt.Errorf("HashFunc failed: %w", err))
}
rows, err := conn.QueryContext(ctx, query, args)
return ctx, rows, err
}
if cached := i.checkCache(ctx, hash); cached != nil {
return ctx, cached, nil
}
rows, err := conn.QueryContext(ctx, query, args)
if err != nil {
return ctx, rows, err
}
cacheSetter := func(item *cache.Item) {
err := i.c.Set(ctx, hash, item, time.Duration(attrs.ttl)*time.Second)
if err != nil {
atomic.AddUint64(&i.stats.Errors, 1)
if i.onErr != nil {
i.onErr(fmt.Errorf("Cache.Set failed: %w", err))
}
}
}
rows = newRowsRecorder(cacheSetter, rows, attrs.maxRows)
return ctx, rows, err
}
func (i *Interceptor) checkCache(ctx context.Context, hash string) driver.Rows {
item, ok, err := i.c.Get(ctx, hash)
if err != nil {
atomic.AddUint64(&i.stats.Errors, 1)
if i.onErr != nil {
i.onErr(fmt.Errorf("Cache.Get failed: %w", err))
}
return nil
}
if !ok {
atomic.AddUint64(&i.stats.Misses, 1)
return nil
}
atomic.AddUint64(&i.stats.Hits, 1)
return &rowsCached{
item,
0,
}
}
// Stats contains sqlcache statistics.
type Stats struct {
Hits uint64
Misses uint64
Errors uint64
}
// Stats returns sqlcache stats.
func (i *Interceptor) Stats() *Stats {
return &Stats{
Hits: atomic.LoadUint64(&i.stats.Hits),
Misses: atomic.LoadUint64(&i.stats.Misses),
Errors: atomic.LoadUint64(&i.stats.Errors),
}
}