Skip to content

Commit

Permalink
Tidying
Browse files Browse the repository at this point in the history
  • Loading branch information
tinyzimmer committed Jul 21, 2023
1 parent e5ea3fc commit 8cc97d3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
28 changes: 14 additions & 14 deletions pkg/store/lockable_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,26 @@ limitations under the License.
package store

import (
"context"
"database/sql"
"fmt"
"sync"

"golang.org/x/exp/slog"

"github.com/webmeshproj/node/pkg/context"
"github.com/webmeshproj/node/pkg/meshdb/raftlogs"
)

var ErrStatementNotReadOnly = fmt.Errorf("statement is not read-only")

type roLockableDB struct {
db *sql.DB
mux sync.Locker
db *sql.DB
mu sync.Locker
}

func (d *roLockableDB) ExecContext(ctx context.Context, stmt string, args ...any) (sql.Result, error) {
d.mux.Lock()
defer d.mux.Unlock()
d.mu.Lock()
defer d.mu.Unlock()
readonly, err := raftlogs.IsReadOnlyStatement(ctx, d.db, stmt)
if err != nil {
return nil, fmt.Errorf("exec, check readonly: %w", err)
Expand All @@ -48,11 +48,11 @@ func (d *roLockableDB) ExecContext(ctx context.Context, stmt string, args ...any
}

func (d *roLockableDB) PrepareContext(ctx context.Context, stmt string) (*sql.Stmt, error) {
d.mux.Lock()
defer d.mux.Unlock()
d.mu.Lock()
defer d.mu.Unlock()
readonly, err := raftlogs.IsReadOnlyStatement(ctx, d.db, stmt)
if err != nil {
return nil, fmt.Errorf("query, check readonly: %w", err)
return nil, fmt.Errorf("prepare, check readonly: %w", err)
}
if !readonly {
return nil, ErrStatementNotReadOnly
Expand All @@ -61,8 +61,8 @@ func (d *roLockableDB) PrepareContext(ctx context.Context, stmt string) (*sql.St
}

func (d *roLockableDB) QueryContext(ctx context.Context, stmt string, args ...any) (*sql.Rows, error) {
d.mux.Lock()
defer d.mux.Unlock()
d.mu.Lock()
defer d.mu.Unlock()
readonly, err := raftlogs.IsReadOnlyStatement(ctx, d.db, stmt)
if err != nil {
return nil, fmt.Errorf("query, check readonly: %w", err)
Expand All @@ -74,16 +74,16 @@ func (d *roLockableDB) QueryContext(ctx context.Context, stmt string, args ...an
}

func (d *roLockableDB) QueryRowContext(ctx context.Context, stmt string, args ...any) *sql.Row {
d.mux.Lock()
defer d.mux.Unlock()
d.mu.Lock()
defer d.mu.Unlock()
readonly, err := raftlogs.IsReadOnlyStatement(ctx, d.db, stmt)
if err != nil {
// send a bad query
slog.Default().Error("query, check readonly", slog.String("error", err.Error()))
context.LoggerFrom(ctx).Error("query row, check readonly", slog.String("error", err.Error()))
return d.db.QueryRowContext(ctx, "SELECT 1 WHERE 0")
} else if !readonly {
// send a bad query
slog.Default().Error("query, check readonly", slog.String("error", ErrStatementNotReadOnly.Error()))
context.LoggerFrom(ctx).Error("query row, check readonly", slog.String("error", ErrStatementNotReadOnly.Error()))
return d.db.QueryRowContext(ctx, "SELECT 1 WHERE 0")
}
return d.db.QueryRowContext(ctx, stmt, args...)
Expand Down
2 changes: 1 addition & 1 deletion pkg/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ func (s *store) WriteDB() meshdb.DBTX {

// ReadDB returns a DB interface for use by the application.
func (s *store) ReadDB() meshdb.DBTX {
return &roLockableDB{db: s.weakData, mux: s.dataMux.RLocker()}
return &roLockableDB{db: s.weakData, mu: s.dataMux.RLocker()}
}

// Raft returns the Raft interface.
Expand Down

0 comments on commit 8cc97d3

Please sign in to comment.