postgres

package
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 23, 2021 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

View Source
const (
	AdvisoryLockClassID_EthBroadcaster int32 = 0
	AdvisoryLockClassID_JobSpawner     int32 = 1
	AdvisoryLockClassID_EthConfirmer   int32 = 2

	// ORM takes lock on 1027321974924625846 which splits into ClassID 239192036, ObjID 2840971190
	AdvisoryLockClassID_ORM int32 = 239192036

	AdvisoryLockObjectID_EthConfirmer int32 = 0
)

NOTE: All advisory lock class IDs used by the Chainlink application MUST be kept here to avoid accidental re-use

View Source
const (
	ChannelJobCreated   = "insert_on_jobs"
	ChannelJobDeleted   = "delete_from_jobs"
	ChannelRunStarted   = "pipeline_run_started"
	ChannelRunCompleted = "pipeline_run_completed"

	// Postgres channel to listen for new eth_txes
	ChannelInsertOnEthTx = "insert_on_eth_txes"
)
View Source
const (
	// LockTimeout controls the max time we will wait for any kind of database lock.
	// It's good to set this to _something_ because waiting for locks forever is really bad.
	LockTimeout = 15 * time.Second
	// IdleInTxSessionTimeout controls the max time we leave a transaction open and idle.
	// It's good to set this to _something_ because leaving transactions open forever is really bad.
	IdleInTxSessionTimeout = 1 * time.Hour
)

NOTE: In an ideal world the timeouts below would be set to something sane in the postgres configuration by the user. Since we do not live in an ideal world, it is necessary to override them here.

They cannot easily be set at a session level due to how Go's connection pooling works.

View Source
const BatchSize uint = 1000

BatchSize is the default number of DB records to access in one batch

View Source
const DefaultQueryTimeout = 10 * time.Second

Variables

View Source
var (
	DefaultSqlTxOptions = sql.TxOptions{

		Isolation: sql.LevelReadCommitted,
	}
)
View Source
var (
	ErrNoDeadlineSet = errors.New("no deadline set")
)

Functions

func Batch added in v0.10.10

func Batch(cb BatchFunc) error

Batch is an iterator for batches of records

func DBWithDefaultContext added in v0.10.8

func DBWithDefaultContext(db *gorm.DB, fc func(db *gorm.DB) error) error

func DefaultQueryCtx added in v0.10.2

func DefaultQueryCtx() (ctx context.Context, cancel context.CancelFunc)

DefaultQueryCtx returns a context with a sensible sanity limit timeout for SQL queries

func GormTransaction

func GormTransaction(ctx context.Context, db *gorm.DB, fc func(tx *gorm.DB) error, txOptss ...sql.TxOptions) (err error)

DEPRECATED: Use the transaction manager instead.

func GormTransactionWithDefaultContext added in v0.10.8

func GormTransactionWithDefaultContext(db *gorm.DB, fc func(tx *gorm.DB) error, txOptss ...sql.TxOptions) error

DEPRECATED: Use the transaction manager instead.

func GormTransactionWithoutContext added in v0.10.8

func GormTransactionWithoutContext(db *gorm.DB, fc func(tx *gorm.DB) error, txOptss ...sql.TxOptions) (err error)

WARNING: Only use for nested txes inside ORM methods where you expect db to already have a ctx with a deadline.

func IsSerializationAnomaly

func IsSerializationAnomaly(err error) bool

func MustSQLDB added in v0.10.7

func MustSQLDB(db *gorm.DB) *sql.DB

func MustSQLTx added in v0.10.8

func MustSQLTx(db *gorm.DB) *sql.Tx

MustSQLDB panics if there is an error getting the underlying SQL TX

func NewEventBroadcaster

func NewEventBroadcaster(uri url.URL, minReconnectInterval time.Duration, maxReconnectDuration time.Duration) *eventBroadcaster

func Sessions added in v0.10.11

func Sessions(db *gorm.DB, offset, limit int) ([]models.Session, error)

Sessions returns all sessions limited by the parameters.

func SqlTransaction added in v1.0.0

func SqlTransaction(ctx context.Context, rdb *sql.DB, fc func(tx *sqlx.Tx) error, txOpts ...sql.TxOptions) (err error)

func SqlxTransaction added in v0.10.10

func SqlxTransaction(ctx context.Context, q Queryer, fc func(tx *sqlx.Tx) error, txOpts ...sql.TxOptions) (err error)

func TxFromContext added in v0.10.11

func TxFromContext(ctx context.Context, db *gorm.DB) *gorm.DB

TxFromContext extracts the tx from the context. If no transaction value is provided in the context, it returns the gorm.DB.

func UnwrapGormDB added in v1.0.0

func UnwrapGormDB(db *gorm.DB) *sqlx.DB

func WrapDbWithSqlx added in v0.10.10

func WrapDbWithSqlx(rdb *sql.DB) *sqlx.DB

Types

type AdvisoryLocker

type AdvisoryLocker interface {
	Unlock(ctx context.Context, classID int32, objectID int32) error
	WithAdvisoryLock(ctx context.Context, classID int32, objectID int32, f func() error) error
	Close() error
}

func NewAdvisoryLock

func NewAdvisoryLock(uri url.URL) AdvisoryLocker

type BatchFunc added in v0.10.10

type BatchFunc func(offset, limit uint) (count uint, err error)

BatchFunc is the function to execute on each batch of records, should return the count of records affected

type Event

type Event struct {
	Channel string
	Payload string
}

type EventBroadcaster

type EventBroadcaster interface {
	service.Service
	Subscribe(channel, payloadFilter string) (Subscription, error)
	Notify(channel string, payload string) error
	NotifyInsideGormTx(tx *gorm.DB, channel string, payload string) error
}

EventBroadcaster opaquely manages a collection of Postgres event listeners and broadcasts events to subscribers (with an optional payload filter).

type NullAdvisoryLocker

type NullAdvisoryLocker struct {
	// contains filtered or unexported fields
}

func (*NullAdvisoryLocker) Close

func (n *NullAdvisoryLocker) Close() error

func (*NullAdvisoryLocker) Unlock

func (*NullAdvisoryLocker) Unlock(ctx context.Context, classID int32, objectID int32) error

func (*NullAdvisoryLocker) WithAdvisoryLock

func (*NullAdvisoryLocker) WithAdvisoryLock(ctx context.Context, classID int32, objectID int32, f func() error) error

type NullEventBroadcaster added in v0.10.8

type NullEventBroadcaster struct{}

NullEventBroadcaster implements null pattern for event broadcaster

func (*NullEventBroadcaster) Close added in v0.10.8

func (*NullEventBroadcaster) Close() error

func (*NullEventBroadcaster) Healthy added in v0.10.8

func (*NullEventBroadcaster) Healthy() error

func (*NullEventBroadcaster) Notify added in v0.10.8

func (*NullEventBroadcaster) Notify(channel string, payload string) error

func (*NullEventBroadcaster) NotifyInsideGormTx added in v0.10.8

func (*NullEventBroadcaster) NotifyInsideGormTx(tx *gorm.DB, channel string, payload string) error

func (*NullEventBroadcaster) Ready added in v0.10.8

func (*NullEventBroadcaster) Ready() error

func (*NullEventBroadcaster) Start added in v0.10.8

func (*NullEventBroadcaster) Start() error

func (*NullEventBroadcaster) Subscribe added in v0.10.8

func (*NullEventBroadcaster) Subscribe(channel, payloadFilter string) (Subscription, error)

type Queryer added in v1.0.0

type Queryer interface {
	sqlx.Ext
	sqlx.ExtContext
	QueryRow(query string, args ...interface{}) *sql.Row
}

func UnwrapGorm added in v1.0.0

func UnwrapGorm(db *gorm.DB) Queryer

type Subscription

type Subscription interface {
	Events() <-chan Event
	Close()

	ChannelName() string
	InterestedIn(event Event) bool
	Send(event Event)
}

Subscription represents a subscription to a Postgres event channel

type TransactionManager added in v0.10.11

type TransactionManager interface {
	Transact(TxFn, ...TransactionOption) error
	TransactWithContext(ctx context.Context, fn TxFn, optsFn ...TransactionOption) (err error)
}

func NewGormTransactionManager added in v0.10.11

func NewGormTransactionManager(db *gorm.DB) TransactionManager

type TransactionOption added in v0.10.11

type TransactionOption func(opts *transactionOptions)

TransactionManagerOption configures how we set up the transaction

func WithTxOptions added in v0.10.11

func WithTxOptions(txopts sql.TxOptions) TransactionOption

WithTxOptions returns a TransactionOption which sets the sql.TxOptions on the transaction.

func WithoutDeadline added in v0.10.11

func WithoutDeadline() TransactionOption

type TxFn added in v0.10.11

type TxFn func(context.Context) error

A TxFn is a function that will be called with a context which has a transaction injected as a value. This can be used for executing statements and queries against a database.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL