sql

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2018 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Client

func Client(s bandmaster.Service) *sql.DB

Client returns the underlying `sql.DB` of the given service.

It assumes that the service is ready; i.e. it might return nil if it's actually not.

NOTE: This will panic if `s` is not a `sql.Service`.

func New

func New(conf *Config) bandmaster.Service

New creates a new SQL service using the provided `Config`. You may use the helpers for environment-based configuration to get a pre-configured `Config` with sane defaults.

It doesn't open any connection nor does it do any kind of I/O; i.e. it cannot fail.

Types

type Config

type Config struct {
	Driver          string
	Addr            string
	ConnMaxLifetime time.Duration
	MaxIdleConns    int
	MaxOpenConns    int
}

type Env

type Env struct {
	Driver          string        `envconfig:"DRIVER" default:"postgres"`
	Addr            string        `envconfig:"ADDR" default:"postgres://postgres:postgres@127.0.0.1:5432/postgres?sslmode=disable"`
	ConnMaxLifetime time.Duration `envconfig:"CONN_MAX_LIFETIME" default:"10m"`
	MaxIdleConns    int           `envconfig:"MAX_IDLE_CONNS" default:"1"`
	MaxOpenConns    int           `envconfig:"MAX_OPEN_CONNS" default:"2"`
}

Env can be used to configure a SQL session via the environment.

It comes with sane defaults for a local development set-up. The default driver is setup to `posgres` with a working local address.

func NewEnv

func NewEnv(prefix string) (*Env, error)

NewEnv parses the environment and returns a new `Env` structure.

`prefix` defines the prefix for the environment keys, e.g. with a 'XX' prefix, 'REPLICAS' would become 'XX_REPLICAS'.

func (*Env) Config

func (e *Env) Config() *Config

Config returns a `Config` using the values from the environment.

type Service

type Service struct {
	*bandmaster.ServiceBase // "inheritance"
	// contains filtered or unexported fields
}

func (*Service) Start

func (s *Service) Start(ctx context.Context, _ map[string]bandmaster.Service) error

Start opens a connection and PINGs the server: if everything goes smoothly, the service is marked as 'started'; otherwise, an error is returned.

Start is used by BandMaster's internal machinery, it shouldn't ever be called directly by the end-user of the service.

func (*Service) Stop

func (s *Service) Stop(context.Context) error

Stop closes the database, releasing any open resources.

Stop is used by BandMaster's internal machinery, it shouldn't ever be called directly by the end-user of the service.

Jump to

Keyboard shortcuts

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