Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions core/internal/service/relay/config_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,11 @@ func SyncRelayConfigsToPostfix(ctx context.Context) error {
} else {

activeConfigs = []*entity.BmRelayConfig{}
// Clear stale relay transport mappings to ensure fallback to original SMTP
_, err = g.DB().Model("bm_domain_smtp_transport").Where("atype", "relay").Delete()
if err != nil {
g.Log().Warningf(ctx, "Failed to clear relay transport mappings: %v", err)
Copy link

Copilot AI Dec 20, 2025

Choose a reason for hiding this comment

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

Error handling inconsistency: Similar database delete operation at line 863 returns an error when the delete fails, but here it only logs a warning. This inconsistency could mask database issues. Consider returning the error instead of just logging it to maintain consistent error handling behavior throughout the function.

Suggested change
g.Log().Warningf(ctx, "Failed to clear relay transport mappings: %v", err)
return gerror.Wrap(err, "Failed to clear relay transport mappings")

Copilot uses AI. Check for mistakes.
}
g.Log().Info(ctx, "No active relay to domain mappings, relay functionality will be disabled")
}

Expand Down Expand Up @@ -800,6 +805,21 @@ sender_dependent_default_transport_maps = pgsql:/etc/postfix/sql/pgsql_sender_tr
}
modified = true
}
} else {
// Remove the configuration block when relay is disabled
if hasConfigBlock {
blockEnd := endIndex + len(endMarker)
// Include trailing newline if present
if blockEnd < len(content) && (content[blockEnd] == '\n' || content[blockEnd] == '\r') {
blockEnd++
if blockEnd < len(content) && content[blockEnd] == '\n' {
Copy link

Copilot AI Dec 20, 2025

Choose a reason for hiding this comment

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

Newline handling could be more robust: The logic handles common cases like \n, \n\n, and \r\n, but doesn't consistently handle all newline character combinations. After checking for the first newline character (which could be \n or \r), the second check only looks for \n. Consider checking for any newline character in the second check as well to handle edge cases more consistently.

Suggested change
if blockEnd < len(content) && content[blockEnd] == '\n' {
if blockEnd < len(content) && (content[blockEnd] == '\n' || content[blockEnd] == '\r') {

Copilot uses AI. Check for mistakes.
blockEnd++
}
}
content = content[:beginIndex] + content[blockEnd:]
modified = true
g.Log().Info(nil, "Removed relay configuration block from main.cf")
Copy link

Copilot AI Dec 20, 2025

Choose a reason for hiding this comment

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

Context parameter inconsistency: The logging call uses nil for the context parameter, but the pattern used elsewhere in this file when context is unavailable is context.Background() (see lines 705, 747, 756, 758). Using context.Background() instead of nil is more idiomatic and consistent with the rest of the codebase.

Suggested change
g.Log().Info(nil, "Removed relay configuration block from main.cf")
g.Log().Info(context.Background(), "Removed relay configuration block from main.cf")

Copilot uses AI. Check for mistakes.
}
}
// If there are modifications, write to the file
if modified {
Expand Down