-
Notifications
You must be signed in to change notification settings - Fork 111
/
caches.go
163 lines (135 loc) · 3.58 KB
/
caches.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
package runtime
import (
"context"
"errors"
"sync"
lru "github.com/hashicorp/golang-lru"
"github.com/hashicorp/golang-lru/simplelru"
"github.com/rilldata/rill/runtime/drivers"
"github.com/rilldata/rill/runtime/services/catalog"
"go.uber.org/zap"
)
var errConnectionCacheClosed = errors.New("connectionCache: closed")
// cache for instance specific connections only
// all instance specific connections should be opened via connection cache only
type connectionCache struct {
cache *simplelru.LRU
lock sync.Mutex
closed bool
logger *zap.Logger
}
func newConnectionCache(size int, logger *zap.Logger) *connectionCache {
cache, err := simplelru.NewLRU(size, func(key interface{}, value interface{}) {
// close the evicted connection
if err := value.(drivers.Connection).Close(); err != nil {
logger.Error("failed closing cached connection for ", zap.String("key", key.(string)), zap.Error(err))
}
})
if err != nil {
panic(err)
}
return &connectionCache{cache: cache, logger: logger}
}
func (c *connectionCache) Close() error {
c.lock.Lock()
if c.closed {
c.lock.Unlock()
return errConnectionCacheClosed
}
c.closed = true
c.lock.Unlock()
var firstErr error
for _, key := range c.cache.Keys() {
val, _ := c.cache.Get(key)
err := val.(drivers.Connection).Close()
if err != nil {
c.logger.Error("failed closing cached connection", zap.Error(err))
if firstErr == nil {
firstErr = err
}
}
}
return firstErr
}
func (c *connectionCache) get(ctx context.Context, instanceID, driver, dsn string) (drivers.Connection, error) {
// TODO: This locks for all instances for the duration of Open and Migrate.
// Adapt to lock only on the lookup, and then on the individual instance's Open and Migrate.
c.lock.Lock()
defer c.lock.Unlock()
if c.closed {
return nil, errConnectionCacheClosed
}
key := instanceID + driver + dsn
val, ok := c.cache.Get(key)
if !ok {
conn, err := drivers.Open(driver, dsn, c.logger)
if err != nil {
return nil, err
}
err = conn.Migrate(ctx)
if err != nil {
return nil, err
}
c.cache.Add(key, conn)
return conn, nil
}
return val.(drivers.Connection), nil
}
// evict removes the connection from cache and closes the connection
func (c *connectionCache) evict(ctx context.Context, instanceID, driver, dsn string) bool {
c.lock.Lock()
defer c.lock.Unlock()
if c.closed {
return false
}
key := instanceID + driver + dsn
conn, ok := c.cache.Get(key)
if ok {
// closing this would mean that any running query might also fail
conn.(drivers.Connection).Close()
c.cache.Remove(key)
}
return ok
}
type migrationMetaCache struct {
cache *simplelru.LRU
lock sync.Mutex
}
func newMigrationMetaCache(size int) *migrationMetaCache {
cache, err := simplelru.NewLRU(size, nil)
if err != nil {
panic(err)
}
return &migrationMetaCache{cache: cache}
}
func (c *migrationMetaCache) get(instID string) *catalog.MigrationMeta {
c.lock.Lock()
defer c.lock.Unlock()
if val, ok := c.cache.Get(instID); ok {
return val.(*catalog.MigrationMeta)
}
meta := catalog.NewMigrationMeta()
c.cache.Add(instID, meta)
return meta
}
func (c *migrationMetaCache) evict(ctx context.Context, instID string) {
c.lock.Lock()
defer c.lock.Unlock()
c.cache.Remove(instID)
}
type queryCache struct {
cache *lru.Cache
}
func newQueryCache(size int) *queryCache {
cache, err := lru.New(size)
if err != nil {
panic(err)
}
return &queryCache{cache: cache}
}
func (c *queryCache) get(key queryCacheKey) (any, bool) {
return c.cache.Get(key)
}
func (c *queryCache) add(key queryCacheKey, value any) bool {
return c.cache.Add(key, value)
}