• Stars
    star
    15
  • Rank 1,325,546 (Top 27 %)
  • Language
    Go
  • License
    MIT License
  • Created over 6 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Minimalistic, pluggable Golang evloop/timer handler with dependency-injection

Anagent Build Status codecov Go Report Card godoc

Minimalistic, pluggable Golang evloop/timer handler with dependency-injection - based on codegangsta/inject - go-macaron/inject and chuckpreslar/emission.

Anagent is a lightweight library that allows you to plug inside to other event loops, or allows you to handle and create your own within your application - leaving the control to you.

It comes with dependency-injection from codegangsta/inject, and it's also a soft-wrapper to chuckpreslar/emission, adding to it dependency injection capabilities and timer handlers.

Usage

Event Emitter with Dependency injection

    package main

    import (
    	"log"
    	"github.com/mudler/anagent"
    )

    type TestTest struct {
    	Test string
    }

    func main() {
    	agent := anagent.New()
    	mytest := &TestTest{Test: "PONG!"}
    	agent.Map(mytest)

    	agent.Once("test", func(te *TestTest, l *log.Logger) {
    		if te.Test == "PONG!" {
    			l.Println("It just works!")
    		}
    	})

    	agent.Emit("test")
    }

What happened here? we mapped our structure instance (TestTest) inside the agent with (agent.Map()), and all fired events can access to them.

Timer / Reactor

    package main

    import "github.com/mudler/anagent"
    import "fmt"

    type TestTest struct {
            Test string
    }

    func main() {
            agent := anagent.New()
            mytest := &TestTest{Test: "PONG!"}
            agent.Map(mytest)

            agent.Emitter().On("test", func(s string) { fmt.Println("Received: " + s) })

            // Not recurring timer
            agent.TimerSeconds(int64(3), false, func(a *anagent.Anagent, te *TestTest) {
                    a.Emitter().Emit("test", te.Test)
                    go a.Stop()
            })

            agent.Start() // Loops here and never returns
    }

The code portion will start and wait for 3 seconds, then it will execute the callback (not recurring, that's why the false) that will fire a custom event defined before (note, it's not using the dependency-injection capabilities, thus it's accessing the emitter handler directly with agent.Emitter()).

The difference is that when we access to On() provided by agent.On(), we access to the agent dependencies, that have been mapped with agent.Map() - otherwise, with agent.Emitter().On() we are free to bind any arguments to the event callback.

After the event is fired, the timer stops the eventloop (a.Stop()), so the program returns.

Hook into other loops

It is often in other framework to use loop patterns, as example in framework for game development, network agents, and such. We can hook into other loops, and run the agent Step function, so we can still leverage the evloop functionalities.

    package main

    import "github.com/mudler/anagent"
    import "fmt"

    type TestTest struct {
    	Test string
    }

    func main() {
    	agent := anagent.New()
    	mytest := &TestTest{Test: "PONG!"}
    	agent.Map(mytest)

        // Reply with a delay of 2s
    	agent.Emitter().On("test", func(s string) {
    		agent.TimerSeconds(int64(2), false, func() {
    			fmt.Println("Received: " + s)
    		})
    	})

    	// Recurring timer
    	agent.TimerSeconds(int64(3), true, func(a *anagent.Anagent, te *TestTest) {
    		fmt.Println("PING!")
    		a.Emitter().Emit("test", te.Test)
    	})

    	for { // Infinite loop
    		agent.Step()
    	}
    }

More Repositories

1

LocalAI

🤖 The free, Open Source OpenAI alternative. Self-hosted, community-driven and local-first. Drop-in replacement for OpenAI running on consumer-grade hardware. No GPU required. Runs gguf, transformers, diffusers and many more models architectures. It allows to generate Text, Audio, Video, Images. Also with voice cloning capabilities.
C++
19,811
star
2

edgevpn

⛵ The immutable, decentralized, statically built p2p VPN without any central server and automatic discovery! Create decentralized introspectable tunnels over p2p with shared tokens
Go
474
star
3

luet

📦 🐳 0-dependency Container-based Package Manager using SAT solver and QLearning
Go
246
star
4

LocalAGI

100% Local AGI with LocalAI
Python
244
star
5

poco

🚢 poCo - portable Containers. Create statically linked, portable binaries from container images (daemonless)
Go
81
star
6

yip

📌 Yaml Instructions Processor - Simply applies a cloud-init style yaml file to the system
Go
62
star
7

golauncher

🚀 Highly extensible, customizable application launcher and window switcher written in less than 300 lines of Golang and fyne
Go
56
star
8

docker-companion

squash and unpack Docker images, in Golang
Go
42
star
9

edgevpn-gui

Graphical front-end for EdgeVPN
Go
18
star
10

go-stable-diffusion

C++
12
star
11

GitInsight

Predict your github contributions using Bayesian inference and Markov chain with perl and PDL
Perl
9
star
12

gh-k8s

Run multiple-node, decentralized k3s clusters on Github action runners for test and development!
Shell
9
star
13

go-pluggable

🍱 go-pluggable is a light Bus-event driven plugin library for Golang
Go
7
star
14

vhproxy

VHProxy public git repository
Perl
7
star
15

linuxbundles

🐧 Standalone, local-runnable binaries of popular linux distributions
Shell
7
star
16

luet-k8s

Luet extension to build packages on kubernetes
Go
7
star
17

WebService-GialloZafferano

Perl interface to GialloZafferano.it website to find cooking recipes
Perl
5
star
18

go-ggllm.cpp

Golang bindings for ggllm.cpp
C++
5
star
19

go-piper

C++
5
star
20

entities

🔏 Declarative modern identity manager for UNIX systems in Go
Go
4
star
21

img-controller

Kubernetes CRD controller to build docker images with img
Go
4
star
22

http

"http" IRC shellcode
Perl
4
star
23

docker-sabayon-base

Sabayon-base docker repository
PLpgSQL
3
star
24

App-whatthecommit

Add a prepare-commit-msg to your git repository that uses whatthecommit.com to generate random commit messages
Perl
3
star
25

go-nodepair

Golang library to handle transparent remote node pairing
Go
3
star
26

WebApp-GitInsight

WebApp in mojolicious for GitInsight
CSS
3
star
27

docker-sabayon-spinbase-amd64

Sabayon spinbase docker repository
Shell
2
star
28

docker-sabayon-builder-amd64

Sabayon builder base image docker repository
Shell
2
star
29

ekcp

🌠 Ephemeral kubernetes cluster provider
JavaScript
2
star
30

Algorithm-Sat-Backtracking

A switchable Pure Perl SAT solver with backtracking
Perl
2
star
31

boson

a docker polling job processor
Go
2
star
32

gluedd-cli

Deepdetect jpeg streamer predictor
Go
2
star
33

go-kdetect

golang kernel driver detector
Go
2
star
34

go-processmanager

Go
2
star
35

Algorithm-QLearning

Algorithm::QLearning - Reinforcement Learning done in Pure Perl
Perl
1
star
36

go-udp-proxy

Little udp proxy needed just releases with binaries built with CI - not my code, found on the net
Go
1
star
37

k8s-resource-scheduler

🏯 Simple (toy/experimental) CPU/Memory pod scheduler
Go
1
star
38

android-builds-recipes

🐳 Android builds recipes for various devices/ROM with docker
Shell
1
star
39

go-mirror-redirector

Simple mirror redirector in golang
Go
1
star
40

fleet-sample

Dockerfile
1
star
41

builder-witchcraft

Shell
1
star
42

go-findpeaks

Go
1
star
43

openqa-scheduler-go

Drop in replacement openQA scheduler written in Golang
Go
1
star
44

perl_training

TeX
1
star
45

blog

My blog, now migrated to hugo on github
HTML
1
star
46

Mojolicious-Plugin-Angular-MaterialDesign

Bundle MaterialDesign for angular in your mojolicious app
Perl
1
star
47

cobra-extensions

Create git-alike extensions for your cobra projects!
Go
1
star
48

poco-github-action

Github action for poco app bundler
Go
1
star