trigger

package
v0.0.0-...-5e451f0 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2024 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	EventTimeReached     = "event_time_reached"
	EventCountReached    = "event_count_reached"
	EventConditionScheme = "event_condition_scheme"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Option

type Option func(*Trigger)

func WithCondition

func WithCondition(c context.Context, f func(ctx context.Context, event string, elements ...interface{}) int) Option

WithCondition is more flexible than WithMaxTime and WithMaxCount, it provides a function to decide the batch count of elements by receiving the event push by Trigger. The param f is a function has three param

ctx is the same as WithCondition first param context.Context

event has three value, see more details from the comment above.

elements are batch that packed when the event happened.

The return is an int value, when event is EventTimeReached or EventCountReached, it's ignored, only when the event is EventConditionScheme, it means the count of elements need to pack for batch.

func WithMaxCount

func WithMaxCount(n int) Option

WithMaxCount sets the count, when elements in Trigger reach it, the Trigger will pack all elements in it to batch and send elements batch to receiver queue.

func WithMaxTime

func WithMaxTime(t time.Duration) Option

WithMaxTime sets a timer, when it's expired, elements in Trigger will be packed as batch and send elements batch to receiver queue.

type Trigger

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

Trigger accepts element put, and work as a counter. When max_count, timer or condition reaches, it will do notify by the callback function. It's used in log or message collector scenes as usual. Logs and messages are put into the Trigger's queue, given a timer or max_count for trigger to pack the elements to a batch, and then send them in one request to the backend server.

func NewTrigger

func NewTrigger(receiver *queue.Buffer, ops ...Option) *Trigger

NewTrigger create the Trigger, the receiver param is the queue for receiving the batch elements which is return by trigger reached. Elements in receiver queue is type '[]interface{}'. Option provides trigger setting, such as max_time, max_count or condition defined by yourself. One option would be setting at least, all options could be set together yet. All methods of Trigger are thread-safe.

func (*Trigger) Add

func (this *Trigger) Add(e interface{})

Add put an element into Trigger.

func (*Trigger) Flush

func (this *Trigger) Flush()

Flush will do pack all elements in Trigger to batch manually.

func (*Trigger) Start

func (this *Trigger) Start()

Start is required to called before call Add or Flush.

func (*Trigger) Stop

func (this *Trigger) Stop()

Stop is required to called in pair with Start for shutdown the Trigger.

Jump to

Keyboard shortcuts

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