SQLite allows having 1 set to MaxOpenConnections (#3941)
* let sqlite have a single concurrent connection
This commit is contained in:
1 parent
fba9d1cdf1
commit
5daef58f54
4 files changed
+33
-3
No files matched your search
@@ -6,4 +6,4 @@ Package cmd contains the command packages to the client cache. Adding
|
||||
these commands to the boundary cli is the only integration between the boundary
|
||||
binary and the client cache.
|
||||
*/
|
||||
package cmd
|
||||
package cmd
|
||||
@@ -5,6 +5,7 @@ package cache
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -506,3 +507,30 @@ func TestSession(t *testing.T) {
|
||||
assert.ErrorContains(t, rw.LookupById(ctx, lookSess), "not found")
|
||||
})
|
||||
}
|
||||
|
||||
func TestRepository_SqliteReadDuringTx(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
s, err := cachedb.Open(ctx)
|
||||
require.NoError(t, err)
|
||||
rw := db.New(s)
|
||||
|
||||
ret := AuthToken{
|
||||
Id: "something",
|
||||
}
|
||||
assert.ErrorContains(t, rw.LookupById(ctx, &ret), "not found")
|
||||
|
||||
var startTx sync.WaitGroup
|
||||
startTx.Add(1)
|
||||
go func() {
|
||||
_, err = rw.DoTx(ctx, db.StdRetryCnt, db.ExpBackoff{}, func(reader db.Reader, writer db.Writer) error {
|
||||
startTx.Done()
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
|
||||
assert.ErrorContains(t, reader.LookupById(ctx, &ret), "not found")
|
||||
return nil
|
||||
})
|
||||
}()
|
||||
// setup a read that potentially executes during a tx
|
||||
startTx.Wait()
|
||||
assert.ErrorContains(t, rw.LookupById(ctx, &ret), "not found")
|
||||
}
|
||||
@@ -34,7 +34,9 @@ func Open(ctx context.Context, opt ...Option) (*db.DB, error) {
|
||||
default:
|
||||
url = DefaultStoreUrl
|
||||
}
|
||||
conn, err := db.Open(ctx, db.Sqlite, url)
|
||||
// sqlite db must be set to 1 max open connection so requests can be serialized
|
||||
// otherwise reading while a tx is ongoing results in sql logic errors.
|
||||
conn, err := db.Open(ctx, db.Sqlite, url, db.WithMaxOpenConnections(1))
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
|
||||
+1
-1
@@ -174,7 +174,7 @@ func Open(ctx context.Context, dbType DbType, connectionUrl string, opt ...Optio
|
||||
wrappedOpts = append(wrappedOpts, dbw.WithLogger(opts.withGormFormatter))
|
||||
}
|
||||
if opts.withMaxOpenConnections > 0 {
|
||||
if opts.withMaxOpenConnections < 5 {
|
||||
if opts.withMaxOpenConnections < 5 && dbType != Sqlite {
|
||||
return nil, fmt.Errorf("max_open_connections cannot be below 5")
|
||||
}
|
||||
wrappedOpts = append(wrappedOpts, dbw.WithMaxOpenConnections(opts.withMaxOpenConnections))
|
||||
|
||||
Reference in new issue
Block a user