Skip to content

experiment: always open stream over HTTP #167

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions internal/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"io"
"reflect"
"strings"
"sync"
"time"

"github.com/libsql/sqlite-antlr4-parser/sqliteparserutils"
_ "github.com/mattn/go-sqlite3"
Expand All @@ -28,6 +30,8 @@ type Db struct {
AuthToken string

sqlDb *sql.DB
conn *sql.Conn
connMutex *sync.Mutex
driver driver
urlScheme string

Expand Down Expand Up @@ -74,7 +78,7 @@ func newRowResultWithError(err error) *rowResult {
func NewDb(dbUri, authToken, proxy string) (*Db, error) {
var err error

var db = Db{Uri: dbUri, AuthToken: authToken}
var db = Db{Uri: dbUri, AuthToken: authToken, connMutex: &sync.Mutex{}}

if IsUrl(dbUri) {
var validSqldUrl bool
Expand All @@ -92,6 +96,10 @@ func NewDb(dbUri, authToken, proxy string) (*Db, error) {
return nil, err
}
db.sqlDb = sql.OpenDB(connector)
db.conn, err = db.sqlDb.Conn(context.Background())
if err != nil {
return nil, err
}
} else {
return nil, &shellerrors.ProtocolError{}
}
Expand All @@ -102,6 +110,19 @@ func NewDb(dbUri, authToken, proxy string) (*Db, error) {
if err != nil {
return nil, err
}
// every second acquire mutex and run a no op
go func() {
tick := time.NewTicker(time.Second)
for range tick.C {
db.connMutex.Lock()
_, err := db.conn.QueryContext(context.Background(), "SELECT 1;", nil)
if err != nil {
fmt.Printf("connection closed. restart your shell")
return
}
db.connMutex.Unlock()
}
}()
return &db, nil
}

Expand Down Expand Up @@ -160,7 +181,9 @@ func (db *Db) executeQuery(query string, statementResultCh chan StatementResult)
ctx, cancel := context.WithCancel(context.Background())
db.cancelRunningQuery = cancel

rows, err := db.sqlDb.QueryContext(ctx, query)
db.connMutex.Lock()
rows, err := db.conn.QueryContext(ctx, query)
db.connMutex.Unlock()
if err != nil {
statementResultCh <- *newStatementResultWithError(err)

Expand Down
Loading