Improve deployment hard limit enforcement to prevent concurrent race conditions
@dushaniw is already working on this.
Since Feb 1, 2026.
- Dominant language
- Go
- Stars
- 71
- Forks
- 111
- Avg merge
- 1d 14h
- Merged PRs (30d)
- 110
Description
Context
This issue tracks an improvement identified during code review of PR #853.
Related PR: https://github.com/wso2/api-platform/pull/853
Related Comment: https://github.com/wso2/api-platform/pull/853#discussion_r2751556691
Requested by: @dushaniw
Problem
In platform-api/src/internal/repository/api.go (CreateDeploymentWithLimitEnforcement method), the current hard limit enforcement can still allow the limit to be exceeded in the following scenarios:
- When there are insufficient archived deployments to delete (fewer than needed to make room)
- When concurrent inserts occur before the transaction completes
The current logic:
- Counts total deployments
- If at/over hard limit, deletes up to 5 oldest archived deployments
- Inserts the new deployment
This doesn't re-check the count after deletion or prevent concurrent inserts from bypassing the limit.
Suggested Improvement
- Acquire a lock for the API+gateway+org combination (e.g., SELECT ... FOR UPDATE or advisory lock)
- Calculate exact number of deletions needed:
neededDeletes = (count + 1) - hardLimit - Fetch and delete exactly that many archived deployments
- Re-check the count after deletion
- If there aren't enough archived rows to make room, return a clear limit-reached error instead of proceeding with the insert
Priority
Enhancement - to be addressed in a future PR
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.