Documentation
¶
Overview ¶
Package sqlstore is experimental: its API and documented formats may change in a minor release; see https://nsgo.games/docs/stability/.
Package sqlstore is a wechatpay.DeliveryStore over database/sql, written once for SQLite and Postgres and opened through a driver package: sqlitestore (with the pure-Go modernc.org/sqlite driver) or pgstore (with github.com/jackc/pgx/v5). It imports no driver itself, so New works with any *sql.DB of a supported dialect.
The schema is one table, created by New when missing and versioned in wechatpay_schema:
- wechatpay_deliveries: out_trade_no (the key), the push's raw Payload in payload, and delivered_at in Unix milliseconds
Put inserts with ON CONFLICT DO NOTHING, so the first record of an order stays. On Postgres the store is also a wechatpay.DeliveryLocker: Lock takes a transaction-level advisory lock on the order, so a Handler in each of several processes delivers an order once.
Example ¶
package main
import (
"fmt"
"github.com/nsgo-dev/game/pkg/online/storeverify/wechatpay/sqlstore"
)
func main() {
fmt.Println(sqlstore.SchemaVersion)
}
Output: 1
Index ¶
Examples ¶
Constants ¶
const SchemaVersion = 1
SchemaVersion is the schema version New creates and accepts.
Variables ¶
var Postgres = Dialect{ Name: "postgres", Numbered: true, Blob: "BYTEA", MigrateLock: `SELECT pg_advisory_xact_lock(2004381817)`, LockOrder: `SELECT pg_advisory_xact_lock(2004381817, hashtext(?))`, }
Postgres is Postgres's dialect. Its locks use the arbitrary class key 2004381817 ("wxpy" in ASCII).
var SQLite = Dialect{Name: "sqlite", Blob: "BLOB"}
SQLite is SQLite's dialect. A SQLite store serves one process (sqlitestore.Open), whose Handler already serializes the handling of each order, so its Lock does not lock.
Functions ¶
This section is empty.
Types ¶
type Dialect ¶
type Dialect struct {
// Name names the database in errors.
Name string
// Numbered is true for "$1" placeholders, false for "?".
Numbered bool
// Blob is the column type of a byte string, such as BLOB or BYTEA.
Blob string
// MigrateLock, when set, runs first in the migration's transaction to serialize migrations of several processes.
MigrateLock string
// LockOrder, when set, runs in the transaction Lock opens with the OutTradeNo as its one parameter and holds a lock on the order until the transaction ends. Without it Lock only returns, which suits a database one process uses.
LockOrder string
}
Dialect is what differs between SQL databases.
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
Store is a wechatpay.DeliveryStore in a SQL database.
func New ¶
New returns a store on db, creating the schema when it is missing. It refuses a database whose schema is newer than SchemaVersion.
func (*Store) Lock ¶
Lock implements wechatpay.DeliveryLocker. With the dialect's LockOrder it opens a transaction holding the order's lock, which unlock rolls back; the transaction holds one of the database's connections until then, and ends by itself when ctx does. Get and Put take a second connection meanwhile, so a pool capped with SetMaxOpenConns must allow two connections for each order handled at once: with every connection holding a lock, Get waits until ctx ends. Without LockOrder it returns at once.