mirror of https://github.com/goproxyio/goproxy
feat: support to set cache expiration
parent
236ffb4996
commit
e03dc0f0bf
8
main.go
8
main.go
|
@ -40,19 +40,18 @@ import (
|
|||
)
|
||||
|
||||
var downloadRoot string
|
||||
|
||||
const listExpire = proxy.ListExpire
|
||||
|
||||
var listen, promListen string
|
||||
var cacheDir string
|
||||
var proxyHost string
|
||||
var excludeHost string
|
||||
var cacheExpire time.Duration
|
||||
|
||||
func init() {
|
||||
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(&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.DurationVar(&cacheExpire, "cacheExpire", 5*time.Minute, "Go Modules cache expiration (min), default is 5 min")
|
||||
flag.Parse()
|
||||
|
||||
if os.Getenv("GIT_TERMINAL_PROMPT") == "" {
|
||||
|
@ -89,6 +88,7 @@ func main() {
|
|||
Pattern: excludeHost,
|
||||
Proxy: proxyHost,
|
||||
DownloadRoot: downloadRoot,
|
||||
CacheExpire: cacheExpire,
|
||||
})}
|
||||
} else {
|
||||
handle = &logger{proxy.NewServer(new(ops))}
|
||||
|
@ -195,7 +195,7 @@ func (*ops) List(ctx context.Context, mpath string) (proxy.File, error) {
|
|||
return nil, err
|
||||
}
|
||||
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)
|
||||
}
|
||||
var list struct {
|
||||
|
|
|
@ -30,6 +30,7 @@ type RouterOptions struct {
|
|||
Pattern string
|
||||
Proxy string
|
||||
DownloadRoot string
|
||||
CacheExpire time.Duration
|
||||
}
|
||||
|
||||
// A Router is the proxy HTTP server,
|
||||
|
@ -41,6 +42,7 @@ type Router struct {
|
|||
proxy *httputil.ReverseProxy
|
||||
pattern string
|
||||
downloadRoot string
|
||||
cacheExpire time.Duration
|
||||
}
|
||||
|
||||
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.pattern = opts.Pattern
|
||||
rt.downloadRoot = opts.DownloadRoot
|
||||
rt.cacheExpire = opts.CacheExpire
|
||||
}
|
||||
return rt
|
||||
}
|
||||
|
@ -215,7 +218,7 @@ func (rt *Router) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
what := r.URL.Path[i+len("/@v/"):]
|
||||
if what == "list" {
|
||||
if time.Since(info.ModTime()) >= ListExpire {
|
||||
if time.Since(info.ModTime()) >= rt.cacheExpire {
|
||||
log.Printf("------ --- %s [proxy]\n", r.URL)
|
||||
rt.proxy.ServeHTTP(mw, r)
|
||||
totalRequest.With(prometheus.Labels{"mode": "proxy", "status": mw.status()}).Inc()
|
||||
|
|
Loading…
Reference in New Issue