grim/hgkeeper

Remove the caching from hgweb.

2022-03-07, Gary Kramlich
f1c78b2d25b9
Parents 4bd445aa0546
Children d30d528a29fa
Remove the caching from hgweb.

It's been disabled for awhile and it never worked right anyways
--- a/hgweb/cache.go Mon Mar 07 01:22:42 2022 -0600
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,96 +0,0 @@
-package hgweb
-
-import (
- "net/http"
-
- log "github.com/sirupsen/logrus"
-
- "github.com/hashicorp/golang-lru"
-)
-
-type cache struct {
- cache *lru.ARCCache
-}
-
-type cacheEntry struct {
- headers http.Header
- body []byte
-}
-
-func newCache(size int) (*cache, error) {
- c, err := lru.NewARC(size)
- if err != nil {
- return nil, err
- }
-
- log.Debugf("created HTTP cache with size of %d", size)
-
- return &cache{
- cache: c,
- }, nil
-}
-
-func (c *cache) middleware(next http.Handler) http.Handler {
- return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- // If this is not a GET request, we pass it straight through to
- // mercurial.
- if r.Method != http.MethodGet {
- next.ServeHTTP(w, r)
-
- return
- }
-
- // if the user didn't have a copy of this page, we check for a cached
- // copy and if we have it, we return it back to them.
- if r.Header.Get("If-None-Match") == "" {
- if raw, ok := c.cache.Get(r.URL.String()); ok {
- entry := raw.(cacheEntry)
-
- newHeaders := w.Header()
- for k, v := range entry.headers {
- newHeaders[k] = v
- }
-
- w.Write(entry.body)
-
- return
- }
- }
-
- // At this point, the user did provide an etag or we don't have a
- // cached copy of the page. So we pass the request on to mercurial,
- // who will validate the etag if specified, or just do the work.
-
- // create our response writer which caches the data
- rw := newCachingResponseWriter(w)
-
- // send the request to mercurial
- next.ServeHTTP(rw, r)
-
- // if the status code is http.StatusOK, then either the users etag was
- // expired or we didn't have this page cached. Regardless we then
- // cache the page.
- if rw.StatusCode() == http.StatusOK {
- // create a http.Header skipping known bad headers
- headers := http.Header{}
- for k, v := range rw.Header() {
- switch k {
- case "Set-Cookie":
- case "Authorization":
- case "WWW-Authenticate":
- case "Proxy-Authorization":
- case "Proxy-Authenticate":
- // these cases are all ignored.
- default:
- headers[k] = v
- }
- }
-
- // add our entry to the cache
- c.cache.Add(r.URL.String(), cacheEntry{
- headers: headers,
- body: rw.Body(),
- })
- }
- })
-}
--- a/hgweb/cachingresponsewriter.go Mon Mar 07 01:22:42 2022 -0600
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,68 +0,0 @@
-package hgweb
-
-import (
- "bytes"
- "io"
- "net/http"
-)
-
-type cachingResponseWriter struct {
- w http.ResponseWriter
- wroteHeader bool
- statusCode int
- written int64
-
- multiWriter io.Writer
- cachedBody *bytes.Buffer
-}
-
-var _ http.ResponseWriter = (*cachingResponseWriter)(nil)
-
-func newCachingResponseWriter(w http.ResponseWriter) *cachingResponseWriter {
- rw := &cachingResponseWriter{
- w: w,
- cachedBody: &bytes.Buffer{},
- }
-
- rw.multiWriter = io.MultiWriter(w, rw.cachedBody)
-
- return rw
-}
-
-func (w *cachingResponseWriter) Header() http.Header {
- return w.w.Header()
-}
-
-func (w *cachingResponseWriter) Write(data []byte) (int, error) {
- if !w.wroteHeader {
- w.wroteHeader = true
- w.statusCode = http.StatusOK
- }
-
- l, err := w.multiWriter.Write(data)
- w.written += int64(l)
-
- return l, err
-}
-
-func (w *cachingResponseWriter) WriteHeader(statusCode int) {
- if !w.wroteHeader {
- w.wroteHeader = true
-
- w.statusCode = statusCode
- }
-
- w.w.WriteHeader(w.statusCode)
-}
-
-func (w *cachingResponseWriter) StatusCode() int {
- return w.statusCode
-}
-
-func (w *cachingResponseWriter) Written() int64 {
- return w.written
-}
-
-func (w *cachingResponseWriter) Body() []byte {
- return w.cachedBody.Bytes()
-}
--- a/hgweb/hgweb.go Mon Mar 07 01:22:42 2022 -0600
+++ b/hgweb/hgweb.go Mon Mar 07 01:26:15 2022 -0600
@@ -27,16 +27,13 @@
configPath string
cgiPath string
- cacheSize int
-
externalHostname string
externalPort string
}
-func NewServer(listenAddr string, cacheSize int, externalHostname, externalPort string) (*Server, error) {
+func NewServer(listenAddr string, externalHostname, externalPort string) (*Server, error) {
return &Server{
listenAddr: listenAddr,
- cacheSize: cacheSize,
server: &http.Server{
Addr: listenAddr,
},
@@ -116,11 +113,6 @@
return err
}
- cache, err := newCache(s.cacheSize)
- if err != nil {
- return err
- }
-
templatesDir, err := hg.TemplatesDir()
if err != nil {
return err
@@ -141,12 +133,7 @@
mux.Handle("/static/", http.StripPrefix("/static", fileServer))
mux.Handle("/", &cgi.Handler{Path: s.cgiPath})
- enableCache := false
- if enableCache {
- s.server.Handler = Logger(cache.middleware(mux))
- } else {
- s.server.Handler = Logger(mux)
- }
+ s.server.Handler = Logger(mux)
log.Infof("http listening on %s", s.listenAddr)
--- a/serve/command.go Mon Mar 07 01:22:42 2022 -0600
+++ b/serve/command.go Mon Mar 07 01:26:15 2022 -0600
@@ -18,7 +18,6 @@
SSHAddr string `kong:"flag,name='ssh-listen-addr',env='HGK_SSH_LISTEN_ADDR',short='l',help='what address to listen on',default=':22222'"`
SSHHostKeysPath string `kong:"flag,name='ssh-host-keys-path',env='HGK_SSH_HOST_KEYS_PATH',short='H',help='the path where host keys are kept',default='host-keys'"`
HTTPAddr string `kong:"flag,name='http-listen-addr',env='HGK_HTTP_LISTEN_ADDR',help='what address the http server listens on',default=':8080'"`
- CacheSize int `kong:"flag,name='cache-size',env='HGK_HTTP_CACHE_SIZE',help='number of pages to cache',default='1000'"`
DisableSSH bool `kong:"flag,name='disable-ssh',env='HGK_DISABLE_SSH',help='disable the SSH server',default='false'"`
DisableHTTP bool `kong:"flag,name='disable-http',env='HGK_DISABLE_HTTP',help='disable the HTTP server',default='false'"`
ExternalHostname string `kong:"flag,name='external-hostname',env='HGK_EXTERNAL_HOSTNAME',help='The external hostname of the hgkeeper instance. This is used to integrate with other ssh servers.'"`
@@ -58,7 +57,7 @@
log.Info("HTTP server has been disabled")
} else {
var err error
- hgwebServer, err = hgweb.NewServer(c.HTTPAddr, c.CacheSize, c.ExternalHostname, c.ExternalPort)
+ hgwebServer, err = hgweb.NewServer(c.HTTPAddr, c.ExternalHostname, c.ExternalPort)
if err != nil {
return err
}