Skip to content

Commit

Permalink
internal/middleware: move the timeout middleware to its own package
Browse files Browse the repository at this point in the history
This removes the direct dependency from cmd/pkgsite to the middleware
package. The middleware package has a lot of dependencies but most
middlewares are not used by cmd/pkgsite.

There are still some middlewares used by internal/frontend so the
indirect dependency still exists.

For #61399

Change-Id: I09eee46b2ff13e112d24df62073074eb5aa3e901
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/511315
Run-TryBot: Michael Matloob <matloob@golang.org>
Reviewed-by: Jamal Carvalho <jamal@golang.org>
kokoro-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
  • Loading branch information
matloob committed Jul 31, 2023
1 parent 0bc4b0e commit 0d6a958
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 7 deletions.
3 changes: 2 additions & 1 deletion cmd/frontend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"golang.org/x/pkgsite/internal/frontend"
"golang.org/x/pkgsite/internal/log"
"golang.org/x/pkgsite/internal/middleware"
"golang.org/x/pkgsite/internal/middleware/timeout"
"golang.org/x/pkgsite/internal/postgres"
"golang.org/x/pkgsite/internal/proxy"
"golang.org/x/pkgsite/internal/queue"
Expand Down Expand Up @@ -188,7 +189,7 @@ func main() {
middleware.Experiment(experimenter),
middleware.Panic(panicHandler),
ermw,
middleware.Timeout(54*time.Second),
timeout.Timeout(54*time.Second),
)
addr := cfg.HostAddr(*hostAddr)
log.Infof(ctx, "Listening on addr %s", addr)
Expand Down
4 changes: 2 additions & 2 deletions cmd/pkgsite/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ import (
"golang.org/x/pkgsite/internal/fetchdatasource"
"golang.org/x/pkgsite/internal/frontend"
"golang.org/x/pkgsite/internal/log"
"golang.org/x/pkgsite/internal/middleware"
"golang.org/x/pkgsite/internal/middleware/timeout"
"golang.org/x/pkgsite/internal/proxy"
"golang.org/x/pkgsite/internal/source"
"golang.org/x/pkgsite/internal/stdlib"
Expand Down Expand Up @@ -170,7 +170,7 @@ func main() {

router := http.NewServeMux()
server.Install(router.Handle, nil, nil)
mw := middleware.Timeout(54 * time.Second)
mw := timeout.Timeout(54 * time.Second)
srv := &http.Server{Addr: addr, Handler: mw(router)}
die("%v", srv.Serve(ln))
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/worker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"golang.org/x/pkgsite/internal/index"
"golang.org/x/pkgsite/internal/log"
"golang.org/x/pkgsite/internal/middleware"
mtimeout "golang.org/x/pkgsite/internal/middleware/timeout"
"golang.org/x/pkgsite/internal/proxy"
"golang.org/x/pkgsite/internal/queue/gcpqueue"
"golang.org/x/pkgsite/internal/source"
Expand Down Expand Up @@ -142,7 +143,7 @@ func main() {

mw := middleware.Chain(
middleware.RequestLog(cmdconfig.Logger(ctx, cfg, "worker-log")),
middleware.Timeout(time.Duration(timeout)*time.Minute),
mtimeout.Timeout(time.Duration(timeout)*time.Minute),
iap,
middleware.Experiment(experimenter),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package middleware
package timeout

import (
"context"
Expand All @@ -12,7 +12,7 @@ import (

// Timeout returns a new Middleware that times out each request after the given
// duration.
func Timeout(d time.Duration) Middleware {
func Timeout(d time.Duration) func(http.Handler) http.Handler {
return func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx, cancel := context.WithTimeout(r.Context(), d)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package middleware
package timeout

import (
"fmt"
Expand Down

0 comments on commit 0d6a958

Please sign in to comment.