• Stars
    star
    15
  • Rank 1,371,379 (Top 28 %)
  • Language
    Go
  • License
    MIT License
  • Created almost 7 years ago
  • Updated about 2 years 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 alternative to OpenAI, Claude and others. Self-hosted 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. Features: Generate Text, Audio, Video, Images, Voice Cloning, Distributed inference
C++
24,299
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
574
star
3

LocalAGI

100% Local AGI with LocalAI
Python
392
star
4

luet

๐Ÿ“ฆ ๐Ÿณ 0-dependency Container-based Package Manager using SAT solver and QLearning
Go
261
star
5

poco

๐Ÿšข poCo - portable Containers. Create statically linked, portable binaries from container images (daemonless)
Go
84
star
6

yip

๐Ÿ“Œ Yaml Instructions Processor - Simply applies a cloud-init style yaml file to the system
Go
70
star
7

golauncher

๐Ÿš€ Highly extensible, customizable application launcher and window switcher written in less than 300 lines of Golang and fyne
Go
63
star
8

docker-companion

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

edgevpn-gui

Graphical front-end for EdgeVPN
Go
23
star
10

go-stable-diffusion

C++
15
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
8
star
14

linuxbundles

๐Ÿง Standalone, local-runnable binaries of popular linux distributions
Shell
8
star
15

vhproxy

VHProxy public git repository
Perl
7
star
16

luet-k8s

Luet extension to build packages on kubernetes
Go
7
star
17

go-piper

C++
7
star
18

WebService-GialloZafferano

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

entities

๐Ÿ” Declarative modern identity manager for UNIX systems in Go
Go
5
star
20

go-ggllm.cpp

Golang bindings for ggllm.cpp
C++
5
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

go-kdetect

golang kernel driver detector
Go
3
star
27

WebApp-GitInsight

WebApp in mojolicious for GitInsight
CSS
3
star
28

LocalAI-examples

LocalAI examples
Jupyter Notebook
3
star
29

blog

My blog, now migrated to hugo on github
HTML
2
star
30

docker-sabayon-spinbase-amd64

Sabayon spinbase docker repository
Shell
2
star
31

ekcp

๐ŸŒ  Ephemeral kubernetes cluster provider
JavaScript
2
star
32

Algorithm-Sat-Backtracking

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

docker-sabayon-builder-amd64

Sabayon builder base image docker repository
Shell
2
star
34

boson

a docker polling job processor
Go
2
star
35

gluedd-cli

Deepdetect jpeg streamer predictor
Go
2
star
36

go-processmanager

Go
2
star
37

localai-github-action

LocalAI github action
2
star
38

Algorithm-QLearning

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

go-udp-proxy

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

k8s-resource-scheduler

๐Ÿฏ Simple (toy/experimental) CPU/Memory pod scheduler
Go
1
star
41

android-builds-recipes

๐Ÿณ Android builds recipes for various devices/ROM with docker
Shell
1
star
42

go-mirror-redirector

Simple mirror redirector in golang
Go
1
star
43

fleet-sample

Dockerfile
1
star
44

builder-witchcraft

Shell
1
star
45

go-findpeaks

Go
1
star
46

openqa-scheduler-go

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

perl_training

TeX
1
star
48

poco-github-action

Github action for poco app bundler
Go
1
star
49

cobra-extensions

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

Mojolicious-Plugin-Angular-MaterialDesign

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

go-libp2p-simple-raft

A simple raft wrapper for libp2p
Go
1
star