fixes #2689 Make the external url of cache server configurable if necessary (#2690)

* Make the external url of cache server configurable if necessary

Signed-off-by: Zoupers <qy@zouper.cn>

* optimize code and doc and fix test and add test

Signed-off-by: Zoupers <qy@zouper.cn>

* optimize code and adjust description

Signed-off-by: Zoupers <qy@zouper.cn>

---------

Signed-off-by: Zoupers <qy@zouper.cn>
Co-authored-by: 林玮 (Jade Lin) <linw1995@icloud.com>
This commit is contained in:
Zoupers Zou
2025-03-05 08:04:23 +08:00
committed by GitHub
parent bd97dc8d94
commit 49710c8504
4 changed files with 55 additions and 9 deletions

View File

@@ -38,10 +38,11 @@ type Handler struct {
gcing atomic.Bool
gcAt time.Time
outboundIP string
outboundIP string
customExternalURL string
}
func StartHandler(dir, outboundIP string, port uint16, logger logrus.FieldLogger) (*Handler, error) {
func StartHandler(dir, customExternalURL string, outboundIP string, port uint16, logger logrus.FieldLogger) (*Handler, error) {
h := &Handler{}
if logger == nil {
@@ -71,6 +72,10 @@ func StartHandler(dir, outboundIP string, port uint16, logger logrus.FieldLogger
}
h.storage = storage
if customExternalURL != "" {
h.customExternalURL = customExternalURL
}
if outboundIP != "" {
h.outboundIP = outboundIP
} else if ip := common.GetOutboundIP(); ip == nil {
@@ -95,6 +100,7 @@ func StartHandler(dir, outboundIP string, port uint16, logger logrus.FieldLogger
if err != nil {
return nil, err
}
server := &http.Server{
ReadHeaderTimeout: 2 * time.Second,
Handler: router,
@@ -110,11 +116,15 @@ func StartHandler(dir, outboundIP string, port uint16, logger logrus.FieldLogger
return h, nil
}
func (h *Handler) GetActualPort() int {
return h.listener.Addr().(*net.TCPAddr).Port
}
func (h *Handler) ExternalURL() string {
// TODO: make the external url configurable if necessary
return fmt.Sprintf("http://%s:%d",
h.outboundIP,
h.listener.Addr().(*net.TCPAddr).Port)
if h.customExternalURL != "" {
return h.customExternalURL
}
return fmt.Sprintf("http://%s:%d", h.outboundIP, h.GetActualPort())
}
func (h *Handler) Close() error {

View File

@@ -20,7 +20,7 @@ import (
func TestHandler(t *testing.T) {
dir := filepath.Join(t.TempDir(), "artifactcache")
handler, err := StartHandler(dir, "", 0, nil)
handler, err := StartHandler(dir, "", "", 0, nil)
require.NoError(t, err)
base := fmt.Sprintf("%s%s", handler.ExternalURL(), urlBase)
@@ -587,9 +587,43 @@ func uploadCacheNormally(t *testing.T, base, key, version string, content []byte
}
}
func TestHandler_CustomExternalURL(t *testing.T) {
dir := filepath.Join(t.TempDir(), "artifactcache")
handler, err := StartHandler(dir, "", "", 0, nil)
require.NoError(t, err)
defer func() {
require.NoError(t, handler.Close())
}()
handler.customExternalURL = fmt.Sprintf("http://%s:%d", "127.0.0.1", handler.GetActualPort())
assert.Equal(t, fmt.Sprintf("http://%s:%d", "127.0.0.1", handler.GetActualPort()), handler.ExternalURL())
base := fmt.Sprintf("%s%s", handler.ExternalURL(), urlBase)
t.Run("advertise url set wrong", func(t *testing.T) {
original := handler.customExternalURL
defer func() {
handler.customExternalURL = original
}()
handler.customExternalURL = "http://127.0.0.999:1234"
assert.Equal(t, "http://127.0.0.999:1234", handler.ExternalURL())
})
t.Run("reserve and upload", func(t *testing.T) {
key := strings.ToLower(t.Name())
version := "c19da02a2bd7e77277f1ac29ab45c09b7d46a4ee758284e26bb3045ad11d9d20"
content := make([]byte, 100)
_, err := rand.Read(content)
require.NoError(t, err)
uploadCacheNormally(t, base, key, version, content)
})
}
func TestHandler_gcCache(t *testing.T) {
dir := filepath.Join(t.TempDir(), "artifactcache")
handler, err := StartHandler(dir, "", 0, nil)
handler, err := StartHandler(dir, "", "", 0, nil)
require.NoError(t, err)
defer func() {