-
Notifications
You must be signed in to change notification settings - Fork 117
/
runtime.go
63 lines (55 loc) · 1.41 KB
/
runtime.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
package runtime
import (
"context"
"fmt"
"github.com/rilldata/rill/runtime/drivers"
"go.uber.org/zap"
)
type Options struct {
ConnectionCacheSize int
MetastoreDriver string
MetastoreDSN string
QueryCacheSize int
AllowHostCredentials bool
SafeSourceRefresh bool
}
type Runtime struct {
opts *Options
metastore drivers.Connection
logger *zap.Logger
connCache *connectionCache
catalogCache *catalogCache
queryCache *queryCache
}
func New(opts *Options, logger *zap.Logger) (*Runtime, error) {
// Open metadata db connection
metastore, err := drivers.Open(opts.MetastoreDriver, opts.MetastoreDSN, logger)
if err != nil {
return nil, fmt.Errorf("could not connect to metadata db: %w", err)
}
err = metastore.Migrate(context.Background())
if err != nil {
return nil, fmt.Errorf("metadata db migration: %w", err)
}
// Check the metastore is a registry
_, ok := metastore.RegistryStore()
if !ok {
return nil, fmt.Errorf("server metastore must be a valid registry")
}
return &Runtime{
opts: opts,
metastore: metastore,
logger: logger,
connCache: newConnectionCache(opts.ConnectionCacheSize, logger),
catalogCache: newCatalogCache(),
queryCache: newQueryCache(opts.QueryCacheSize),
}, nil
}
func (r *Runtime) Close() error {
err1 := r.metastore.Close()
err2 := r.connCache.Close()
if err1 != nil {
return err1
}
return err2
}