Skip to content

Fix port allocation scoped per-site for transport services#296

Draft
Copilot wants to merge 3 commits intomainfrom
copilot/fix-port-allocation-issue
Draft

Fix port allocation scoped per-site for transport services#296
Copilot wants to merge 3 commits intomainfrom
copilot/fix-port-allocation-issue

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented May 6, 2026

Port allocation for TCP/UDP transport services was global — port numbers from site 1 were treated as unavailable when allocating for site 2, causing unexpectedly high ports in new sites.

Changes

migrations/20260506000000-transport-service-site-scoped-port-constraint.js (new)

  • Adds siteId column to TransportServices, backfilled via Service → Container
  • Drops the global (protocol, externalPort) unique index
  • Adds a site-scoped (siteId, protocol, externalPort) unique index

models/transport-service.js

  • Denormalized siteId field added to the model with a belongsTo Site association
  • nextAvailablePortInRange now requires siteId and filters directly on the TransportServices table — no joins needed
// Before: scans all sites
static async nextAvailablePortInRange(protocol, minPort, maxPort, transaction = null)

// After: scoped to site via denormalized siteId column
static async nextAvailablePortInRange(protocol, minPort, maxPort, siteId, transaction = null)

routers/containers.js

  • Both POST and PUT container handlers pass siteId to nextAvailablePortInRange and include it in TransportService.create()
  • Corrected pre-existing typo: max port 6556565535 (valid TCP/UDP ceiling)

Copilot AI linked an issue May 6, 2026 that may be closed by this pull request
Copilot AI changed the title [WIP] Fix port allocation shared across sites Fix port allocation scoped per-site for transport services May 6, 2026
Copilot AI requested a review from runleveldev May 6, 2026 14:18
Copy link
Copy Markdown
Collaborator

@runleveldev runleveldev left a comment

Choose a reason for hiding this comment

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

@copilot there is a unique constraint on (protocol, externalPort) in the TransportServices model. To support this change, the unique constraint should instead be on (siteId, protocol, externalPort). There is a one-to-one relationship from TransportService to Sites via TransportService(serviceId)->Service(containerId)->Container(nodeId)->Node(siteId). Denormalization of the TransportService model may be nessecary to apply this change.

… port constraint

Agent-Logs-Url: https://github.com/mieweb/opensource-server/sessions/755f5326-8e1f-43d8-8b0b-8426d4f6f98c

Co-authored-by: runleveldev <44057501+runleveldev@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented May 6, 2026

@copilot there is a unique constraint on (protocol, externalPort) in the TransportServices model. To support this change, the unique constraint should instead be on (siteId, protocol, externalPort). T...

Done in 5cb4f1c. siteId is now denormalized onto TransportService:

  • Migration (20260506000000): adds siteId column, backfills it via Service → Container, makes it NOT NULL, drops the global (protocol, externalPort) unique index, and adds a site-scoped (siteId, protocol, externalPort) unique index
  • Model: siteId field added with belongsTo Site association; nextAvailablePortInRange now filters directly by siteId on TransportServices (no joins)
  • containers.js: both TransportService.create() calls now include siteId

Copilot AI requested a review from runleveldev May 6, 2026 14:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Port allocation shared across sites

2 participants