mirror of
https://code.forgejo.org/forgejo/runner.git
synced 2025-08-11 17:50:58 +00:00
use safe sync.Map
This commit is contained in:
parent
41032137ea
commit
a6a1df7556
1 changed files with 18 additions and 11 deletions
|
@ -16,6 +16,7 @@ import (
|
||||||
"net/http/httputil"
|
"net/http/httputil"
|
||||||
"net/url"
|
"net/url"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/julienschmidt/httprouter"
|
"github.com/julienschmidt/httprouter"
|
||||||
|
@ -40,7 +41,7 @@ type Handler struct {
|
||||||
|
|
||||||
cacheSecret string
|
cacheSecret string
|
||||||
|
|
||||||
runs map[string]RunData
|
runs sync.Map
|
||||||
}
|
}
|
||||||
|
|
||||||
type RunData struct {
|
type RunData struct {
|
||||||
|
@ -72,7 +73,7 @@ func StartHandler(targetHost string, outboundIP string, port uint16, cacheSecret
|
||||||
h.logger = logger
|
h.logger = logger
|
||||||
|
|
||||||
h.cacheSecret = cacheSecret
|
h.cacheSecret = cacheSecret
|
||||||
h.runs = make(map[string]RunData)
|
// h.runs = make(map[string]RunData)
|
||||||
|
|
||||||
if outboundIP != "" {
|
if outboundIP != "" {
|
||||||
h.outboundIP = outboundIP
|
h.outboundIP = outboundIP
|
||||||
|
@ -137,12 +138,19 @@ func (h *Handler) newReverseProxy(targetHost string) (*httputil.ReverseProxy, er
|
||||||
re := regexp.MustCompile(`/(\w+)/_apis/artifactcache`)
|
re := regexp.MustCompile(`/(\w+)/_apis/artifactcache`)
|
||||||
matches := re.FindStringSubmatch(r.In.URL.Path)
|
matches := re.FindStringSubmatch(r.In.URL.Path)
|
||||||
id := matches[1]
|
id := matches[1]
|
||||||
data := h.runs[id]
|
data, ok := h.runs.Load(id)
|
||||||
|
var runData = data.(RunData)
|
||||||
|
if !ok {
|
||||||
|
// The ID doesn't exist.
|
||||||
|
// ! this should probably be handled more gracefully but i can't figure out how
|
||||||
|
// ! it really shouldn't happen anyway so it's fine for now
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
r.Out.Header.Add("Forgejo-Cache-Repo", data.repositoryFullName)
|
r.Out.Header.Add("Forgejo-Cache-Repo", runData.repositoryFullName)
|
||||||
r.Out.Header.Add("Forgejo-Cache-RunNumber", data.runNumber)
|
r.Out.Header.Add("Forgejo-Cache-RunNumber", runData.runNumber)
|
||||||
r.Out.Header.Add("Forgejo-Cache-Timestamp", data.timestamp)
|
r.Out.Header.Add("Forgejo-Cache-Timestamp", runData.timestamp)
|
||||||
r.Out.Header.Add("Forgejo-Cache-MAC", data.repositoryMAC)
|
r.Out.Header.Add("Forgejo-Cache-MAC", runData.repositoryMAC)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
return proxy, nil
|
return proxy, nil
|
||||||
|
@ -166,17 +174,16 @@ func (h *Handler) AddRun(data RunData) (string, error) {
|
||||||
}
|
}
|
||||||
key := hex.EncodeToString(keyBytes)
|
key := hex.EncodeToString(keyBytes)
|
||||||
|
|
||||||
h.runs[key] = data
|
h.runs.Store(key, data)
|
||||||
|
|
||||||
return key, nil
|
return key, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handler) RemoveRun(runID string) error {
|
func (h *Handler) RemoveRun(runID string) error {
|
||||||
_, exists := h.runs[runID]
|
_, existed := h.runs.LoadAndDelete(runID)
|
||||||
if !exists {
|
if !existed {
|
||||||
return errors.New("The run id was not known to the proxy")
|
return errors.New("The run id was not known to the proxy")
|
||||||
}
|
}
|
||||||
delete(h.runs, runID)
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue