postgres

package
v0.10.10 Latest Latest
Warning

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

Go to latest
Published: Jul 26, 2021 License: MIT Imports: 19 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 BulkDeleteRuns added in v0.10.10

func BulkDeleteRuns(db *gorm.DB, bulkQuery *models.BulkDeleteRunRequest) error

BulkDeleteRuns removes JobRuns and their related records: TaskRuns and RunResults.

RunResults and RunRequests are pointed at by JobRuns so we must use two CTEs to remove both parents in one hit.

TaskRuns are removed by ON DELETE CASCADE when the JobRuns and RunResults are deleted.

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)

func GormTransactionWithDefaultContext added in v0.10.8

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

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 SqlxTransaction added in v0.10.10

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

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 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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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