From 43a6d1be0f4a93bbbb53bdf883fa0d3baeae9d22 Mon Sep 17 00:00:00 2001 From: MaysWind Date: Thu, 15 Jan 2026 23:35:47 +0800 Subject: [PATCH] initialize the http transport only once --- pkg/api/map_image_proxies.go | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/pkg/api/map_image_proxies.go b/pkg/api/map_image_proxies.go index a7055f5d..3a7f9d44 100644 --- a/pkg/api/map_image_proxies.go +++ b/pkg/api/map_image_proxies.go @@ -5,6 +5,7 @@ import ( "net/http/httputil" "net/url" "strings" + "sync" "github.com/mayswind/ezbookkeeping/pkg/core" "github.com/mayswind/ezbookkeeping/pkg/errs" @@ -25,6 +26,8 @@ const tianDiTuMapAnnotationUrlFormat = "https://t0.tianditu.gov.cn/cva_w/wmts?SE // MapImageProxy represents map image proxy type MapImageProxy struct { ApiUsingConfig + mutex sync.Mutex + transport *http.Transport } // Initialize a map image proxy singleton instance @@ -36,6 +39,18 @@ var ( } ) +func (p *MapImageProxy) initializeHttpTransport() { + p.mutex.Lock() + defer p.mutex.Unlock() + + if p.transport != nil { + return + } + + p.transport = http.DefaultTransport.(*http.Transport).Clone() + httpclient.SetProxyUrl(p.transport, p.CurrentConfig().MapProxy) +} + // MapTileImageProxyHandler returns map tile image func (p *MapImageProxy) MapTileImageProxyHandler(c *core.WebContext) (*httputil.ReverseProxy, *errs.Error) { return p.mapImageProxyHandler(c, func(c *core.WebContext, mapProvider string) (string, *errs.Error) { @@ -109,8 +124,9 @@ func (p *MapImageProxy) mapImageProxyHandler(c *core.WebContext, fn func(c *core return nil, err } - transport := http.DefaultTransport.(*http.Transport).Clone() - httpclient.SetProxyUrl(transport, p.CurrentConfig().MapProxy) + if p.transport == nil { + p.initializeHttpTransport() + } director := func(req *http.Request) { imageRawUrl := targetUrl @@ -126,7 +142,7 @@ func (p *MapImageProxy) mapImageProxyHandler(c *core.WebContext, fn func(c *core } return &httputil.ReverseProxy{ - Transport: transport, + Transport: p.transport, Director: director, }, nil }