aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


server/database_metrics.go (2511B)


      1 package main
      2 
      3 import (
      4 	"context"
      5 	"database/sql/driver"
      6 	"sync/atomic"
      7 
      8 	"modernc.org/sqlite"
      9 )
     10 
     11 // Count executions at the driver boundary, including prepared transaction queries.
     12 // Transaction control and SQLite's internal trigger statements are not queries.
     13 type databaseConnector struct{ queries *atomic.Uint64 }
     14 
     15 func (connector databaseConnector) Driver() driver.Driver { return &sqlite.Driver{} }
     16 func (connector databaseConnector) Connect(ctx context.Context) (driver.Conn, error) {
     17 	connection, err := connector.Driver().Open("data/world.sqlite?_foreign_keys=on&_journal_mode=WAL&_synchronous=FULL&_busy_timeout=5000")
     18 	if err != nil {
     19 		return nil, err
     20 	}
     21 	return &databaseConnection{connection.(sqliteConnection), connector.queries}, nil
     22 }
     23 
     24 // Preserve the driver's context and connection-pool behavior while counting queries.
     25 type sqliteConnection interface {
     26 	driver.Conn
     27 	driver.ConnBeginTx
     28 	driver.ConnPrepareContext
     29 	driver.ExecerContext
     30 	driver.QueryerContext
     31 	driver.Pinger
     32 	driver.SessionResetter
     33 	driver.Validator
     34 	RegisterCommitHook(sqlite.CommitHookFn)
     35 }
     36 
     37 type databaseConnection struct {
     38 	sqliteConnection
     39 	queries *atomic.Uint64
     40 }
     41 
     42 func (connection *databaseConnection) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) {
     43 	connection.queries.Add(1)
     44 	return connection.sqliteConnection.ExecContext(ctx, query, args)
     45 }
     46 func (connection *databaseConnection) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) {
     47 	connection.queries.Add(1)
     48 	return connection.sqliteConnection.QueryContext(ctx, query, args)
     49 }
     50 func (connection *databaseConnection) PrepareContext(ctx context.Context, query string) (driver.Stmt, error) {
     51 	statement, err := connection.sqliteConnection.PrepareContext(ctx, query)
     52 	if err != nil {
     53 		return nil, err
     54 	}
     55 	return &databaseStatement{statement.(sqliteStatement), connection.queries}, nil
     56 }
     57 
     58 type sqliteStatement interface {
     59 	driver.Stmt
     60 	driver.StmtExecContext
     61 	driver.StmtQueryContext
     62 }
     63 
     64 type databaseStatement struct {
     65 	sqliteStatement
     66 	queries *atomic.Uint64
     67 }
     68 
     69 func (statement *databaseStatement) ExecContext(ctx context.Context, args []driver.NamedValue) (driver.Result, error) {
     70 	statement.queries.Add(1)
     71 	return statement.sqliteStatement.ExecContext(ctx, args)
     72 }
     73 func (statement *databaseStatement) QueryContext(ctx context.Context, args []driver.NamedValue) (driver.Rows, error) {
     74 	statement.queries.Add(1)
     75 	return statement.sqliteStatement.QueryContext(ctx, args)
     76 }