Skip to content

Commit fee46ea

Browse files
committed
Updated plugin
1 parent 9432a3b commit fee46ea

File tree

4 files changed

+76
-18
lines changed

4 files changed

+76
-18
lines changed

pkg/sqlite3/pool.go

+12-1
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@ func (cfg PoolConfig) WithTrace(fn TraceFunc) PoolConfig {
8080
return cfg
8181
}
8282

83+
// Enable or disable creation of database files
84+
func (cfg PoolConfig) WithCreate(create bool) PoolConfig {
85+
cfg.Create = create
86+
return cfg
87+
}
88+
8389
// Set maxmimum concurrent connections
8490
func (cfg PoolConfig) WithMaxConnections(n int) PoolConfig {
8591
if n >= 0 {
@@ -184,7 +190,7 @@ func (p *Pool) String() string {
184190
str += fmt.Sprintf(" ver=%q", Version())
185191
str += fmt.Sprint(" flags=", p.cfg.Flags)
186192
str += fmt.Sprint(" cur=", p.Cur())
187-
str += fmt.Sprint(" max=", p.cfg.Max)
193+
str += fmt.Sprint(" max=", p.Max())
188194
for schema := range p.cfg.Schemas {
189195
str += fmt.Sprintf(" <schema %s=%q>", strings.TrimSpace(schema), p.pathForSchema(schema))
190196
}
@@ -217,6 +223,11 @@ func (p *Pool) Cur() int {
217223
return int(atomic.LoadInt32(&p.n))
218224
}
219225

226+
// Return maximum allowed connections
227+
func (p *Pool) Max() int {
228+
return int(p.cfg.Max)
229+
}
230+
220231
// Set maximum number of "checked out" connections
221232
func (p *Pool) SetMax(n int) {
222233
if n == 0 {

plugin/sqlite3/handlers.go

+8-11
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@ import (
2020
. "github.com/mutablelogic/go-server"
2121
. "github.com/mutablelogic/go-sqlite"
2222
. "github.com/mutablelogic/go-sqlite/pkg/lang"
23-
24-
// Some sort of hack
25-
_ "gopkg.in/yaml.v3"
2623
)
2724

2825
///////////////////////////////////////////////////////////////////////////////
@@ -36,8 +33,8 @@ type PingResponse struct {
3633
}
3734

3835
type PoolResponse struct {
39-
Cur int32 `json:"cur"`
40-
Max int32 `json:"max"`
36+
Cur int `json:"cur"`
37+
Max int `json:"max"`
4138
}
4239

4340
type SchemaResponse struct {
@@ -146,7 +143,7 @@ func (p *plugin) AddHandlers(ctx context.Context, provider Provider) error {
146143

147144
func (p *plugin) ServePing(w http.ResponseWriter, req *http.Request) {
148145
// Get a connection
149-
conn := p.Get(req.Context())
146+
conn := p.Get()
150147
if conn == nil {
151148
router.ServeError(w, http.StatusBadGateway, "No connection")
152149
return
@@ -161,7 +158,7 @@ func (p *plugin) ServePing(w http.ResponseWriter, req *http.Request) {
161158
response.Version = sqlite3.Version()
162159
response.Schemas = append(response.Schemas, conn.Schemas()...)
163160
response.Modules = append(response.Modules, conn.Modules()...)
164-
response.Pool = PoolResponse{Cur: p.Cur(), Max: p.Max()}
161+
response.Pool = PoolResponse{Cur: p.pool.Cur(), Max: p.pool.Max()}
165162

166163
// Serve response
167164
router.ServeJSON(w, response, http.StatusOK, 2)
@@ -172,7 +169,7 @@ func (p *plugin) ServeSchema(w http.ResponseWriter, req *http.Request) {
172169
params := router.RequestParams(req)
173170

174171
// Get a connection
175-
conn := p.Get(req.Context())
172+
conn := p.Get()
176173
if conn == nil {
177174
router.ServeError(w, http.StatusBadGateway, "No connection")
178175
return
@@ -240,7 +237,7 @@ func (p *plugin) ServeTable(w http.ResponseWriter, req *http.Request) {
240237
}
241238

242239
// Get a connection
243-
conn := p.Get(req.Context())
240+
conn := p.Get()
244241
if conn == nil {
245242
router.ServeError(w, http.StatusBadGateway, "No connection")
246243
return
@@ -293,7 +290,7 @@ func (p *plugin) ServeTokenizer(w http.ResponseWriter, req *http.Request) {
293290
}
294291

295292
// Get a connection
296-
conn := p.Get(req.Context())
293+
conn := p.Get()
297294
if conn == nil {
298295
router.ServeError(w, http.StatusBadGateway, "No connection")
299296
return
@@ -326,7 +323,7 @@ func (p *plugin) ServeQuery(w http.ResponseWriter, req *http.Request) {
326323
}
327324

328325
// Get a connection
329-
conn := p.Get(req.Context())
326+
conn := p.Get()
330327
if conn == nil {
331328
router.ServeError(w, http.StatusBadGateway, "No connection")
332329
return

plugin/sqlite3/main.go

+53-6
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,31 @@ package main
33
import (
44
"context"
55
"fmt"
6+
"time"
67

78
// Packages
89
sqlite3 "github.com/mutablelogic/go-sqlite/pkg/sqlite3"
910

1011
// Namespace imports
1112
. "github.com/mutablelogic/go-server"
1213
. "github.com/mutablelogic/go-sqlite"
14+
15+
// Some sort of hack
16+
_ "gopkg.in/yaml.v3"
1317
)
1418

1519
///////////////////////////////////////////////////////////////////////////////
1620
// TYPES
1721

22+
type Config struct {
23+
Databases map[string]string `yaml:"databases"`
24+
Max int `yaml:"max"`
25+
Create bool `yaml:"create"`
26+
Trace bool `yaml:"trace"`
27+
}
28+
1829
type plugin struct {
19-
SQPool
30+
pool SQPool
2031
errs chan error
2132
}
2233

@@ -28,21 +39,42 @@ func New(ctx context.Context, provider Provider) Plugin {
2839
p := new(plugin)
2940

3041
// Get configuration
31-
cfg := sqlite3.PoolConfig{}
42+
var cfg Config
3243
if err := provider.GetConfig(ctx, &cfg); err != nil {
3344
provider.Print(ctx, err)
3445
return nil
3546
}
47+
// Check for databases
48+
if len(cfg.Databases) == 0 {
49+
provider.Print(ctx, fmt.Errorf("no databases defined"))
50+
return nil
51+
}
52+
// Create the pool
53+
poolcfg := sqlite3.NewConfig().
54+
WithMaxConnections(cfg.Max).
55+
WithCreate(cfg.Create)
56+
for name, path := range cfg.Databases {
57+
poolcfg = poolcfg.WithSchema(name, path)
58+
}
59+
if cfg.Trace {
60+
poolcfg = poolcfg.WithTrace(func(q string, d time.Duration) {
61+
if d >= 0 {
62+
provider.Printf(ctx, "TRACE %q => %v", q, d)
63+
}
64+
})
65+
}
3666

3767
// Create a channel for errors
3868
p.errs = make(chan error)
3969

4070
// Create a pool
41-
if pool, err := sqlite3.OpenPool(cfg, p.errs); err != nil {
71+
if pool, err := sqlite3.OpenPool(poolcfg, p.errs); err != nil {
4272
provider.Print(ctx, err)
73+
close(p.errs)
74+
p.errs = nil
4375
return nil
4476
} else {
45-
p.SQPool = pool
77+
p.pool = pool
4678
}
4779

4880
// Return success
@@ -53,7 +85,11 @@ func New(ctx context.Context, provider Provider) Plugin {
5385
// STRINGIFY
5486

5587
func (p *plugin) String() string {
56-
return fmt.Sprint(p.SQPool)
88+
str := "<sqlite3"
89+
if p.pool != nil {
90+
str += fmt.Sprint(" ", p.pool)
91+
}
92+
return str + ">"
5793
}
5894

5995
///////////////////////////////////////////////////////////////////////////////
@@ -83,7 +119,7 @@ FOR_LOOP:
83119
}
84120

85121
// Close the pool
86-
if err := p.SQPool.Close(); err != nil {
122+
if err := p.pool.Close(); err != nil {
87123
provider.Print(ctx, err)
88124
}
89125

@@ -93,3 +129,14 @@ FOR_LOOP:
93129
// Return success
94130
return nil
95131
}
132+
133+
///////////////////////////////////////////////////////////////////////////////
134+
// PUBLIC METHODS - PUT AND GET
135+
136+
func (p *plugin) Get() SQConnection {
137+
return p.pool.Get()
138+
}
139+
140+
func (p *plugin) Put(conn SQConnection) {
141+
p.pool.Put(conn)
142+
}

sqlite.go

+3
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ type SQPool interface {
3939
// Cur returns the current number of used connections
4040
Cur() int
4141

42+
// Max returns the maximum allowed number of used connections
43+
Max() int
44+
4245
// SetMax allowed connections released from pool. Note this does not change
4346
// the maximum instantly, it will settle to this value over time.
4447
SetMax(int)

0 commit comments

Comments
 (0)