aboutsummaryrefslogtreecommitdiff
path: root/internal/frontend/server.go
diff options
context:
space:
mode:
authorRob Findley <rfindley@google.com>2025-05-23 14:05:37 +0000
committerRobert Findley <rfindley@google.com>2025-06-23 06:06:51 -0700
commit4b544d88ef33b543889e92d50bcc7b02d4b95ff0 (patch)
tree19fd58d9dcc6955f07465f70fd759b5df720fa56 /internal/frontend/server.go
parent041c7c0b878cb88962867185208d4d2ec79de7d0 (diff)
downloadgo-x-pkgsite-4b544d88ef33b543889e92d50bcc7b02d4b95ff0.tar.xz
internal/frontend: recycle database connections every 5m
In order to avoid imbalance between pkgsite's two database instances, recycle connections every 5 minutes. Change-Id: I9ca1e686a90f8c61619fd76454ec66163e501ee1 Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/680175 kokoro-CI: kokoro <noreply+kokoro@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Jonathan Amsterdam <jba@google.com>
Diffstat (limited to 'internal/frontend/server.go')
-rw-r--r--internal/frontend/server.go9
1 files changed, 5 insertions, 4 deletions
diff --git a/internal/frontend/server.go b/internal/frontend/server.go
index a5e5e533..3e63f69b 100644
--- a/internal/frontend/server.go
+++ b/internal/frontend/server.go
@@ -42,7 +42,7 @@ import (
type Server struct {
fetchServer FetchServerInterface
// getDataSource should never be called from a handler. It is called only in Server.errorHandler.
- getDataSource func(context.Context) internal.DataSource
+ getDataSource func(context.Context) (internal.DataSource, func())
queue queue.Queue
templateFS template.TrustedFS
staticFS fs.FS
@@ -82,9 +82,9 @@ type ServerConfig struct {
Config *config.Config
// Note that FetchServer may be nil.
FetchServer FetchServerInterface
- // DataSourceGetter should return a DataSource on each call.
+ // DataSourceGetter should return a DataSource and a release function on each call.
// It should be goroutine-safe.
- DataSourceGetter func(context.Context) internal.DataSource
+ DataSourceGetter func(context.Context) (internal.DataSource, func())
Queue queue.Queue
TemplateFS template.TrustedFS // for loading templates safely
StaticFS fs.FS // for static/ directory
@@ -503,7 +503,8 @@ func (s *Server) PanicHandler() (_ http.HandlerFunc, err error) {
func (s *Server) errorHandler(f func(w http.ResponseWriter, r *http.Request, ds internal.DataSource) error) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// Obtain a DataSource to use for this request.
- ds := s.getDataSource(r.Context())
+ ds, release := s.getDataSource(r.Context())
+ defer release()
if err := f(w, r, ds); err != nil {
s.serveError(w, r, err)
}