Skip to content

Update configuration to handle starting broker and fail over#34

Merged
rustyeddy merged 7 commits into
mainfrom
config
Dec 1, 2025
Merged

Update configuration to handle starting broker and fail over#34
rustyeddy merged 7 commits into
mainfrom
config

Conversation

@rustyeddy
Copy link
Copy Markdown
Owner

Handle starting the broker and handling fail over in the event a broker is already running.

@rustyeddy rustyeddy self-assigned this Dec 1, 2025
Copilot AI review requested due to automatic review settings December 1, 2025 16:49
@rustyeddy rustyeddy added the enhancement New feature or request label Dec 1, 2025
@rustyeddy
Copy link
Copy Markdown
Owner Author

@copilot please review

Copy link
Copy Markdown
Contributor

Copilot AI commented Dec 1, 2025

@rustyeddy I've opened a new pull request, #35, to work on those changes. Once the pull request is ready, I'll request review from you.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors the MQTT broker lifecycle management in the Otto framework by moving broker startup/shutdown logic from the OttO struct into the messanger package, introducing lazy initialization in GetMessanger(), and implementing failover handling when a broker is already running.

Key Changes:

  • Centralized broker lifecycle management using a global shutdown variable in messanger/mqtt_broker.go
  • Refactored GetMessanger() to perform lazy initialization with configuration priority: config → environment variables → default "otto" broker
  • Simplified OttO.Init() by delegating messanger creation to GetMessanger() and removed the brokerShutdown field

Reviewed changes

Copilot reviewed 8 out of 9 changed files in this pull request and generated 13 comments.

Show a summary per file
File Description
otto.go Removed broker startup logic from Init() and brokerShutdown field; simplified Stop() to call messanger.StopMQTTBroker()
otto_test.go Commented out assertions for removed brokerShutdown field
messanger/mqtt_broker.go Added global shutdown variable and StopMQTTBroker() function for centralized broker lifecycle
messanger/messanger.go Refactored GetMessanger() with lazy initialization and environment variable support; changed NewMessanger() parameter from id to broker
messanger/mqtt.go Changed default credentials from "otto"/"otto123" to empty strings
messanger/messanger_test.go Updated test expectations to match new behavior where unknown IDs create default MQTT messangers
config.go New file embedding messanger.Config for top-level configuration
.gitignore Added coverage.* pattern to ignore coverage output files

Comment thread otto_test.go Outdated
Comment thread otto.go
Comment on lines 233 to +235
if o.Messanger != nil {
o.Messanger.Close()
}

if o.brokerShutdown != nil {
_ = o.brokerShutdown(context.Background())
messanger.StopMQTTBroker(context.Background())
Copy link

Copilot AI Dec 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The StopMQTTBroker is called unconditionally in the Stop() method even when a Messanger might be using a remote broker (not the embedded one). This could cause issues if:

  1. The global shutdown function was set by a different component
  2. Multiple OttO instances are running
  3. The Messanger is using a remote broker

Consider tracking whether this OttO instance started the broker before attempting to stop it, or handle errors from StopMQTTBroker more explicitly.

Copilot uses AI. Check for mistakes.
Comment thread messanger/messanger.go Outdated
Comment thread messanger/messanger.go
Comment thread messanger/messanger.go
Comment thread messanger/mqtt.go
Comment thread messanger/messanger_test.go
Comment thread messanger/messanger.go
Comment on lines +128 to +143
func NewMessanger(broker string) (m Messanger) {

switch broker {
case "none":
m = NewMessangerLocal(id)
case "mqtt":
m = NewMessangerMQTT(id, messangerConfig.Broker)
case "local":
msgr = NewMessangerLocal(broker)

case "otto":
_, err := StartMQTTBroker(context.Background())
if err != nil {
slog.Error("Failed to start embedded MQTT broker", "error", err)
return nil
}
m = NewMessangerMQTT(id, messangerConfig.Broker)
msgr = NewMessangerMQTT(broker, broker)

default:
slog.Error("Unknown messanger ID", "id", id)
return nil
msgr = NewMessangerMQTT(broker, broker)
Copy link

Copilot AI Dec 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The NewMessanger function signature has changed from taking an id parameter to taking a broker parameter, but this is semantically confusing. In line 132, NewMessangerLocal(broker) is called where broker is "none", but "none" is being used as an ID, not a broker address. Similarly, in line 140, NewMessangerMQTT(broker, broker) is called where the first parameter should be an ID but is receiving "otto" (the broker type). This parameter naming and usage is inconsistent and could lead to confusion.

Copilot uses AI. Check for mistakes.
Comment thread messanger/messanger.go
Comment on lines 135 to 139
_, err := StartMQTTBroker(context.Background())
if err != nil {
slog.Error("Failed to start embedded MQTT broker", "error", err)
return nil
}
Copy link

Copilot AI Dec 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The NewMessanger function discards the shutdown function returned by StartMQTTBroker at line 135, which means there's no way to gracefully shutdown the broker started by this function. This differs from GetMessanger which properly stores it in the global shutdown variable. This creates an inconsistency where brokers started via NewMessanger("otto") cannot be cleanly shut down.

Copilot uses AI. Check for mistakes.
Comment thread otto.go
Copy link
Copy Markdown
Contributor

Copilot AI commented Dec 1, 2025

@rustyeddy I've opened a new pull request, #37, to work on those changes. Once the pull request is ready, I'll request review from you.

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

Copilot AI commented Dec 1, 2025

@rustyeddy I've opened a new pull request, #38, to work on those changes. Once the pull request is ready, I'll request review from you.

@rustyeddy
Copy link
Copy Markdown
Owner Author

Changes have been merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants