-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
131 lines (114 loc) · 3.54 KB
/
Copy pathconfig.go
File metadata and controls
131 lines (114 loc) · 3.54 KB
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
package sqliteproxy
import (
"fmt"
"log/slog"
"sort"
"time"
)
// SQLiteProxyOption configures the SQLiteProxy
type SQLiteProxyOption func(*SQLiteProxy)
// WithDSNResolver sets a custom DSN resolver function
func WithDSNResolver(resolver func(orgID, baseDsnInfo string) string) SQLiteProxyOption {
return func(p *SQLiteProxy) {
p.dsnResolver = resolver
}
}
// WithOrgIDKey sets the context key used to extract organization ID
func WithOrgIDKey(key OrgIDKey) SQLiteProxyOption {
return func(p *SQLiteProxy) {
p.orgIDKey = key
}
}
// WithMaxOpenConns sets the maximum number of open connections per database
func WithMaxOpenConns(n int) SQLiteProxyOption {
return func(p *SQLiteProxy) {
p.maxOpenConns = n
}
}
// WithMaxIdleConns sets the maximum number of idle connections per database
func WithMaxIdleConns(n int) SQLiteProxyOption {
return func(p *SQLiteProxy) {
p.maxIdleConns = n
}
}
// WithConnMaxLifetime sets the maximum lifetime of connections
func WithConnMaxLifetime(d time.Duration) SQLiteProxyOption {
return func(p *SQLiteProxy) {
p.connMaxLife = d
}
}
// WithConnMaxIdleTime sets the maximum idle time for connections
func WithConnMaxIdleTime(d time.Duration) SQLiteProxyOption {
return func(p *SQLiteProxy) {
p.connMaxIdle = d
}
}
// WithEviction enables connection eviction with the specified parameters
func WithEviction(maxConnections int, evictionInterval, idleTimeout time.Duration) SQLiteProxyOption {
return func(p *SQLiteProxy) {
p.evictionEnabled = true
p.maxConnections = maxConnections
p.evictionInterval = evictionInterval
p.idleTimeout = idleTimeout
}
}
// WithResourceLimits sets global resource limits
func WithResourceLimits(maxTotalConnections int64, maxMemoryPerTenant, maxTotalMemory int64) SQLiteProxyOption {
return func(p *SQLiteProxy) {
p.maxTotalConnections = maxTotalConnections
p.maxMemoryPerTenant = maxMemoryPerTenant
p.maxTotalMemory = maxTotalMemory
}
}
// WithLogger sets a custom structured logger
func WithLogger(logger *slog.Logger) SQLiteProxyOption {
return func(p *SQLiteProxy) {
p.logger = logger
}
}
// WithShutdownTimeout sets the graceful shutdown timeout
func WithShutdownTimeout(timeout time.Duration) SQLiteProxyOption {
return func(p *SQLiteProxy) {
p.shutdownTimeout = timeout
}
}
// WithQueryTimeout sets a timeout that applies to all database operations
// If timeout is 0, no timeout is applied (database/sql default behavior)
func WithQueryTimeout(timeout time.Duration) SQLiteProxyOption {
return func(p *SQLiteProxy) {
p.queryTimeout = timeout
}
}
// WithMigrations configures the proxy with schema migrations
func WithMigrations(migrations ...Migration) SQLiteProxyOption {
return func(p *SQLiteProxy) {
// Sort migrations by version
sorted := make([]Migration, len(migrations))
copy(sorted, migrations)
sort.Slice(sorted, func(i, j int) bool {
return sorted[i].Version < sorted[j].Version
})
// Validate migrations
for i, m := range sorted {
if m.Version <= 0 {
panic(fmt.Sprintf("migration version must be positive, got %d", m.Version))
}
if i > 0 && sorted[i-1].Version == m.Version {
panic(fmt.Sprintf("duplicate migration version: %d", m.Version))
}
if m.Up == "" {
panic(fmt.Sprintf("migration %d has empty Up SQL", m.Version))
}
}
p.migrations = sorted
if len(sorted) > 0 {
p.targetSchemaVersion = sorted[len(sorted)-1].Version
}
}
}
// WithMigrationMode sets how migrations are applied
func WithMigrationMode(mode MigrationMode) SQLiteProxyOption {
return func(p *SQLiteProxy) {
p.migrationMode = mode
}
}