Skip to content

feat(os/gevent): Add gevent package for event-driven architecture support #4365

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
14 changes: 14 additions & 0 deletions os/gevent/gevent.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.

// Package gevent provides event bus for event dispatching.
package gevent

// DefaultEventBus is the default event bus instance.
var DefaultEventBus = NewSeqEventBus(SeqEventBusOption{
QueueSize: 100,
WorkerSize: 10,
})
76 changes: 76 additions & 0 deletions os/gevent/gevent_base_event.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.

// Package gevent provides event bus for event dispatching.
package gevent

// BaseEventFactoryFunc is the default event factory function used to create basic events
var BaseEventFactoryFunc = func(topic string, params map[string]any, errorModel ErrorModel, execModel ExecModel) Event {
return &BaseEvent{
Topic: topic,
Data: params,
ErrorModel: errorModel,
ExecModel: execModel,
}
}

// BaseEvent is the basic implementation of an event
type BaseEvent struct {
Topic string // Event topic
Data map[string]any // Event data
ErrorModel ErrorModel // Error handling mode
ExecModel ExecModel // Execution mode (sequential or parallel)
}

// SetTopic sets the event topic
func (be *BaseEvent) SetTopic(topic string) {
be.Topic = topic
}

// GetTopic gets the event topic
func (be *BaseEvent) GetTopic() string {
return be.Topic
}

// SetData sets the event data
func (be *BaseEvent) SetData(data map[string]any) {
be.Data = data
}

// GetData gets the event data
func (be *BaseEvent) GetData() map[string]any {
return be.Data
}

// SetErrorModel sets the error handling mode
func (be *BaseEvent) SetErrorModel(model ErrorModel) {
be.ErrorModel = model
}

// GetErrorModel gets the error handling mode
func (be *BaseEvent) GetErrorModel() ErrorModel {
return be.ErrorModel
}

// GetExecModel gets the execution mode
func (be *BaseEvent) GetExecModel() ExecModel {
return be.ExecModel
}

// SetExecModel sets the execution mode
func (be *BaseEvent) SetExecModel(execModel ExecModel) {
be.ExecModel = execModel
}

// Clone creates a copy of the event
func (be *BaseEvent) Clone() Event {
return &BaseEvent{
Topic: be.Topic,
Data: be.Data,
ErrorModel: be.ErrorModel,
ExecModel: be.ExecModel,
}
}
87 changes: 87 additions & 0 deletions os/gevent/gevent_constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.

// Package gevent provides event bus for event dispatching.
package gevent

import (
"github.com/gogf/gf/v2/errors/gerror"
)

// Common error definitions
var (
// EventBusClosedError indicates that the event bus has been closed
EventBusClosedError = gerror.New("event bus closed")

// ChannelFullError indicates that the event channel is full
ChannelFullError = gerror.New("event channel full")

// TopicEmptyError indicates that the topic is empty
TopicEmptyError = gerror.New("topic is empty")

// HandlerNilError indicates that the handler is nil
HandlerNilError = gerror.New("handler is nil")

// NotFoundError indicates that the item is not found
NotFoundError = gerror.New("not found")

// SubscriberEmptyError indicates that there are no subscribers
SubscriberEmptyError = gerror.New("subscriber is empty")

// NoHandlerError indicates that there is no handler
NoHandlerError = gerror.New("no handler")

// EventNilError indicates that the event is nil
EventNilError = gerror.New("event is empty")

// FactoryFuncIsNilError indicates that the factory function is nil
FactoryFuncIsNilError = gerror.New("factory func is nil")
)

// Priority defines the priority levels for event handlers
type Priority int

const (
// PriorityNone indicates no priority
PriorityNone Priority = iota

// PriorityLow indicates low priority
PriorityLow

// PriorityNormal indicates normal priority (default)
PriorityNormal

// PriorityHigh indicates high priority
PriorityHigh

// PriorityUrgent indicates urgent priority
PriorityUrgent

// PriorityImmediate indicates immediate priority
PriorityImmediate
)

// ErrorModel defines how errors are handled during event processing
type ErrorModel int

const (
// Stop indicates that event processing should stop when an error occurs
Stop ErrorModel = iota

// Ignore indicates that errors should be ignored and processing should continue
Ignore
)

// ExecModel defines how events are executed
type ExecModel int

const (
// Seq indicates sequential execution
Seq ExecModel = iota

// Parallel indicates parallel execution
Parallel
)
50 changes: 50 additions & 0 deletions os/gevent/gevent_core.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.

// Package gevent provides event bus for event dispatching.
package gevent

// Event represents an event that can be published and handled
type Event interface {
// SetTopic sets the event topic
SetTopic(topic string)

// GetTopic gets the event topic
GetTopic() string

// SetData sets the event data
SetData(data map[string]any)

// GetData gets the event data
GetData() map[string]any

// SetErrorModel sets the error handling mode
SetErrorModel(model ErrorModel)

// GetErrorModel gets the error handling mode
GetErrorModel() ErrorModel

// SetExecModel sets the execution mode
SetExecModel(model ExecModel)

// GetExecModel gets the execution mode
GetExecModel() ExecModel

// Clone creates a copy of the event
Clone() Event
}

// HandlerFunc is the function signature for event handlers
type HandlerFunc func(e Event) error

// RecoverFunc is the function signature for error recovery handlers
type RecoverFunc func(e Event, err any)

// ErrorFunc is the function signature for error handlers
type ErrorFunc func(e Event, err error) error

// EventFactoryFunc is the function signature for event factory functions
type EventFactoryFunc func(topic string, params map[string]any, errorModel ErrorModel, execModel ExecModel) Event
Loading
Loading