infrastructure

package
v0.0.0-...-fa5135e Latest Latest
Warning

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

Go to latest
Published: Jan 25, 2021 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (

	// ErrInvalidType represents an error that indicates a unexpected type
	// was provided to the data mapper.
	ErrInvalidType = errors.New("infrastructure: invalid type provided to data mapper")
)

Errors that are potentially thrown during data mapper interactions.

View Source
var Module = fx.Options(
	fx.Provide(NewQueryer),
	fx.Provide(func(c config.Configuration) (DBResult, error) {
		var db *sql.DB
		connect := func() (err error) {
			db, err = sql.Open("mysql", c.Database.DSN())
			return
		}
		options := []retry.Option{
			retry.Attempts(3),
			retry.Delay(time.Second * 5),
			retry.OnRetry(func(attempt uint, retryErr error) {
				fmt.Println("Attempting to connect:", c.Database.DSN())
			}),
		}
		if err := retry.Do(connect, options...); err != nil {
			return DBResult{}, err
		}
		ping := func() (err error) {
			err = db.Ping()
			return
		}
		if err := retry.Do(ping, options...); err != nil {
			return DBResult{}, err
		}
		return DBResult{DB: db}, nil
	}),
	fx.Provide(func(c config.Configuration) (tally.Scope, error) {
		addr := fmt.Sprintf("%s:%d", c.Metrics.Host, c.Metrics.Port)
		flushInterval := time.Duration(c.Metrics.MaxFlushInterval) * time.Millisecond
		statter, err :=
			statsd.NewBufferedClient(
				addr, c.Metrics.Prefix, flushInterval, c.Metrics.MaxFlushBytes)
		if err != nil {
			return nil, err
		}
		reporter := tstatsd.NewReporter(statter, tstatsd.Options{
			SampleRate: 1.0,
		})
		scope, _ := tally.NewRootScope(tally.ScopeOptions{
			Tags:     map[string]string{},
			Reporter: reporter,
		}, time.Second)
		return scope, nil
	}),
	fx.Provide(func(l *zap.Logger) UnitResult {
		dataMappers := make(map[unit.TypeName]unit.DataMapper)
		accountTN := unit.TypeNameOf(domain.Account{})
		dm := NewAccountDataMapper(AccountDataMapperParameters{Logger: l})
		dataMappers[accountTN] = &dm
		return UnitResult{Option: unit.DataMappers(dataMappers)}
	}),
	fx.Provide(func(l *zap.Logger) UnitResult {
		return UnitResult{Option: unit.Logger(l)}
	}),
	fx.Provide(func(s tally.Scope) UnitResult {
		return UnitResult{Option: unit.Scope(s)}
	}),
	fx.Provide(func(parameters DBParameters) UnitResult {
		return UnitResult{Option: unit.DB(parameters.DB)}
	}),
	workfx.Module,
)

Functions

This section is empty.

Types

type AccountDataMapper

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

func NewAccountDataMapper

func NewAccountDataMapper(
	parameters AccountDataMapperParameters) AccountDataMapper

func (*AccountDataMapper) Delete

func (dm *AccountDataMapper) Delete(ctx context.Context, mCtx unit.MapperContext, accounts ...interface{}) error

func (*AccountDataMapper) Insert

func (dm *AccountDataMapper) Insert(ctx context.Context, mCtx unit.MapperContext, accounts ...interface{}) error

func (*AccountDataMapper) Update

func (dm *AccountDataMapper) Update(ctx context.Context, mCtx unit.MapperContext, accounts ...interface{}) error

type AccountDataMapperParameters

type AccountDataMapperParameters struct {
	fx.In

	DB     *sql.DB `name:"rwDB"`
	Logger *zap.Logger
}

type AccountQuery

type AccountQuery interface {
	Execute() ([]domain.Account, error)
}

func NewFindAccountByUUIDQuery

func NewFindAccountByUUIDQuery(db *sql.DB, uuid u.UUID) AccountQuery

type AccountRepository

type AccountRepository interface {
	Get(u.UUID) (*domain.Account, error)
	Put(domain.Account) error
	Remove(domain.Account) error
	Add(domain.Account) error
	Find(AccountQuery) ([]domain.Account, error)
	Size() (int, error)
}

AccountRepository represents a collection of all accounts within the application.

func NewAccountRepository

func NewAccountRepository(unit unit.Unit, queryer Queryer) AccountRepository

type DBParameters

type DBParameters struct {
	fx.In

	DB *sql.DB `name:"rwDB"`
}

type DBResult

type DBResult struct {
	fx.Out

	DB *sql.DB `name:"rwDB"`
}

type Queryer

type Queryer interface {
	Query(u.UUID) AccountQuery
}

func NewQueryer

func NewQueryer(parameters QueryerParameters) Queryer

type QueryerParameters

type QueryerParameters struct {
	fx.In

	DB *sql.DB `name:"rwDB"`
}

type UnitResult

type UnitResult struct {
	fx.Out

	Option unit.Option `group:"unitOptions"`
}

Jump to

Keyboard shortcuts

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