carlos / platform Public

console: keep the ship handler and server.go under their limits

Move the #1389 expect_artifacts checks and the kind preflight out of
handleAPIShip into helpers in apiship_expect.go, and fold the
releaseReads field and init in server.go. Check order is unchanged.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev bd91a38f6645adb8c72af2010806bb6ac55ea7f1 parent 76ed744
3 files changed, +54 −33
  • internal/console/apicli.go +2 −26
  • internal/console/apiship_expect.go +47 −0
  • internal/console/server.go +5 −7
diff --git a/internal/console/apicli.go b/internal/console/apicli.go
index 809e48798..f19c892c9 100644
--- a/internal/console/apicli.go
+++ b/internal/console/apicli.go
@@ -2439,26 +2439,7 @@ func (s *Server) handleAPIShip(w http.ResponseWriter, r *http.Request) {
}
upload.lower(apiShipMaxBytes)
}
- if err := checkShipExpectShape(meta.Kind, meta.ExpectArtifacts, meta.RootfsSize); err != nil {
- refuseUnread(w, http.StatusBadRequest, err.Error())
- return
- }
- // An app's kind and hibernation are fixed by its first ship, so a
- // release of another kind, or a rootfs release with the other
- // hibernation, is refused here, before any part is read. The check is
- // advisory: release.Ship and release.ShipSpooled repeat it at
- // publication, which is what decides a race between two first ships.
- kctx, kcancel := context.WithTimeout(r.Context(), 20*time.Second)
- stub := release.Manifest{App: app, Version: ver, Kind: meta.Kind, Hibernate: meta.Hibernate}
- kerr := release.CheckShipKind(kctx, s.cfg.Releases, acct.Sqid, &stub)
- kcancel()
- if kerr != nil {
- if msg, ok := shipKindConflict(kerr); ok {
- refuseUnread(w, http.StatusConflict, msg)
- return
- }
- log.Printf("console: api ship %s/%s %s: kind check: %v", acct.Sqid, app, ver, kerr)
- refuseUnread(w, http.StatusServiceUnavailable, shipKindUnchecked)
+ if s.refuseShipPreflight(w, r, acct, app, ver, meta) {
return
}
if rootfs {
@@ -2531,12 +2512,7 @@ func (s *Server) handleAPIShip(w http.ResponseWriter, r *http.Request) {
Size: int64(len(b)),
}
}
- if len(files) == 0 {
- jsonError(w, http.StatusBadRequest, "no artifact parts in the upload")
- return
- }
- if err := checkShipExpected(meta.ExpectArtifacts, artifacts); err != nil {
- jsonError(w, http.StatusBadRequest, err.Error())
+ if refuseShipUpload(w, files, meta.ExpectArtifacts, artifacts) {
return
}
diff --git a/internal/console/apiship_expect.go b/internal/console/apiship_expect.go
index 1073bbabc..bbfd8b385 100644
--- a/internal/console/apiship_expect.go
+++ b/internal/console/apiship_expect.go
@@ -1,9 +1,13 @@
package console
import (
+ "context"
"fmt"
+ "log"
+ "net/http"
"regexp"
"sort"
+ "time"
"github.com/carlosframework/platform/internal/release"
)
@@ -87,3 +91,46 @@ func sortedExpectKeys(expect map[string]shipExpect) []string {
sort.Strings(keys)
return keys
}
+
+// refuseShipPreflight runs the checks a ship makes before any part is read:
+// the expect_artifacts shape, then the app's kind and hibernation, which a
+// first ship fixes. A release of another kind, or a rootfs release with the
+// other hibernation, is refused here. The kind check is advisory:
+// release.Ship and release.ShipSpooled repeat it at publication, which is
+// what decides a race between two first ships. It writes the refusal and
+// returns true when the ship must stop.
+func (s *Server) refuseShipPreflight(w http.ResponseWriter, r *http.Request, acct *Account, app, ver string, meta shipMeta) bool {
+ if err := checkShipExpectShape(meta.Kind, meta.ExpectArtifacts, meta.RootfsSize); err != nil {
+ refuseUnread(w, http.StatusBadRequest, err.Error())
+ return true
+ }
+ kctx, kcancel := context.WithTimeout(r.Context(), 20*time.Second)
+ stub := release.Manifest{App: app, Version: ver, Kind: meta.Kind, Hibernate: meta.Hibernate}
+ kerr := release.CheckShipKind(kctx, s.cfg.Releases, acct.Sqid, &stub)
+ kcancel()
+ if kerr == nil {
+ return false
+ }
+ if msg, ok := shipKindConflict(kerr); ok {
+ refuseUnread(w, http.StatusConflict, msg)
+ return true
+ }
+ log.Printf("console: api ship %s/%s %s: kind check: %v", acct.Sqid, app, ver, kerr)
+ refuseUnread(w, http.StatusServiceUnavailable, shipKindUnchecked)
+ return true
+}
+
+// refuseShipUpload refuses a ship with no artifact parts, or whose artifacts
+// differ from expect, before anything is stored. It writes the refusal and
+// returns true when the ship must stop.
+func refuseShipUpload(w http.ResponseWriter, files map[string][]byte, expect map[string]shipExpect, got map[string]release.Artifact) bool {
+ if len(files) == 0 {
+ jsonError(w, http.StatusBadRequest, "no artifact parts in the upload")
+ return true
+ }
+ if err := checkShipExpected(expect, got); err != nil {
+ jsonError(w, http.StatusBadRequest, err.Error())
+ return true
+ }
+ return false
+}
diff --git a/internal/console/server.go b/internal/console/server.go
index bea3934e1..b011e6e50 100644
--- a/internal/console/server.go
+++ b/internal/console/server.go
@@ -1463,12 +1463,11 @@ type Server struct {
// counter alone does nothing to close. Always initialized (never nil):
// unlike cliNotified below this is load-bearing in production, not a
// test hook.
- shipSem chan struct{}
+ //
// releaseReads bounds how many release artifact downloads
- // (apirelease_read.go, #1389) stream AT ONCE, for shipSem's reason: a
- // download holds a goroutine and a store read open for as long as the
- // client drains it. Always initialized, cap releaseReadsMax.
- releaseReads chan struct{}
+ // (apirelease_read.go, #1389) stream AT ONCE, for shipSem's reason.
+ // Always initialized, cap releaseReadsMax.
+ shipSem, releaseReads chan struct{}
// shipStarted, when non-nil, is signalled after a ship request
// acquires shipSem, before it does any (possibly slow, possibly
// blocking) reading of the request body. Tests only, same reasoning as
@@ -2046,8 +2045,7 @@ func NewServer(cfg Config, st *Store) (*Server, error) {
// human running `carlos ship` by hand, tight enough that a fleet of
// slow-loris uploads against a stolen or shared token can't pile up
// goroutines and buffers without bound.
- shipSem: make(chan struct{}, 2),
- releaseReads: make(chan struct{}, releaseReadsMax),
+ shipSem: make(chan struct{}, 2), releaseReads: make(chan struct{}, releaseReadsMax),
delegationZone: normalizeDelegationZone(cfg.DelegationZone),
steeringHost: normalizeSteeringHost(cfg.SteeringHost),
lookupCNAME: net.DefaultResolver.LookupCNAME,