events

package
v0.46.0 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2026 License: Apache-2.0 Imports: 11 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const (
	// DefStreamMaxLen is the default approx number of messages in the Redis event stream.
	DefStreamMaxLen = 100000

	// DefBufferSize is the default capacity of the publish
	// buffer channel.
	DefBufferSize = 10000

	// DefDrainInitialInterval is the initial sleep duration between XADD retries when
	// emitting an event fails.
	DefDrainInitialInterval = time.Second

	// DefDrainMaxBackoff is the maximum backoff duration between XADD retries.
	DefDrainMaxBackoff = 30 * time.Second

	// DefShutdownDrainTimeout is the maximum duration that Close() can spends trying to drain
	// the remaining buffered events.
	DefShutdownDrainTimeout = 5 * time.Second
)
View Source
const (

	// Event operation strings
	ThingCreate                = thingPrefix + "create"
	ThingUpdate                = thingPrefix + "update"
	ThingUpdateGroupAndProfile = thingPrefix + "update_group_and_profile"
	ThingUpdateMetadata        = thingPrefix + "update_metadata"
	ThingUpdateExternalKey     = thingPrefix + "update_external_key"
	ThingRemoveExternalKey     = thingPrefix + "remove_external_key"
	ThingRemove                = thingPrefix + "remove"

	ProfileCreate = profilePrefix + "create"
	ProfileUpdate = profilePrefix + "update"
	ProfileRemove = profilePrefix + "remove"

	GroupCreate = groupPrefix + "create"
	GroupUpdate = groupPrefix + "update"
	GroupRemove = groupPrefix + "remove"

	GroupMembershipCreate = groupMembershipPrefix + "create"
	GroupMembershipUpdate = groupMembershipPrefix + "update"
	GroupMembershipRemove = groupMembershipPrefix + "remove"

	OrgCreate = orgPrefix + "create"
	OrgUpdate = orgPrefix + "update"
	OrgRemove = orgPrefix + "remove"

	OrgMembershipCreate = orgMembershipPrefix + "create"
	OrgMembershipUpdate = orgMembershipPrefix + "update"
	OrgMembershipRemove = orgMembershipPrefix + "remove"

	OrgInviteCreate  = orgInvitePrefix + "create"
	OrgInviteRevoke  = orgInvitePrefix + "revoke"
	OrgInviteAccept  = orgInvitePrefix + "accept"
	OrgInviteDecline = orgInvitePrefix + "decline"

	APIKeyIssue  = apiKeyPrefix + "issue"
	APIKeyRevoke = apiKeyPrefix + "revoke"

	SessionLogoutAll = sessionPrefix + "logout_all"

	UserRegister          = userPrefix + "register"
	UserLogin             = userPrefix + "login"
	UserLoginFail         = userPrefix + "login_fail"
	UserUpdate            = userPrefix + "update"
	UserChangePassword    = userPrefix + "change_password"
	UserResetPassword     = userPrefix + "reset_password"
	UserEnable            = userPrefix + "enable"
	UserDisable           = userPrefix + "disable"
	UserAssignAdminRole   = userPrefix + "assign_admin_role"
	UserUnassignAdminRole = userPrefix + "unassign_admin_role"
	UserImpersonate       = userPrefix + "impersonate"

	PlatformInviteCreate = platformInvitePrefix + "create"
	PlatformInviteRevoke = platformInvitePrefix + "revoke"

	// Redis event streams
	ThingsStream = mainfluxPrefix + "things"
	AuthStream   = mainfluxPrefix + "auth"
	UsersStream  = mainfluxPrefix + "users"
)

Variables

View Source
var (
	// ErrEmptyStream is returned when stream name is empty.
	ErrEmptyStream = errors.New("stream name cannot be empty")

	// ErrEmptyName is returned when the subscriber name is empty.
	ErrEmptyName = errors.New("subscriber name cannot be empty")
)

Functions

This section is empty.

Types

type APIKeyIssued added in v0.46.0

type APIKeyIssued struct {
	ID        string
	ExpiresAt time.Time
}

APIKeyIssued signals that a user issued an API key, without the key secret.

func (APIKeyIssued) Encode added in v0.46.0

func (e APIKeyIssued) Encode() map[string]any

func (APIKeyIssued) Operation added in v0.46.0

func (e APIKeyIssued) Operation() string

type APIKeyRevoked added in v0.46.0

type APIKeyRevoked struct {
	ID string
}

APIKeyRevoked signals that a user revoked an API key.

func (APIKeyRevoked) Encode added in v0.46.0

func (e APIKeyRevoked) Encode() map[string]any

func (APIKeyRevoked) Operation added in v0.46.0

func (e APIKeyRevoked) Operation() string

type Event

type Event struct {
	// The specific EventAction that occurred.
	Action EventAction
	// The identity of the user whose action triggered the event.
	JWTUserIdentity domain.Identity
	// The ID of the organization to which the event belongs. May be empty.
	OrgID string
	// The ID of the group to which the event belongs. May be empty.
	GroupID    string
	OccurredAt time.Time
}

Event is the type representing a platform event.

func (Event) Encode

func (e Event) Encode() redisEvent

Encode turns an Event into a redisEvent

type EventAction added in v0.41.0

type EventAction interface {
	// Encode returns a map representation of the event action.
	Encode() map[string]any

	// Operation returns the string representation of the event action operation.
	Operation() string
}

EventAction represents an action occurring on the platform.

type EventHandler

type EventHandler interface {
	Handle(ctx context.Context, event Event) error
}

EventHandler reacts to a single event delivered by a Subscriber.

type GroupCreated added in v0.46.0

type GroupCreated struct {
	ID          string
	Name        string
	Description string
	Metadata    map[string]any
}

GroupCreated signals the creation of a group.

func (GroupCreated) Encode added in v0.46.0

func (e GroupCreated) Encode() map[string]any

func (GroupCreated) Operation added in v0.46.0

func (e GroupCreated) Operation() string

type GroupMembershipCreated added in v0.46.0

type GroupMembershipCreated struct {
	GroupID  string
	MemberID string
	Role     string
}

GroupMembershipCreated signals that a member was added to a group.

func (GroupMembershipCreated) Encode added in v0.46.0

func (e GroupMembershipCreated) Encode() map[string]any

func (GroupMembershipCreated) Operation added in v0.46.0

func (e GroupMembershipCreated) Operation() string

type GroupMembershipRemoved added in v0.46.0

type GroupMembershipRemoved struct {
	GroupID  string
	MemberID string
}

GroupMembershipRemoved signals that a member was removed from a group.

func (GroupMembershipRemoved) Encode added in v0.46.0

func (e GroupMembershipRemoved) Encode() map[string]any

func (GroupMembershipRemoved) Operation added in v0.46.0

func (e GroupMembershipRemoved) Operation() string

type GroupMembershipUpdated added in v0.46.0

type GroupMembershipUpdated struct {
	GroupID  string
	MemberID string
	Role     string
}

GroupMembershipUpdated signals that a group member's role was changed.

func (GroupMembershipUpdated) Encode added in v0.46.0

func (e GroupMembershipUpdated) Encode() map[string]any

func (GroupMembershipUpdated) Operation added in v0.46.0

func (e GroupMembershipUpdated) Operation() string

type GroupRemoved added in v0.41.0

type GroupRemoved struct {
	ID       string
	ThingIDs []string
}

GroupRemoved signals that a group has been removed.

func (GroupRemoved) Encode added in v0.41.0

func (e GroupRemoved) Encode() map[string]any

func (GroupRemoved) Operation added in v0.41.0

func (e GroupRemoved) Operation() string

type GroupUpdated added in v0.46.0

type GroupUpdated struct {
	ID          string
	Name        string
	Description string
	Metadata    map[string]any
}

GroupUpdated signals a group update.

func (GroupUpdated) Encode added in v0.46.0

func (e GroupUpdated) Encode() map[string]any

func (GroupUpdated) Operation added in v0.46.0

func (e GroupUpdated) Operation() string

type OrgCreated added in v0.41.0

type OrgCreated struct {
	ID string
}

OrgCreated signals the creation of an organization.

func (OrgCreated) Encode added in v0.41.0

func (e OrgCreated) Encode() map[string]any

func (OrgCreated) Operation added in v0.41.0

func (e OrgCreated) Operation() string

type OrgInviteAccepted added in v0.46.0

type OrgInviteAccepted struct {
	ID string
}

OrgInviteAccepted signals that an organization invite was accepted by its invitee.

func (OrgInviteAccepted) Encode added in v0.46.0

func (e OrgInviteAccepted) Encode() map[string]any

func (OrgInviteAccepted) Operation added in v0.46.0

func (e OrgInviteAccepted) Operation() string

type OrgInviteCreated added in v0.46.0

type OrgInviteCreated struct {
	ID           string
	InviteeID    string
	InviteeEmail string
	InviteeRole  string
	GroupInvites []domain.GroupInvite
}

OrgInviteCreated signals that a user was invited to an organization.

func (OrgInviteCreated) Encode added in v0.46.0

func (e OrgInviteCreated) Encode() map[string]any

func (OrgInviteCreated) Operation added in v0.46.0

func (e OrgInviteCreated) Operation() string

type OrgInviteDeclined added in v0.46.0

type OrgInviteDeclined struct {
	ID string
}

OrgInviteDeclined signals that an organization invite was declined by its invitee.

func (OrgInviteDeclined) Encode added in v0.46.0

func (e OrgInviteDeclined) Encode() map[string]any

func (OrgInviteDeclined) Operation added in v0.46.0

func (e OrgInviteDeclined) Operation() string

type OrgInviteRevoked added in v0.46.0

type OrgInviteRevoked struct {
	ID string
}

OrgInviteRevoked signals that an organization invite was revoked by its inviter.

func (OrgInviteRevoked) Encode added in v0.46.0

func (e OrgInviteRevoked) Encode() map[string]any

func (OrgInviteRevoked) Operation added in v0.46.0

func (e OrgInviteRevoked) Operation() string

type OrgMembershipCreated added in v0.46.0

type OrgMembershipCreated struct {
	OrgID    string
	MemberID string
	Role     string
}

OrgMembershipCreated signals that a member was added to an organization.

func (OrgMembershipCreated) Encode added in v0.46.0

func (e OrgMembershipCreated) Encode() map[string]any

func (OrgMembershipCreated) Operation added in v0.46.0

func (e OrgMembershipCreated) Operation() string

type OrgMembershipRemoved added in v0.46.0

type OrgMembershipRemoved struct {
	OrgID    string
	MemberID string
}

OrgMembershipRemoved signals that a member was removed from an organization.

func (OrgMembershipRemoved) Encode added in v0.46.0

func (e OrgMembershipRemoved) Encode() map[string]any

func (OrgMembershipRemoved) Operation added in v0.46.0

func (e OrgMembershipRemoved) Operation() string

type OrgMembershipUpdated added in v0.46.0

type OrgMembershipUpdated struct {
	OrgID    string
	MemberID string
	Role     string
}

OrgMembershipUpdated signals that an organization member's role was changed.

func (OrgMembershipUpdated) Encode added in v0.46.0

func (e OrgMembershipUpdated) Encode() map[string]any

func (OrgMembershipUpdated) Operation added in v0.46.0

func (e OrgMembershipUpdated) Operation() string

type OrgRemoved added in v0.41.0

type OrgRemoved struct {
	ID string
}

OrgRemoved signals that an organization has been removed.

func (OrgRemoved) Encode added in v0.41.0

func (e OrgRemoved) Encode() map[string]any

func (OrgRemoved) Operation added in v0.41.0

func (e OrgRemoved) Operation() string

type OrgUpdated added in v0.46.0

type OrgUpdated struct {
	ID          string
	Name        string
	Description string
	Metadata    map[string]any
}

OrgUpdated signals an organization update.

func (OrgUpdated) Encode added in v0.46.0

func (e OrgUpdated) Encode() map[string]any

func (OrgUpdated) Operation added in v0.46.0

func (e OrgUpdated) Operation() string

type PlatformInviteCreated added in v0.46.0

type PlatformInviteCreated struct {
	ID           string
	InviteeEmail string
}

PlatformInviteCreated signals that a person was invited to register on the platform.

func (PlatformInviteCreated) Encode added in v0.46.0

func (e PlatformInviteCreated) Encode() map[string]any

func (PlatformInviteCreated) Operation added in v0.46.0

func (e PlatformInviteCreated) Operation() string

type PlatformInviteRevoked added in v0.46.0

type PlatformInviteRevoked struct {
	ID string
}

PlatformInviteRevoked signals that a platform owner or admin revoked a platform invite.

func (PlatformInviteRevoked) Encode added in v0.46.0

func (e PlatformInviteRevoked) Encode() map[string]any

func (PlatformInviteRevoked) Operation added in v0.46.0

func (e PlatformInviteRevoked) Operation() string

type ProfileCreated added in v0.41.0

type ProfileCreated struct {
	ID       string
	GroupID  string
	Name     string
	Metadata map[string]any
}

ProfileCreated signals the creation of a profile.

func (ProfileCreated) Encode added in v0.41.0

func (e ProfileCreated) Encode() map[string]any

func (ProfileCreated) Operation added in v0.41.0

func (e ProfileCreated) Operation() string

type ProfileRemoved added in v0.41.0

type ProfileRemoved struct {
	ID string
}

ProfileRemoved signals that a profile has been removed.

func (ProfileRemoved) Encode added in v0.41.0

func (e ProfileRemoved) Encode() map[string]any

func (ProfileRemoved) Operation added in v0.41.0

func (e ProfileRemoved) Operation() string

type ProfileUpdated added in v0.41.0

type ProfileUpdated struct {
	ID       string
	Name     string
	Config   *domain.ProfileConfig
	Metadata map[string]any
}

ProfileUpdated signals a profile update.

func (ProfileUpdated) Encode added in v0.41.0

func (e ProfileUpdated) Encode() map[string]any

func (ProfileUpdated) Operation added in v0.41.0

func (e ProfileUpdated) Operation() string

type Publisher added in v0.41.0

type Publisher interface {
	// Publish queues the passed event for delivery by sending it to the buffer
	// channel. It's asynchronous, meaning that it doesn't block the caller
	// and as such returns no errors. If the underlying event buffer is full,
	// the oldest queued event is dropped to make space.
	Publish(ctx context.Context, e Event)

	// Close stops the event drainer goroutine, attempts a final drain,
	// and closes the underlying Redis client.
	Close() error
}

Publisher delivers Events to the Redis event store asynchronously. It uses an in-memory buffer to queue and retry failed events.

func NewPublisher added in v0.41.0

func NewPublisher(cfg PublisherConfig, log logger.Logger) (Publisher, error)

NewPublisher returns a Publisher that buffers events in process and drains them to Redis in the background.

type PublisherConfig added in v0.41.0

type PublisherConfig struct {
	URL    string
	Stream string

	// MaxLen is the approximate stream retention (XADD MAXLEN ~).
	MaxLen int64

	// BufferSize is the capacity of the buffer channel holding events.
	BufferSize int

	// DrainInitialInterval is the initial XADD retry sleep on failure.
	DrainInitialInterval time.Duration

	// DrainMaxBackoff caps the exponential retry backoff.
	DrainMaxBackoff time.Duration

	// ShutdownDrainTimeout bounds the final drain in Close().
	ShutdownDrainTimeout time.Duration
}

type SessionLoggedOutAll added in v0.46.0

type SessionLoggedOutAll struct{}

SessionLoggedOutAll signals that a user ended all of their sessions.

func (SessionLoggedOutAll) Encode added in v0.46.0

func (e SessionLoggedOutAll) Encode() map[string]any

func (SessionLoggedOutAll) Operation added in v0.46.0

func (e SessionLoggedOutAll) Operation() string

type Subscriber

type Subscriber interface {
	// Subscribe subscribes to the event stream and consumes events.
	Subscribe(ctx context.Context, handler EventHandler) error

	// Close gracefully closes event subscriber's connection.
	Close() error
}

Subscriber specifies event subscription API.

func NewSubscriber added in v0.34.0

func NewSubscriber(cfg SubscriberConfig, log logger.Logger) (Subscriber, error)

NewSubscriber returns a Subscriber that reads from a Redis stream using XRead and tracks its position via a per-subscriber Redis key.

type SubscriberConfig added in v0.41.0

type SubscriberConfig struct {
	// URL is the Redis connection URL (e.g. redis://host:6379/0).
	URL string
	// Stream is the name of the Redis stream to read from.
	Stream string
	// Name identifies this subscriber. Used in the cursor key so
	// independent subscribers maintain independent cursors on the same
	// stream.
	Name string
}

SubscriberConfig holds the parameters for creating a Subscriber.

type ThingCreated added in v0.41.0

type ThingCreated struct {
	ID        string
	GroupID   string
	ProfileID string
	Name      string
	Metadata  map[string]any
}

ThingCreated signals the creation of a thing.

func (ThingCreated) Encode added in v0.41.0

func (e ThingCreated) Encode() map[string]any

func (ThingCreated) Operation added in v0.41.0

func (e ThingCreated) Operation() string

type ThingExternalKeyRemoved added in v0.46.0

type ThingExternalKeyRemoved struct {
	ID string
}

ThingExternalKeyRemoved signals that a thing's external key was removed.

func (ThingExternalKeyRemoved) Encode added in v0.46.0

func (e ThingExternalKeyRemoved) Encode() map[string]any

func (ThingExternalKeyRemoved) Operation added in v0.46.0

func (e ThingExternalKeyRemoved) Operation() string

type ThingExternalKeyUpdated added in v0.46.0

type ThingExternalKeyUpdated struct {
	ID string
}

ThingExternalKeyUpdated signals that a thing's external key was set, without the key itself.

func (ThingExternalKeyUpdated) Encode added in v0.46.0

func (e ThingExternalKeyUpdated) Encode() map[string]any

func (ThingExternalKeyUpdated) Operation added in v0.46.0

func (e ThingExternalKeyUpdated) Operation() string

type ThingGroupAndProfileUpdated added in v0.41.0

type ThingGroupAndProfileUpdated struct {
	ID        string
	ProfileID string
	GroupID   string
}

ThingGroupAndProfileUpdated signals a thing's group/profile reassignment.

func (ThingGroupAndProfileUpdated) Encode added in v0.41.0

func (e ThingGroupAndProfileUpdated) Encode() map[string]any

func (ThingGroupAndProfileUpdated) Operation added in v0.41.0

func (e ThingGroupAndProfileUpdated) Operation() string

type ThingMetadataUpdated added in v0.46.0

type ThingMetadataUpdated struct {
	ID       string
	Metadata map[string]any
}

ThingMetadataUpdated signals a merge of metadata keys into a thing's metadata.

func (ThingMetadataUpdated) Encode added in v0.46.0

func (e ThingMetadataUpdated) Encode() map[string]any

func (ThingMetadataUpdated) Operation added in v0.46.0

func (e ThingMetadataUpdated) Operation() string

type ThingRemoved added in v0.41.0

type ThingRemoved struct {
	ID string
}

ThingRemoved signals that a thing has been removed.

func (ThingRemoved) Encode added in v0.41.0

func (e ThingRemoved) Encode() map[string]any

func (ThingRemoved) Operation added in v0.41.0

func (e ThingRemoved) Operation() string

type ThingUpdated added in v0.41.0

type ThingUpdated struct {
	ID        string
	ProfileID string
	Name      string
	Metadata  map[string]any
}

ThingUpdated signals a thing update.

func (ThingUpdated) Encode added in v0.41.0

func (e ThingUpdated) Encode() map[string]any

func (ThingUpdated) Operation added in v0.41.0

func (e ThingUpdated) Operation() string

type UserAdminRoleAssigned added in v0.46.0

type UserAdminRoleAssigned struct {
	ID string
}

UserAdminRoleAssigned signals that the platform owner assigned the platform admin role to a user.

func (UserAdminRoleAssigned) Encode added in v0.46.0

func (e UserAdminRoleAssigned) Encode() map[string]any

func (UserAdminRoleAssigned) Operation added in v0.46.0

func (e UserAdminRoleAssigned) Operation() string

type UserAdminRoleUnassigned added in v0.46.0

type UserAdminRoleUnassigned struct {
	ID string
}

UserAdminRoleUnassigned signals that the platform owner removed the platform admin role from a user.

func (UserAdminRoleUnassigned) Encode added in v0.46.0

func (e UserAdminRoleUnassigned) Encode() map[string]any

func (UserAdminRoleUnassigned) Operation added in v0.46.0

func (e UserAdminRoleUnassigned) Operation() string

type UserDisabled added in v0.46.0

type UserDisabled struct {
	ID string
}

UserDisabled signals that a platform owner or admin disabled a user.

func (UserDisabled) Encode added in v0.46.0

func (e UserDisabled) Encode() map[string]any

func (UserDisabled) Operation added in v0.46.0

func (e UserDisabled) Operation() string

type UserEnabled added in v0.46.0

type UserEnabled struct {
	ID string
}

UserEnabled signals that a platform owner or admin enabled a user.

func (UserEnabled) Encode added in v0.46.0

func (e UserEnabled) Encode() map[string]any

func (UserEnabled) Operation added in v0.46.0

func (e UserEnabled) Operation() string

type UserImpersonated added in v0.46.0

type UserImpersonated struct {
	ID    string
	Email string
}

UserImpersonated signals that a platform admin started an impersonated session of a user.

func (UserImpersonated) Encode added in v0.46.0

func (e UserImpersonated) Encode() map[string]any

func (UserImpersonated) Operation added in v0.46.0

func (e UserImpersonated) Operation() string

type UserLoggedIn added in v0.46.0

type UserLoggedIn struct{}

UserLoggedIn signals that a user started a new session.

func (UserLoggedIn) Encode added in v0.46.0

func (e UserLoggedIn) Encode() map[string]any

func (UserLoggedIn) Operation added in v0.46.0

func (e UserLoggedIn) Operation() string

type UserLoginFailed added in v0.46.0

type UserLoginFailed struct {
	Email string
}

UserLoginFailed signals a login attempt with invalid credentials.

func (UserLoginFailed) Encode added in v0.46.0

func (e UserLoginFailed) Encode() map[string]any

func (UserLoginFailed) Operation added in v0.46.0

func (e UserLoginFailed) Operation() string

type UserPasswordChanged added in v0.46.0

type UserPasswordChanged struct {
	ID string
}

UserPasswordChanged signals that a user's password was changed by the user or a platform owner or admin.

func (UserPasswordChanged) Encode added in v0.46.0

func (e UserPasswordChanged) Encode() map[string]any

func (UserPasswordChanged) Operation added in v0.46.0

func (e UserPasswordChanged) Operation() string

type UserPasswordReset added in v0.46.0

type UserPasswordReset struct {
	ID string
}

UserPasswordReset signals that a user set a new password through the recovery flow.

func (UserPasswordReset) Encode added in v0.46.0

func (e UserPasswordReset) Encode() map[string]any

func (UserPasswordReset) Operation added in v0.46.0

func (e UserPasswordReset) Operation() string

type UserRegistered added in v0.46.0

type UserRegistered struct {
	ID       string
	Email    string
	InviteID string
}

UserRegistered signals that a new user account was created.

func (UserRegistered) Encode added in v0.46.0

func (e UserRegistered) Encode() map[string]any

func (UserRegistered) Operation added in v0.46.0

func (e UserRegistered) Operation() string

type UserUpdated added in v0.46.0

type UserUpdated struct {
	ID       string
	Metadata map[string]any
}

UserUpdated signals that a user changed their metadata.

func (UserUpdated) Encode added in v0.46.0

func (e UserUpdated) Encode() map[string]any

func (UserUpdated) Operation added in v0.46.0

func (e UserUpdated) Operation() string

Jump to

Keyboard shortcuts

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