gevent imply go-event which tries to make event handling easier.

gevent imply go-event which tries to make event handling easier.

What does gevent want to do

  1. Async execute jobs safely without too many go routines.
  2. Support delayed events to execute.
  3. Support to config executing options

Main usage scenarios:

  1. Separate side business from main, like system log which should be async.
  2. Use delayed event to confirm order status or pay status.
  3. Decouple domain events to avoid cycling call.
  4. Notify downstream while domain event happening.

Attention

  1. Instant event may delay a few milliseconds while dispatching.
  2. Local events are not durable, use redis durable model for distribute systems.

TODO

  1. Support use rmq etc.
  2. Benchmark

Any question or suggestion, please let me know.

How to use:

    go get github.com/ltwonders/gevent

Example:

package main

import (
	"context"
	"log"
	"time"

	"github.com/alicebob/miniredis"
	"github.com/gomodule/redigo/redis"
	"github.com/ltwonders/gevent"
)

func main() {
	ctx := context.Background()
	gevent.LocalInit()
	s, err0 := miniredis.Run()
	if err0 != nil {
		panic(err0)
	}
	defer s.Close()

	pool := &redis.Pool{
		MaxIdle: 2,
		Dial:    func() (redis.Conn, error) { return redis.Dial("tcp", s.Addr()) },
	}
	redisDispatcher := gevent.NewRedisWithContext(ctx, &gevent.ClientSimple{Pool: pool})

	//annoy func to handle event
	var annoyFunc = func(ctx context.Context, evt gevent.Event) error {
		log.Printf("instant event [%+v] start at %+v", evt, time.Now().Second())
		time.Sleep(2 * time.Second)
		log.Printf("instant event [%+v] finish at %+v", evt, time.Now().Second())
		return nil
	}
	if err1 := gevent.Local().Register(ctx, "instant", annoyFunc); nil != err1 {
		log.Printf("fail to register local handler")
	}

	type instantEvent struct {
		ID int
	}

	// dispatch events
	for i := 1; i <= 100; i++ {
		inst := &instantEvent{ID: i}
		if err := gevent.Dispatch(ctx, "instant", inst, gevent.Local(), redisDispatcher); nil != err {
			log.Printf("dispatch failed: %s", err)
		}
	}
	if err2 := redisDispatcher.Register(ctx, "instant", annoyFunc); nil != err2 {
		log.Printf("fail to register redis handler")
	}

	time.Sleep(200 * time.Second)
}

more examples, see example package

Similar Resources

FreeSWITCH Event Socket library for the Go programming language.

eventsocket FreeSWITCH Event Socket library for the Go programming language. It supports both inbound and outbound event socket connections, acting ei

Dec 11, 2022

File system event notification library on steroids.

notify Filesystem event notification library on steroids. (under active development) Documentation godoc.org/github.com/rjeczalik/notify Installation

Jan 7, 2023

Idiomatic Event Sourcing in Go

Idiomatic Event Sourcing in Go

Event Sourcing for Go Idiomatic library to help you build Event Sourced application in Go. Please note The library is currently under development and

Oct 27, 2022

An example event-driven application using Atmo and NATS

Atmo + NATS Example Project This repo is an example of using Atmo with NATS as a streaming messaging layer. In this example, Atmo connects to NATS and

Oct 27, 2021

The kprobe package allows construction of dynamic struct based on kprobe event format descriptions.

The kprobe package allows construction of dynamic struct based on kprobe event format descriptions.

Oct 27, 2021

Eye - An easy-use lib for event-driven pattern

📝 Eye Eye 是一个简单易用的事件驱动模式库。 Read me in English 🥇 功能特性 敬请期待。。。 历史版本的特性请查看 HISTOR

Jan 17, 2022

This is a simple chat app which shows how to use Go with Hotwire.

This is a simple chat app which shows how to use Go with Hotwire.

chat-hotwire-go This is a simple chat app which shows how to use Go with Hotwire. Messages from another user are received via Turbo Streams over a web

Jul 2, 2022

Build for all Go-supported platforms by default, disable those which you don't want.

bagop Build for all Go-supported platforms by default, disable those which you don't want. Overview bagop is a simple build tool for Go which tries to

Jul 29, 2022

Geth client which picks the most profitable blocks to mine using a greedy algorithm

Greeden-Geth Greeden-Geth is a protocol-agnostic client which uses a greedy algorithm to pick the most profitable blocks to submit to the network out

Nov 16, 2022
Related tags
An unified key management system to make life easier.
An unified key management system to make life easier.

Safebox An unified key management system to make life easier. The main goal of safebox is to make key backup easier with single main key to derive the

Nov 15, 2022
Build reply markup keyboards easier than ever.

gotgbot keyboard Build reply markup keyboards easier than ever.

Feb 24, 2022
A tool that makes vendoring your project easier to do

vendorme Vendorme is a tool that makes vendoring your project easier to do. It improves the developer workflow by giving you one single place to manag

Dec 16, 2022
CapMonsterTool is a set of Go tools designed to simply make requests to the CapMonster Cloud API.

✨ CapMonsterTool ✨ About this module What is ✨ CapMonsterTool ✨ ? CapMonsterTool is a set of Go tools designed to simply make requests to the CapMonst

Dec 1, 2022
Default godoc generator - make your first steps towards better code documentation

godoc-generate Overview godoc-generate is a simple command line tool that generates default godoc comments on all exported types, functions, consts an

Sep 14, 2022
Mpq Decoder Golang TODO: MAke Description

MPQ MPQ archive codec Report Bug · Request Feature About This package provides a MPQ archive codec, for compressing and decompressing MoPaQ archives.

Nov 2, 2021
TLDR Page Creator is a program designed to help users make TLDR pages

TLDR Page Creator is a program designed to help users make TLDR pages, while avoiding syntax errors from TLDR-style markdown.

Dec 3, 2022
Day-1 is apart of my 6 days of Christmas challenge where i write in two new languages everyday, and make something weird out of it.

Day-1 is apart of my 6 days of Christmas challenge where i write in two new languages everyday, and make something weird out of it. today was a HTTP server written with PostGreSQL using Golang, R, and shell script read more

Dec 21, 2021
This application is a tool that allows you to prepare mail designs specific to your group and make Christmas raffles.

What is this? This application is a tool that allows you to prepare mail designs specific to your group and make Christmas raffles. How is it working?

Dec 15, 2022
Golang-echo-sample - Make an out-of-the-box backend based on golang-echo

Golang-echo-sample - Make an out-of-the-box backend based on golang-echo

Dec 31, 2021