reminder

package module
v0.0.0-...-58401cb Latest Latest
Warning

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

Go to latest
Published: May 21, 2018 License: MIT Imports: 12 Imported by: 0

README

Proto Actor [Go] - Reminder

Go Report Card

Go package that provides a mechanism for actors to receive time delayed reminders.

Idea based on Microsoft Orleans Reminders

Get started

Install package:

go get github.com/artyomturkin/protoactor-go-reminder

Create a reminder actor. It requires persistence plugin to work.

func main() {
	//Create reminder actor with 10 ms trigger window and InMemory Persistence store
	remProps := actor.FromProducer(reminder.Producer(10 * time.Millisecond)).
		WithMiddleware(persistence.Using(newProvider(5)))
	rem, err := actor.SpawnNamed(remProps, "reminder")
	if err != nil {
		fmt.Printf("failed to spawn reminder: %v", err)
		os.Exit(1)
	}

	.....
}

Define actor, that can setup and receive reminders

type exampleActor struct {
	reminder.Mixin
}

type ExampleMessage struct{}

func (r *exampleActor) Receive(ctx actor.Context) {
	switch msg := ctx.Message().(type) {
	case *ExampleMessage:
		r.RemindMe("hello", 1*time.Millisecond, false)
	case *msgs.Remind:
		if msg.Name == "hello" {
			fmt.Printf("Received reminder %s\n", msg.Name)
		}
	}
}

Create example actor instance

func main() {
	.....
	recProps := actor.FromProducer(exampleActorProducer).
		WithMiddleware(reminder.Reminder(rem))
	rec, err := actor.SpawnNamed(recProps, "receiver")
	if err != nil {
		fmt.Printf("failed to spawn receiver: %v", err)
		os.Exit(1)
	}
	.....
}

Tell example actor to setup a remind for itself

func main() {
	.....

	rec.Tell(&ExampleMessage{})
	
	.....
}

Documentation

Overview

Package reminder is a generated protocol buffer package.

It is generated from these files:

messages.proto

It has these top-level messages:

Reminder
Remind
Reminded
Snapshot

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidLengthMessages = fmt.Errorf("proto: negative length found during unmarshaling")
	ErrIntOverflowMessages   = fmt.Errorf("proto: integer overflow")
)

Functions

func Middleware

func Middleware(reminder *actor.PID) func(next actor.ActorFunc) actor.ActorFunc

Middleware middleware

Example
package main

import (
	"fmt"
	"os"
	"sync"
	"time"

	"github.com/AsynkronIT/protoactor-go/actor"
	"github.com/AsynkronIT/protoactor-go/persistence"
	reminder "github.com/artyomturkin/protoactor-go-reminder"
)

type exampleActor struct {
	reminder.Mixin

	wg *sync.WaitGroup
}

type ExampleMessage struct{}

func (r *exampleActor) Receive(ctx actor.Context) {
	switch msg := ctx.Message().(type) {
	case *ExampleMessage:
		r.RemindMe("hello", 1*time.Millisecond, false)
	case *reminder.Remind:
		if msg.Name == "hello" {
			fmt.Printf("Received reminder %s\n", msg.Name)
			r.wg.Done()
		}
	}
}

func exampleActorProducer(wg *sync.WaitGroup) func() actor.Actor {
	return func() actor.Actor {
		return &exampleActor{wg: wg}
	}
}

func main() {
	remProps := actor.FromProducer(reminder.Producer(10 * time.Millisecond)).
		WithMiddleware(persistence.Using(newProvider(5)))
	rem, err := actor.SpawnNamed(remProps, "reminder")
	if err != nil {
		fmt.Printf("failed to spawn reminder: %v", err)
		os.Exit(1)
	}

	wg := &sync.WaitGroup{}
	recProps := actor.FromProducer(exampleActorProducer(wg)).
		WithMiddleware(reminder.Middleware(rem))
	rec, err := actor.SpawnNamed(recProps, "receiver")
	if err != nil {
		fmt.Printf("failed to spawn receiver: %v", err)
		os.Exit(1)
	}

	rems := 2
	wg.Add(rems)
	for i := 0; i < rems; i++ {
		rec.Tell(&ExampleMessage{})
	}
	wg.Wait()

	rem.GracefulPoison()
	rec.GracefulPoison()

}
Output:

Received reminder hello
Received reminder hello

func Producer

func Producer(window time.Duration) func() actor.Actor

Producer Reminder ActorProducer

Types

type Mixin

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

Mixin provides reminder service to actor

func (*Mixin) RemindMe

func (m *Mixin) RemindMe(name string, in time.Duration, collate bool)

RemindMe set reminder

type Remind

type Remind struct {
	Name string `protobuf:"bytes,1,opt,name=Name,proto3" json:"Name,omitempty"`
}

func (*Remind) Descriptor

func (*Remind) Descriptor() ([]byte, []int)

func (*Remind) Equal

func (this *Remind) Equal(that interface{}) bool

func (*Remind) GetName

func (m *Remind) GetName() string

func (*Remind) GoString

func (this *Remind) GoString() string

func (*Remind) Marshal

func (m *Remind) Marshal() (dAtA []byte, err error)

func (*Remind) MarshalTo

func (m *Remind) MarshalTo(dAtA []byte) (int, error)

func (*Remind) ProtoMessage

func (*Remind) ProtoMessage()

func (*Remind) Reset

func (m *Remind) Reset()

func (*Remind) Size

func (m *Remind) Size() (n int)

func (*Remind) String

func (this *Remind) String() string

func (*Remind) Unmarshal

func (m *Remind) Unmarshal(dAtA []byte) error

type Reminded

type Reminded struct {
	At *google_protobuf1.Timestamp `protobuf:"bytes,2,opt,name=At" json:"At,omitempty"`
}

func (*Reminded) Descriptor

func (*Reminded) Descriptor() ([]byte, []int)

func (*Reminded) Equal

func (this *Reminded) Equal(that interface{}) bool

func (*Reminded) GetAt

func (m *Reminded) GetAt() *google_protobuf1.Timestamp

func (*Reminded) GoString

func (this *Reminded) GoString() string

func (*Reminded) Marshal

func (m *Reminded) Marshal() (dAtA []byte, err error)

func (*Reminded) MarshalTo

func (m *Reminded) MarshalTo(dAtA []byte) (int, error)

func (*Reminded) ProtoMessage

func (*Reminded) ProtoMessage()

func (*Reminded) Reset

func (m *Reminded) Reset()

func (*Reminded) Size

func (m *Reminded) Size() (n int)

func (*Reminded) String

func (this *Reminded) String() string

func (*Reminded) Unmarshal

func (m *Reminded) Unmarshal(dAtA []byte) error

type Reminder

type Reminder struct {
	Receiver *actor.PID                  `protobuf:"bytes,1,opt,name=Receiver" json:"Receiver,omitempty"`
	At       *google_protobuf1.Timestamp `protobuf:"bytes,2,opt,name=At" json:"At,omitempty"`
	Name     string                      `protobuf:"bytes,3,opt,name=Name,proto3" json:"Name,omitempty"`
	Collate  bool                        `protobuf:"varint,4,opt,name=Collate,proto3" json:"Collate,omitempty"`
}

func (*Reminder) Descriptor

func (*Reminder) Descriptor() ([]byte, []int)

func (*Reminder) Equal

func (this *Reminder) Equal(that interface{}) bool

func (*Reminder) GetAt

func (m *Reminder) GetAt() *google_protobuf1.Timestamp

func (*Reminder) GetCollate

func (m *Reminder) GetCollate() bool

func (*Reminder) GetName

func (m *Reminder) GetName() string

func (*Reminder) GetReceiver

func (m *Reminder) GetReceiver() *actor.PID

func (*Reminder) GoString

func (this *Reminder) GoString() string

func (*Reminder) Marshal

func (m *Reminder) Marshal() (dAtA []byte, err error)

func (*Reminder) MarshalTo

func (m *Reminder) MarshalTo(dAtA []byte) (int, error)

func (*Reminder) ProtoMessage

func (*Reminder) ProtoMessage()

func (*Reminder) Reset

func (m *Reminder) Reset()

func (*Reminder) Size

func (m *Reminder) Size() (n int)

func (*Reminder) String

func (this *Reminder) String() string

func (*Reminder) Unmarshal

func (m *Reminder) Unmarshal(dAtA []byte) error

type Snapshot

type Snapshot struct {
	Reminds []*Reminder                 `protobuf:"bytes,1,rep,name=Reminds" json:"Reminds,omitempty"`
	At      *google_protobuf1.Timestamp `protobuf:"bytes,2,opt,name=At" json:"At,omitempty"`
}

func (*Snapshot) Descriptor

func (*Snapshot) Descriptor() ([]byte, []int)

func (*Snapshot) Equal

func (this *Snapshot) Equal(that interface{}) bool

func (*Snapshot) GetAt

func (m *Snapshot) GetAt() *google_protobuf1.Timestamp

func (*Snapshot) GetReminds

func (m *Snapshot) GetReminds() []*Reminder

func (*Snapshot) GoString

func (this *Snapshot) GoString() string

func (*Snapshot) Marshal

func (m *Snapshot) Marshal() (dAtA []byte, err error)

func (*Snapshot) MarshalTo

func (m *Snapshot) MarshalTo(dAtA []byte) (int, error)

func (*Snapshot) ProtoMessage

func (*Snapshot) ProtoMessage()

func (*Snapshot) Reset

func (m *Snapshot) Reset()

func (*Snapshot) Size

func (m *Snapshot) Size() (n int)

func (*Snapshot) String

func (this *Snapshot) String() string

func (*Snapshot) Unmarshal

func (m *Snapshot) Unmarshal(dAtA []byte) error

Jump to

Keyboard shortcuts

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