feat: support to set cache expiration

pull/193/head
Y4ssss 2021-10-28 09:54:47 +08:00 committed by Curith
parent 236ffb4996
commit e03dc0f0bf
2 changed files with 8 additions and 5 deletions

View File

@ -40,19 +40,18 @@ import (
) )
var downloadRoot string var downloadRoot string
const listExpire = proxy.ListExpire
var listen, promListen string var listen, promListen string
var cacheDir string var cacheDir string
var proxyHost string var proxyHost string
var excludeHost string var excludeHost string
var cacheExpire time.Duration
func init() { func init() {
flag.StringVar(&excludeHost, "exclude", "", "exclude host pattern, you can exclude internal Git services") flag.StringVar(&excludeHost, "exclude", "", "exclude host pattern, you can exclude internal Git services")
flag.StringVar(&proxyHost, "proxy", "", "next hop proxy for Go Modules, recommend use https://gopropxy.io") flag.StringVar(&proxyHost, "proxy", "", "next hop proxy for Go Modules, recommend use https://gopropxy.io")
flag.StringVar(&cacheDir, "cacheDir", "", "Go Modules cache dir, default is $GOPATH/pkg/mod/cache/download") flag.StringVar(&cacheDir, "cacheDir", "", "Go Modules cache dir, default is $GOPATH/pkg/mod/cache/download")
flag.StringVar(&listen, "listen", "0.0.0.0:8081", "service listen address") flag.StringVar(&listen, "listen", "0.0.0.0:8081", "service listen address")
flag.DurationVar(&cacheExpire, "cacheExpire", 5*time.Minute, "Go Modules cache expiration (min), default is 5 min")
flag.Parse() flag.Parse()
if os.Getenv("GIT_TERMINAL_PROMPT") == "" { if os.Getenv("GIT_TERMINAL_PROMPT") == "" {
@ -89,6 +88,7 @@ func main() {
Pattern: excludeHost, Pattern: excludeHost,
Proxy: proxyHost, Proxy: proxyHost,
DownloadRoot: downloadRoot, DownloadRoot: downloadRoot,
CacheExpire: cacheExpire,
})} })}
} else { } else {
handle = &logger{proxy.NewServer(new(ops))} handle = &logger{proxy.NewServer(new(ops))}
@ -195,7 +195,7 @@ func (*ops) List(ctx context.Context, mpath string) (proxy.File, error) {
return nil, err return nil, err
} }
file := filepath.Join(downloadRoot, escMod, "@v", "list") file := filepath.Join(downloadRoot, escMod, "@v", "list")
if info, err := os.Stat(file); err == nil && time.Since(info.ModTime()) < listExpire { if info, err := os.Stat(file); err == nil && time.Since(info.ModTime()) < cacheExpire {
return os.Open(file) return os.Open(file)
} }
var list struct { var list struct {

View File

@ -30,6 +30,7 @@ type RouterOptions struct {
Pattern string Pattern string
Proxy string Proxy string
DownloadRoot string DownloadRoot string
CacheExpire time.Duration
} }
// A Router is the proxy HTTP server, // A Router is the proxy HTTP server,
@ -41,6 +42,7 @@ type Router struct {
proxy *httputil.ReverseProxy proxy *httputil.ReverseProxy
pattern string pattern string
downloadRoot string downloadRoot string
cacheExpire time.Duration
} }
func (router *Router) customModResponse(r *http.Response) error { func (router *Router) customModResponse(r *http.Response) error {
@ -157,6 +159,7 @@ func NewRouter(srv *Server, opts *RouterOptions) *Router {
rt.proxy.ModifyResponse = rt.customModResponse rt.proxy.ModifyResponse = rt.customModResponse
rt.pattern = opts.Pattern rt.pattern = opts.Pattern
rt.downloadRoot = opts.DownloadRoot rt.downloadRoot = opts.DownloadRoot
rt.cacheExpire = opts.CacheExpire
} }
return rt return rt
} }
@ -215,7 +218,7 @@ func (rt *Router) ServeHTTP(w http.ResponseWriter, r *http.Request) {
what := r.URL.Path[i+len("/@v/"):] what := r.URL.Path[i+len("/@v/"):]
if what == "list" { if what == "list" {
if time.Since(info.ModTime()) >= ListExpire { if time.Since(info.ModTime()) >= rt.cacheExpire {
log.Printf("------ --- %s [proxy]\n", r.URL) log.Printf("------ --- %s [proxy]\n", r.URL)
rt.proxy.ServeHTTP(mw, r) rt.proxy.ServeHTTP(mw, r)
totalRequest.With(prometheus.Labels{"mode": "proxy", "status": mw.status()}).Inc() totalRequest.With(prometheus.Labels{"mode": "proxy", "status": mw.status()}).Inc()