scheduler

package
v0.0.1 Latest Latest
Warning

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

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

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithContext

func WithContext(ctx actor.SenderContext) timerOptionFunc

WithContext configures the scheduler to use ctx rather than the default, EmptyRootContext.

Types

type CancelFunc

type CancelFunc func()

type Stopper

type Stopper interface {
	Stop()
}

type TimerScheduler

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

A scheduler utilizing timers to send messages in the future and at regular intervals.

Example (SendRepeatedly)

Use the timer scheduler to repeatedly send messages to an actor.

package main

import (
	"fmt"
	"sync"
	"time"

	"github.com/AsynkronIT/protoactor-go/actor"
	"github.com/AsynkronIT/protoactor-go/scheduler"
)

func main() {
	var wg sync.WaitGroup
	wg.Add(2)

	count := 0
	props := actor.PropsFromFunc(func(c actor.Context) {
		switch v := c.Message().(type) {
		case string:
			count++
			fmt.Println(count, v)
			wg.Done()
		}
	})

	pid := actor.EmptyRootContext.Spawn(props)

	s := scheduler.NewTimerScheduler()
	cancel := s.SendRepeatedly(1*time.Millisecond, 1*time.Millisecond, pid, "Hello")
	wg.Wait()
	cancel()

}
Output:

1 Hello
2 Hello

func NewTimerScheduler

func NewTimerScheduler(opts ...timerOptionFunc) *TimerScheduler

NewTimerScheduler creates a new scheduler using the EmptyRootContext. Additional options may be specified to override the default behavior.

func (*TimerScheduler) RequestOnce

func (s *TimerScheduler) RequestOnce(delay time.Duration, pid *actor.PID, message interface{}) CancelFunc

RequestOnce waits for the duration to elapse and then calls actor.SenderContext.Request to forward the message to pid.

func (*TimerScheduler) RequestRepeatedly

func (s *TimerScheduler) RequestRepeatedly(delay, interval time.Duration, pid *actor.PID, message interface{}) CancelFunc

RequestRepeatedly waits for the initial duration to elapse and then calls Request to forward the message to pid repeatedly for each interval.

func (*TimerScheduler) SendOnce

func (s *TimerScheduler) SendOnce(delay time.Duration, pid *actor.PID, message interface{}) CancelFunc

SendOnce waits for the duration to elapse and then calls actor.SenderContext.Send to forward the message to pid.

func (*TimerScheduler) SendRepeatedly

func (s *TimerScheduler) SendRepeatedly(initial, interval time.Duration, pid *actor.PID, message interface{}) CancelFunc

SendRepeatedly waits for the initial duration to elapse and then calls Send to forward the message to pid repeatedly for each interval.

Jump to

Keyboard shortcuts

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