sweep

package module
v0.0.0-...-93996d8 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2019 License: Apache-2.0 Imports: 2 Imported by: 0

README

sweep

Sweep away expired data from databases safely.

Installation

$ go get -u -d github.com/codingconcepts/sweep

Usage

// Sweep is a blocking function that will only return if you pass it
// a poison pill via its Done channel. If you'd like to report on how
// many items are deleted or how many errors occur during operation,
// this can be acheived with channels.
affected := make(chan int64, 10)
errors := make(chan error, 10)

// Inform sweep to wake up every hour and attempt to delete 1,000
// expired items from a database table. If more than zero items are
// deleted, it will continue to sweep every seconds, until there are
// no more expired items in the table. At which point, it sleeps for
// another hour.
s := New(db, Config{
	Affected:          make(chan int64, 10),
	Errors:            make(chan error, 10),
	Interval:          time.Hour * 1,
	IncrementInterval: time.Second * 10,
	DeleteFunc:        func() (string, []interface{}) {
		return `DELETE FROM "your_table"
		WHERE "your_expiry_indicator" < $1
		LIMIT $2`, []interface{}{ time.Now().UTC(),	1000 }
	},
})

go s.Sweep()

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Interval          time.Duration
	IncrementInterval time.Duration
	Errors            chan error
	Affected          chan int64
	DeleteFunc        func() (string, []interface{})
}

Config contains the configuration properties that will be used by the sweeper during calls to Sweep.

type Sweeper

type Sweeper struct {
	Errs     chan error
	Affected chan int64
	Done     chan struct{}
	// contains filtered or unexported fields
}

func New

func New(db *sql.DB, c Config) *Sweeper

New returns a new sweeper object configured with the provide Config object.

func (*Sweeper) Sweep

func (s *Sweeper) Sweep()

Sweep is a blocking function that clears down a database table using Config provided at initialisation. It will wait for a given Interval before starting a sweep process, clearing down Limit items at a time every IncrementInterval until the table is empty.

Jump to

Keyboard shortcuts

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