actor

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Jul 26, 2022 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Overview

Package actor provides basic support for building actors for use in the Agent.

Example
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.

package main

import (
	"context"
	"fmt"
	"time"
)

func main() {
	type component struct {
		ch    chan int
		actor Goroutine
	}

	c := &component{
		ch: make(chan int),
	}

	run := func(ctx context.Context) {
		for {
			select {
			case v := <-c.ch:
				fmt.Printf("GOT: %d\n", v)
			case <-ctx.Done():
				fmt.Println("Stopping")
				return
			}
		}
	}

	c.actor.Start(run)
	c.ch <- 1
	c.ch <- 2
	c.actor.Stop(context.Background())

}

func run(ctx context.Context) {
	tkr := time.NewTicker(time.Millisecond)
	for {
		select {
		case <-tkr.C:
			fmt.Println("tick")
		case <-ctx.Done():
			return
		}
	}
}
Output:

GOT: 1
GOT: 2
Stopping

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Goroutine

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

Goroutine manages an actor goroutine, supporting starting and later stopping the goroutine. This is one-shot: once started and stopped, the goroutine cannot be started again.

The zero value is a valid initial state.

func (*Goroutine) HookLifecycle

func (gr *Goroutine) HookLifecycle(lc fx.Lifecycle, run RunFunc)

HookLifecycle connects this goroutine to the given fx.Lifecycle, starting and stopping it with the lifecycle. Use this method _or_ the Start and Stop methods, but not both.

func (*Goroutine) Start

func (gr *Goroutine) Start(run RunFunc)

Start starts run in a goroutine, setting up to stop it by cancelling the context it receives.

func (*Goroutine) Stop

func (gr *Goroutine) Stop(ctx context.Context) error

Stop stops the goroutine, waiting until it is complete, or the given context is cancelled, before returning. Returns the error from context if it is cancelled.

type RunFunc

type RunFunc func(context.Context)

RunFunc defines the function implementing the actor's event loop. It should run until the passed context is cancelled.

Jump to

Keyboard shortcuts

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