common

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2024 License: Apache-2.0 Imports: 25 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BulkLoad

func BulkLoad(
	ctx context.Context,
	tx pgx.Tx,
	tupleTableName string,
	colNames []string,
	iter datastore.BulkWriteRelationshipSource,
) (uint64, error)

func ConfigureOTELTracer

func ConfigureOTELTracer(connConfig *pgx.ConnConfig)

ConfigureOTELTracer adds OTEL tracing to a pgx.ConnConfig

func ConfigurePGXLogger

func ConfigurePGXLogger(connConfig *pgx.ConnConfig)

ConfigurePGXLogger sets zerolog global logger into the connection pool configuration, and maps info level events to debug, as they are rather verbose for SpiceDB's info level

func ConnectWithInstrumentation

func ConnectWithInstrumentation(ctx context.Context, url string) (*pgx.Conn, error)

ConnectWithInstrumentation returns a pgx.Conn that has been instrumented for observability

func ConnectWithInstrumentationAndTimeout added in v0.0.3

func ConnectWithInstrumentationAndTimeout(ctx context.Context, url string, connectTimeout time.Duration) (*pgx.Conn, error)

ConnectWithInstrumentationAndTimeout returns a pgx.Conn that has been instrumented for observability

func ConvertToWriteConstraintError

func ConvertToWriteConstraintError(livingTupleConstraints []string, err error) error

ConvertToWriteConstraintError converts the given Postgres error into a CreateRelationshipExistsError if applicable. If not applicable, returns nils.

func IsCancellationError

func IsCancellationError(err error) bool

IsCancellationError determines if an error returned by pgx has been caused by context cancellation.

func IsConstraintFailureError

func IsConstraintFailureError(err error) bool

IsConstraintFailureError returns true if the error is a Postgres error indicating a constraint failure.

func IsReadOnlyTransactionError added in v0.0.3

func IsReadOnlyTransactionError(err error) bool

IsReadOnlyTransactionError returns true if the error is a Postgres error indicating a read-only transaction.

func IsSerializationError

func IsSerializationError(err error) bool

func NewPGXExecutor

func NewPGXExecutor(querier DBFuncQuerier) common.ExecuteQueryFunc

NewPGXExecutor creates an executor that uses the pgx library to make the specified queries.

func NewPGXExecutorWithIntegrityOption added in v0.0.3

func NewPGXExecutorWithIntegrityOption(querier DBFuncQuerier, withIntegrity bool) common.ExecuteQueryFunc

func ParseConfigWithInstrumentation

func ParseConfigWithInstrumentation(url string) (*pgx.ConnConfig, error)

ParseConfigWithInstrumentation returns a pgx.ConnConfig that has been instrumented for observability

func SleepOnErr

func SleepOnErr(ctx context.Context, err error, retries uint8)

SleepOnErr sleeps for a short period of time after an error has occurred.

Types

type ComposedTracer

type ComposedTracer struct {
	Tracers []pgx.QueryTracer
}

ComposedTracer allows adding multiple tracers to a pgx.ConnConfig

func (*ComposedTracer) TraceQueryEnd

func (m *ComposedTracer) TraceQueryEnd(ctx context.Context, conn *pgx.Conn, data pgx.TraceQueryEndData)

func (*ComposedTracer) TraceQueryStart

func (m *ComposedTracer) TraceQueryStart(ctx context.Context, conn *pgx.Conn, data pgx.TraceQueryStartData) context.Context

type ConnPooler

type ConnPooler interface {
	Querier
	Begin(ctx context.Context) (pgx.Tx, error)
	BeginTx(ctx context.Context, txOptions pgx.TxOptions) (pgx.Tx, error)
	CopyFrom(ctx context.Context, tableName pgx.Identifier, columnNames []string, rowSrc pgx.CopyFromSource) (int64, error)
	Close()
}

ConnPooler is an interface to pgx.Pool methods used by postgres-based datastores

func MustNewInterceptorPooler

func MustNewInterceptorPooler(pooler ConnPooler, interceptor QueryInterceptor) ConnPooler

type DBFuncQuerier

type DBFuncQuerier interface {
	ExecFunc(ctx context.Context, tagFunc func(ctx context.Context, tag pgconn.CommandTag, err error) error, sql string, arguments ...any) error
	QueryFunc(ctx context.Context, rowsFunc func(ctx context.Context, rows pgx.Rows) error, sql string, optionsAndArgs ...any) error
	QueryRowFunc(ctx context.Context, rowFunc func(ctx context.Context, row pgx.Row) error, sql string, optionsAndArgs ...any) error
}

DBFuncQuerier is satisfied by RetryPool and QuerierFuncs (which can wrap a pgxpool or transaction)

func QuerierFuncsFor

func QuerierFuncsFor(d Querier) DBFuncQuerier

type InterceptorPooler

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

func (InterceptorPooler) Begin

func (i InterceptorPooler) Begin(ctx context.Context) (pgx.Tx, error)

func (InterceptorPooler) BeginTx

func (i InterceptorPooler) BeginTx(ctx context.Context, txOptions pgx.TxOptions) (pgx.Tx, error)

func (InterceptorPooler) Close

func (i InterceptorPooler) Close()

func (InterceptorPooler) CopyFrom

func (i InterceptorPooler) CopyFrom(ctx context.Context, tableName pgx.Identifier, columnNames []string, rowSrc pgx.CopyFromSource) (int64, error)

func (InterceptorPooler) Exec

func (i InterceptorPooler) Exec(ctx context.Context, sql string, arguments ...any) (pgconn.CommandTag, error)

func (InterceptorPooler) Query

func (i InterceptorPooler) Query(ctx context.Context, sql string, optionsAndArgs ...any) (pgx.Rows, error)

func (InterceptorPooler) QueryRow

func (i InterceptorPooler) QueryRow(ctx context.Context, sql string, optionsAndArgs ...any) pgx.Row

type PoolOptions

type PoolOptions struct {
	ConnMaxIdleTime         *time.Duration
	ConnMaxLifetime         *time.Duration
	ConnMaxLifetimeJitter   *time.Duration
	ConnHealthCheckInterval *time.Duration
	MinOpenConns            *int
	MaxOpenConns            *int
}

PoolOptions is the set of configuration used for a pgx connection pool.

func (PoolOptions) ConfigurePgx

func (opts PoolOptions) ConfigurePgx(pgxConfig *pgxpool.Config) error

ConfigurePgx applies PoolOptions to a pgx connection pool confiugration.

type Querier

type Querier interface {
	Exec(ctx context.Context, sql string, arguments ...any) (pgconn.CommandTag, error)
	Query(ctx context.Context, sql string, optionsAndArgs ...any) (pgx.Rows, error)
	QueryRow(ctx context.Context, sql string, optionsAndArgs ...any) pgx.Row
}

Querier holds common methods for connections and pools, equivalent to Querier (which is deprecated for pgx v5)

type QuerierFuncs

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

func (*QuerierFuncs) ExecFunc

func (t *QuerierFuncs) ExecFunc(ctx context.Context, tagFunc func(ctx context.Context, tag pgconn.CommandTag, err error) error, sql string, arguments ...any) error

func (*QuerierFuncs) QueryFunc

func (t *QuerierFuncs) QueryFunc(ctx context.Context, rowsFunc func(ctx context.Context, rows pgx.Rows) error, sql string, optionsAndArgs ...any) error

func (*QuerierFuncs) QueryRowFunc

func (t *QuerierFuncs) QueryRowFunc(ctx context.Context, rowFunc func(ctx context.Context, row pgx.Row) error, sql string, optionsAndArgs ...any) error

type QueryInterceptor

type QueryInterceptor interface {
	// InterceptExec is the method to intercept Querier.Exec. The implementation is responsible to invoke the
	// delegate with the provided arguments
	InterceptExec(ctx context.Context, delegate Querier, sql string, arguments ...any) (pgconn.CommandTag, error)

	// InterceptQuery is the method to intercept Querier.Query. The implementation is responsible to invoke the
	// delegate with the provided arguments
	InterceptQuery(ctx context.Context, delegate Querier, sql string, args ...any) (pgx.Rows, error)

	// InterceptQueryRow is the method to intercept Querier.QueryRow. The implementation is responsible to invoke the
	// delegate with the provided arguments
	InterceptQueryRow(ctx context.Context, delegate Querier, sql string, optionsAndArgs ...any) pgx.Row
}

QueryInterceptor exposes a mechanism to intercept all methods exposed in Querier This can be used as a sort of middleware layer for pgx queries

Jump to

Keyboard shortcuts

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