mqttclient

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 14, 2025 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidConfig        = errors.New("invalid MQTT client configuration")
	ErrClientNotConnected   = errors.New("MQTT client is not connected")
	ErrClientConnected      = errors.New("MQTT client is connected")
	ErrClientConnecting     = errors.New("MQTT client is connecting")
	ErrMaxReconnectAttempts = errors.New("maximum reconnect attempts reached")
)
View Source
var (
	DroppedMessages = prometheus.NewCounterVec(
		prometheus.CounterOpts{
			Name: "mqtt_dropped_messages_total",
			Help: "Total dropped messages by reason",
		},
		[]string{"reason"},
	)
)

Functions

This section is empty.

Types

type ClientOption

type ClientOption func(*MQTTClient)

ClientOption customizes the MQTT client behavior

func WithLogger

func WithLogger(logger *zap.Logger) ClientOption

WithLogger sets the logger for the MQTT client

func WithMessageHandler

func WithMessageHandler(handler MessageHandler) ClientOption

type ConfigOption

type ConfigOption func(*MQTTConfig)

ConfigOption configures the MQTT client

func WithBroker

func WithBroker(broker string) ConfigOption

WithBroker sets the broker address

func WithCleanSession

func WithCleanSession(cleanSession bool) ConfigOption

WithCleanSession sets the clean session flag

func WithClientID

func WithClientID(clientID string) ConfigOption

WithClientID sets the client ID

func WithConnectTimeout

func WithConnectTimeout(timeout time.Duration) ConfigOption

WithConnectTimeout sets the connection timeout

func WithCredentials

func WithCredentials(username, password string) ConfigOption

WithCredentials sets the username and password for authentication

func WithFullChannelRetries

func WithFullChannelRetries(retries int) ConfigOption

WithFullChannelRetries sets the number of retries for full channels

func WithFullChannelStrategy

func WithFullChannelStrategy(strategy FullChannelStrategy) ConfigOption

WithFullChannelStrategy sets the strategy for handling full message channels

func WithHealthCheck

func WithHealthCheck(interval time.Duration, activeCheckEnabled bool, activeCheckTopic *string) ConfigOption

WithHealthCheck sets the health check configuration

func WithKeepAlive

func WithKeepAlive(keepalive time.Duration) ConfigOption

WithKeepAlive sets the keepalive interval

func WithMessageChannelSize

func WithMessageChannelSize(size int) ConfigOption

WithMessageChannelSize sets the size of the message channel

func WithPort

func WithPort(port int) ConfigOption

WithPort sets the broker port

func WithQoS

func WithQoS(qos byte) ConfigOption

WithQoS sets the Quality of Service level

func WithReconnectOnDisconnect

func WithReconnectOnDisconnect(initialDelay, maxDelay time.Duration, delayMultiplier float64, maxRetries int) ConfigOption

WithReconnectOnDisconnect sets the reconnect configuration

func WithTopic

func WithTopic(topic string) ConfigOption

WithTopic sets the topic to subscribe to

type ConnectionState

type ConnectionState int32

Constants and Types

const (
	StateDisconnected ConnectionState = iota
	StateConnecting
	StateConnected
)

type FullChannelStrategy

type FullChannelStrategy int

FullChannelStrategy defines the strategy for handling full message channels

const (
	DropOldest FullChannelStrategy = iota // Default
	DropCurrent
	Block
)

type HealthCheckConfig

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

type MQTTClient

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

MQTTClient represents a managed MQTT client connection

func NewMQTTClient

func NewMQTTClient(cfg *MQTTConfig, opts ...ClientOption) (*MQTTClient, error)

NewMQTTClient creates a new managed MQTT client

func (*MQTTClient) AddHandler

func (c *MQTTClient) AddHandler(handler MessageHandler)

AddHandler adds a message handler to the client

func (*MQTTClient) Close

func (c *MQTTClient) Close() error

Close closes the MQTT client connection

func (*MQTTClient) Connect

func (c *MQTTClient) Connect() error

Connect establishes a connection to the MQTT broker

func (*MQTTClient) GetMessageChannel

func (c *MQTTClient) GetMessageChannel() <-chan mqtt.Message

--------------------------------Message Handling---------------------------- GetMessageChannel returns the message channel for receiving messages

func (*MQTTClient) State

func (m *MQTTClient) State() ConnectionState

func (*MQTTClient) Subscribe

func (c *MQTTClient) Subscribe(ctx context.Context) error

-------------------------------Subscription Management---------------------------- Subscribe subscribes to the configured topic

type MQTTConfig

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

MQTTConfig holds client configuration

func NewMQTTConfig

func NewMQTTConfig(opts ...ConfigOption) (*MQTTConfig, error)

NewMQTTConfig creates a new MQTTConfig with default values

func (*MQTTConfig) GetBroker

func (c *MQTTConfig) GetBroker() string

Getter functions GetBroker returns the broker address

func (*MQTTConfig) GetCleanSession

func (c *MQTTConfig) GetCleanSession() bool

GetCleanSession returns the clean session flag

func (*MQTTConfig) GetClientID

func (c *MQTTConfig) GetClientID() string

GetClientID returns the client ID

func (*MQTTConfig) GetConnectTimeout

func (c *MQTTConfig) GetConnectTimeout() time.Duration

GetConnectTimeout returns the connection timeout

func (*MQTTConfig) GetFullChannelRetries

func (c *MQTTConfig) GetFullChannelRetries() int

GetFullChannelRetries returns the number of retries for full channels

func (*MQTTConfig) GetFullChannelStrategy

func (c *MQTTConfig) GetFullChannelStrategy() FullChannelStrategy

GetFullChannelStrategy returns the strategy for handling full message channels

func (*MQTTConfig) GetHealthCheckConfig

func (c *MQTTConfig) GetHealthCheckConfig() HealthCheckConfig

GetHealthCheckConfig returns the health check configuration

func (*MQTTConfig) GetKeepAlive

func (c *MQTTConfig) GetKeepAlive() time.Duration

GetKeepAlive returns the keepalive interval

func (*MQTTConfig) GetMessageChannelSize

func (c *MQTTConfig) GetMessageChannelSize() int

GetMessageChannelSize returns the size of the message channel

func (*MQTTConfig) GetPort

func (c *MQTTConfig) GetPort() int

GetPort returns the broker port

func (*MQTTConfig) GetQoS

func (c *MQTTConfig) GetQoS() byte

GetQoS returns the Quality of Service level

func (*MQTTConfig) GetReconnectConfig

func (c *MQTTConfig) GetReconnectConfig() *ReconnectConfig

GetReconnectConfig returns the reconnect configuration

func (*MQTTConfig) GetTopic

func (c *MQTTConfig) GetTopic() string

GetTopic returns the topic to subscribe to

type MessageHandler

type MessageHandler func(mqtt.Message) error

MessageHandler processes incoming MQTT messages

type MessageQueue

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

MessageQueue handles queuing and processing messages

func NewMessageQueue

func NewMessageQueue(bufferSize int, workers int) *MessageQueue

NewMessageQueue creates a new message queue with a specified buffer size

func (*MessageQueue) Enqueue

func (mq *MessageQueue) Enqueue(msg mqtt.Message)

Enqueue adds a message to the queue

func (*MessageQueue) StartProcessing

func (mq *MessageQueue) StartProcessing(handler func(mqtt.Message))

StartProcessing starts worker goroutines to process messages from the queue

type ReconnectConfig

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

ReconnectConfig holds configuration for reconnecting

Jump to

Keyboard shortcuts

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