1
0
Fork 0
mirror of https://code.forgejo.org/forgejo/runner.git synced 2025-09-15 18:57:01 +00:00

fix: reduce the time during which the database stays open

* During get/upload, close the database while reading/writing so
  it does not stay open for longer than necessary. This may be helpful
  when uploads run in parallel.
* Be more informative when returning error 500
* Make useCache handle errors
* Return 500 immediately when writing the cache fails instead of falling
  through to 200

Refs: https://code.forgejo.org/forgejo/runner/issues/509
This commit is contained in:
Earl Warren 2025-03-23 22:31:16 +01:00
parent 776ccb8b21
commit 187e1df52c

View file

@ -263,7 +263,7 @@ func (h *Handler) upload(w http.ResponseWriter, r *http.Request, params httprout
cache := &Cache{}
db, err := h.openDB()
if err != nil {
h.responseJSON(w, r, 500, err)
h.responseJSON(w, r, 500, fmt.Errorf("cache openDB: %w", err))
return
}
defer db.Close()
@ -272,13 +272,13 @@ func (h *Handler) upload(w http.ResponseWriter, r *http.Request, params httprout
h.responseJSON(w, r, 400, fmt.Errorf("cache %d: not reserved", id))
return
}
h.responseJSON(w, r, 500, err)
h.responseJSON(w, r, 500, fmt.Errorf("cache Get: %w", err))
return
}
// Should not happen
if cache.Repo != repo {
h.responseJSON(w, r, 500, ErrValidation)
h.responseJSON(w, r, 500, fmt.Errorf("cache repo is not valid"))
return
}
@ -286,16 +286,20 @@ func (h *Handler) upload(w http.ResponseWriter, r *http.Request, params httprout
h.responseJSON(w, r, 400, fmt.Errorf("cache %v %q: already complete", cache.ID, cache.Key))
return
}
defer db.Close()
db.Close()
start, _, err := parseContentRange(r.Header.Get("Content-Range"))
if err != nil {
h.responseJSON(w, r, 400, err)
h.responseJSON(w, r, 400, fmt.Errorf("cache parseContentRange(%s): %w", r.Header.Get("Content-Range"), err))
return
}
if err := h.storage.Write(cache.ID, start, r.Body); err != nil {
h.responseJSON(w, r, 500, err)
h.responseJSON(w, r, 500, fmt.Errorf("cache storage.Write: %w", err))
return
}
if err := h.useCache(id); err != nil {
h.responseJSON(w, r, 500, fmt.Errorf("cache useCache: %w", err))
return
}
h.useCache(db, cache)
h.responseJSON(w, r, 200)
}
@ -332,7 +336,7 @@ func (h *Handler) commit(w http.ResponseWriter, r *http.Request, params httprout
// Should not happen
if cache.Repo != repo {
h.responseJSON(w, r, 500, ErrValidation)
h.responseJSON(w, r, 500, fmt.Errorf("cache repo is not valid"))
return
}
@ -385,26 +389,29 @@ func (h *Handler) get(w http.ResponseWriter, r *http.Request, params httprouter.
cache := &Cache{}
db, err := h.openDB()
if err != nil {
h.responseJSON(w, r, 500, err)
h.responseJSON(w, r, 500, fmt.Errorf("cache openDB: %w", err))
return
}
defer db.Close()
db.Close()
if err := db.Get(id, cache); err != nil {
if errors.Is(err, bolthold.ErrNotFound) {
h.responseJSON(w, r, 404, fmt.Errorf("cache %d: not reserved", id))
return
}
h.responseJSON(w, r, 500, err)
h.responseJSON(w, r, 500, fmt.Errorf("cache Get: %w", err))
return
}
// Should not happen
if cache.Repo != repo {
h.responseJSON(w, r, 500, ErrValidation)
h.responseJSON(w, r, 500, fmt.Errorf("cache repo is not valid"))
return
}
h.useCache(db, cache)
if err := h.useCache(id); err != nil {
h.responseJSON(w, r, 500, fmt.Errorf("cache useCache: %w", err))
return
}
h.storage.Serve(w, r, id)
}
@ -478,9 +485,18 @@ func insertCache(db *bolthold.Store, cache *Cache) error {
return nil
}
func (h *Handler) useCache(db *bolthold.Store, cache *Cache) {
func (h *Handler) useCache(id uint64) error {
db, err := h.openDB()
if err != nil {
return err
}
defer db.Close()
cache := &Cache{}
if err := db.Get(id, cache); err != nil {
return err
}
cache.UsedAt = time.Now().Unix()
_ = db.Update(cache.ID, cache)
return db.Update(cache.ID, cache)
}
const (