eventgoround

package module
v2.0.5 Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2026 License: MIT Imports: 6 Imported by: 0

README

Event Go Round

Go Reference Go Report Card

Event Go Round is a simple, thread-safe event scheduling and dispatch system for Go, originally designed for game backends. It allows you to schedule events to be executed at specific times with flexible event handlers.

Features

  • Thread-safe: Event dispatching is concurrent-safe and can run in multiple goroutines
  • Time-based scheduling: Schedule events for immediate, past, or future execution
  • Flexible event handlers: Implement your own event registry to handle events however you need
  • Panic recovery: Automatically recovers from panics in event handlers
  • Pause/Resume support: Control event loop execution dynamically
  • Decoupled design: Event handlers implement the simple IEventRegistry interface

Installation

go get github.com/tanerius/EventGoRound/v2

Quick Start

package main

import (
    "fmt"
    "time"
    eventgoround "github.com/tanerius/EventGoRound/v2"
)

// Implement IEventRegistry
type MyRegistry struct {
    handlers map[string]func(any)
}

func (r *MyRegistry) GetHandler(name string) (func(any), error) {
    if handler, exists := r.handlers[name]; exists {
        return handler, nil
    }
    return nil, fmt.Errorf("handler not found: %s", name)
}

func main() {
    // Create registry and register handlers
    registry := &MyRegistry{
        handlers: map[string]func(any){
            "greet": func(payload any) {
                fmt.Printf("Hello, %s!\n", payload.(string))
            },
        },
    }

    // Create and start event loop
    eventLoop := eventgoround.NewEventLoop(100*time.Millisecond, registry)
    eventLoop.Start()

    // Schedule an event
    eventLoop.ScheduleEvent(time.Now().UnixMilli(), 0, "greet", "World")

    time.Sleep(1 * time.Second)
    eventLoop.Stop()
}

API Overview

EventLoop

The main component for managing event scheduling and execution.

// Create a new event loop with specified tick interval
func NewEventLoop(tickInterval time.Duration, registry IEventRegistry) *EventLoop

// Start the event loop
func (el *EventLoop) Start()

// Stop the event loop
func (el *EventLoop) Stop()

// Schedule an event
func (el *EventLoop) ScheduleEvent(timestamp int64, duration int64, handlerName string, payload any)

// Pause/Resume event processing
func (el *EventLoop) Pause()
func (el *EventLoop) Resume()

// Check if catching up on past events
func (el *EventLoop) IsCatchingUp() bool
Event

Represents a scheduled event with timing and handler information.

type Event struct {
    Timestamp int64       // Unix timestamp in milliseconds
    Duration  int64       // Duration for the event
    Payload   interface{} // Event data
    Handler   string      // Name of the handler function
}
IEventRegistry

Interface that your event handler registry must implement.

type IEventRegistry interface {
    GetHandler(name string) (func(any), error)
}

Examples

See the examples directory for more detailed usage examples:

Use Cases

Event Go Round is perfect for:

  • Game server event scheduling
  • Time-based task execution
  • Background job processing
  • Event-driven architectures
  • Scheduled notifications and reminders

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Documentation

Index

Constants

View Source
const (
	// DefaultMaxBytes is the default maximum size of log file before rotation (10MB)
	DefaultMaxBytes = 10 * 1024 * 1024 // 10 megabytes
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Event

type Event struct {
	Timestamp int64       `json:"timestamp"`
	Duration  int64       `json:"duration"`
	Payload   interface{} `json:"payload"`
	Handler   string      `json:"handler"`
	// contains filtered or unexported fields
}

Event represents a scheduled event with a handler function

func (Event) Addhandler

func (e Event) Addhandler(h func(any))

type EventLoop

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

EventLoop manages the event scheduling and execution

func NewEventLoop

func NewEventLoop(tickInterval time.Duration, registry IEventRegistry, logConfig *LogConfig) *EventLoop

NewEventLoop creates a new event loop with the specified tick interval logConfig is optional - pass nil to disable logging

func (*EventLoop) IsCatchingUp

func (el *EventLoop) IsCatchingUp() bool

IsCatchingUp returns whether the loop is currently in catch-up mode

func (*EventLoop) IsPaused

func (el *EventLoop) IsPaused() bool

IsPaused returns whether the loop is currently paused

func (*EventLoop) Pause

func (el *EventLoop) Pause()

Pause pauses the event loop, preventing event scheduling and processing

func (*EventLoop) ScheduleEvent

func (el *EventLoop) ScheduleEvent(timestamp int64, duration int64, handlername string, payload any) error

ScheduleEvent schedules an event to be executed at the specified timestamp This will block during catch-up mode until all past events are processed Events cannot be scheduled when the loop is paused

func (*EventLoop) Start

func (el *EventLoop) Start()

Start begins the event loop processing

func (*EventLoop) Stop

func (el *EventLoop) Stop()

Stop gracefully stops the event loop

func (*EventLoop) Unpause

func (el *EventLoop) Unpause()

Unpause resumes the event loop, allowing event scheduling and processing

type IEventRegistry

type IEventRegistry interface {
	GetHandler(name string) (func(any), error)
}

Registry of event handlers. It allows you to retrieve events by name.

type LogConfig added in v2.0.4

type LogConfig struct {
	Enabled     bool   // Whether logging is enabled
	FilePath    string // Path to the log file
	IncludeInfo bool   // Whether to include INFO level logs (ERROR always logged when enabled)
}

LogConfig holds configuration for event loop logging

type RotatingFileWriter added in v2.0.4

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

RotatingFileWriter implements io.Writer with automatic file rotation when the file size exceeds a maximum threshold

func NewRotatingFileWriter added in v2.0.4

func NewRotatingFileWriter(filepath string, maxBytes int64) (*RotatingFileWriter, error)

NewRotatingFileWriter creates a new rotating file writer

func (*RotatingFileWriter) Close added in v2.0.4

func (rfw *RotatingFileWriter) Close() error

Close closes the underlying file

func (*RotatingFileWriter) Write added in v2.0.4

func (rfw *RotatingFileWriter) Write(p []byte) (n int, err error)

Write writes data to the file, rotating if necessary

Jump to

Keyboard shortcuts

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