diff --git a/Makefile b/Makefile index 35599e11..457f102d 100644 --- a/Makefile +++ b/Makefile @@ -28,7 +28,7 @@ unit_tests: go test -race $$(go list ./... | grep -v integration_tests) integration_tests: build - go test -race -v ./cs_integration_tests + go test -race -v ./integration_tests update_deps: go get -t -u ./... && go mod tidy && go mod vendor diff --git a/cs_integration_tests/backend_helpers.go b/integration_tests/backend_helpers.go similarity index 100% rename from cs_integration_tests/backend_helpers.go rename to integration_tests/backend_helpers.go diff --git a/cs_integration_tests/error_handling_test.go b/integration_tests/error_handling_test.go similarity index 100% rename from cs_integration_tests/error_handling_test.go rename to integration_tests/error_handling_test.go diff --git a/cs_integration_tests/gone_test.go b/integration_tests/gone_test.go similarity index 100% rename from cs_integration_tests/gone_test.go rename to integration_tests/gone_test.go diff --git a/cs_integration_tests/http_request_helpers.go b/integration_tests/http_request_helpers.go similarity index 100% rename from cs_integration_tests/http_request_helpers.go rename to integration_tests/http_request_helpers.go diff --git a/cs_integration_tests/integration_test.go b/integration_tests/integration_test.go similarity index 100% rename from cs_integration_tests/integration_test.go rename to integration_tests/integration_test.go diff --git a/cs_integration_tests/metrics_test.go b/integration_tests/metrics_test.go similarity index 100% rename from cs_integration_tests/metrics_test.go rename to integration_tests/metrics_test.go diff --git a/cs_integration_tests/performance_test.go b/integration_tests/performance_test.go similarity index 100% rename from cs_integration_tests/performance_test.go rename to integration_tests/performance_test.go diff --git a/cs_integration_tests/proxy_function_test.go b/integration_tests/proxy_function_test.go similarity index 100% rename from cs_integration_tests/proxy_function_test.go rename to integration_tests/proxy_function_test.go diff --git a/cs_integration_tests/redirect_test.go b/integration_tests/redirect_test.go similarity index 100% rename from cs_integration_tests/redirect_test.go rename to integration_tests/redirect_test.go diff --git a/cs_integration_tests/reload_api_test.go b/integration_tests/reload_api_test.go similarity index 100% rename from cs_integration_tests/reload_api_test.go rename to integration_tests/reload_api_test.go diff --git a/cs_integration_tests/route_helpers.go b/integration_tests/route_helpers.go similarity index 100% rename from cs_integration_tests/route_helpers.go rename to integration_tests/route_helpers.go diff --git a/cs_integration_tests/route_loading_test.go b/integration_tests/route_loading_test.go similarity index 100% rename from cs_integration_tests/route_loading_test.go rename to integration_tests/route_loading_test.go diff --git a/cs_integration_tests/route_selection_test.go b/integration_tests/route_selection_test.go similarity index 100% rename from cs_integration_tests/route_selection_test.go rename to integration_tests/route_selection_test.go diff --git a/cs_integration_tests/router_logging.go b/integration_tests/router_logging.go similarity index 100% rename from cs_integration_tests/router_logging.go rename to integration_tests/router_logging.go diff --git a/cs_integration_tests/router_support.go b/integration_tests/router_support.go similarity index 100% rename from cs_integration_tests/router_support.go rename to integration_tests/router_support.go diff --git a/cs_integration_tests/testdata/init-user-db.sh b/integration_tests/testdata/init-user-db.sh similarity index 100% rename from cs_integration_tests/testdata/init-user-db.sh rename to integration_tests/testdata/init-user-db.sh diff --git a/lib/load_routes.go b/lib/load_routes.go index ebf6d325..590177fc 100644 --- a/lib/load_routes.go +++ b/lib/load_routes.go @@ -27,7 +27,7 @@ type PgxIface interface { Query(context.Context, string, ...interface{}) (pgx.Rows, error) } -func addHandler(mux *triemux.Mux, route *CsRoute, backends map[string]http.Handler) error { +func addHandler(mux *triemux.Mux, route *Route, backends map[string]http.Handler) error { if route.IncomingPath == nil || route.RouteType == nil { logWarn(fmt.Sprintf("router: found route %+v with nil fields, skipping!", route)) return nil @@ -76,7 +76,7 @@ func addHandler(mux *triemux.Mux, route *CsRoute, backends map[string]http.Handl return nil } -func loadRoutesFromCS(pool PgxIface, mux *triemux.Mux, backends map[string]http.Handler) error { +func loadRoutes(pool PgxIface, mux *triemux.Mux, backends map[string]http.Handler) error { rows, err := pool.Query(context.Background(), loadRoutesQuery) if err != nil { @@ -86,7 +86,7 @@ func loadRoutesFromCS(pool PgxIface, mux *triemux.Mux, backends map[string]http. defer rows.Close() for rows.Next() { - route := &CsRoute{} + route := &Route{} scans := []any{ &route.BackendID, &route.IncomingPath, @@ -131,7 +131,7 @@ func (rt *Router) listenForContentStoreUpdates(ctx context.Context) error { func(ctx context.Context, notification *pgconn.Notification, conn *pgx.Conn) error { // This is a non-blocking send, if there is already a notification to reload we don't need to send another one select { - case rt.CsReloadChan <- true: + case rt.ReloadChan <- true: default: } return nil @@ -148,13 +148,13 @@ func (rt *Router) listenForContentStoreUpdates(ctx context.Context) error { return nil } -func (rt *Router) PeriodicCSRouteUpdates() { +func (rt *Router) PeriodicRouteUpdates() { tick := time.Tick(5 * time.Second) for range tick { - if time.Since(rt.csLastAttemptReloadTime) > rt.opts.RouteReloadInterval { + if time.Since(rt.lastAttemptReloadTime) > rt.opts.RouteReloadInterval { // This is a non-blocking send, if there is already a notification to reload we don't need to send another one select { - case rt.CsReloadChan <- true: + case rt.ReloadChan <- true: default: } } @@ -162,12 +162,12 @@ func (rt *Router) PeriodicCSRouteUpdates() { } func (rt *Router) waitForReload() { - for range rt.CsReloadChan { - rt.reloadCsRoutes(rt.pool) + for range rt.ReloadChan { + rt.reloadRoutes(rt.pool) } } -func (rt *Router) reloadCsRoutes(pool PgxIface) { +func (rt *Router) reloadRoutes(pool PgxIface) { var success bool timer := prometheus.NewTimer(prometheus.ObserverFunc(func(v float64) { labels := prometheus.Labels{"success": strconv.FormatBool(success), "source": "content-store"} @@ -178,7 +178,7 @@ func (rt *Router) reloadCsRoutes(pool PgxIface) { success = true if r := recover(); r != nil { success = false - logWarn("router: recovered from panic in reloadCsRoutes:", r) + logWarn("router: recovered from panic in reloadRoutes:", r) logInfo("router: original content store routes have not been modified") errorMessage := fmt.Sprintf("panic: %v", r) err := logger.RecoveredError{ErrorMessage: errorMessage} @@ -187,12 +187,12 @@ func (rt *Router) reloadCsRoutes(pool PgxIface) { timer.ObserveDuration() }() - rt.csLastAttemptReloadTime = time.Now() + rt.lastAttemptReloadTime = time.Now() logInfo("router: reloading routes from content store") newmux := triemux.NewMux() - err := loadRoutesFromCS(pool, newmux, rt.backends) + err := loadRoutes(pool, newmux, rt.backends) if err != nil { logWarn(fmt.Sprintf("router: error reloading routes from content store: %v", err)) return @@ -201,7 +201,7 @@ func (rt *Router) reloadCsRoutes(pool PgxIface) { routeCount := newmux.RouteCount() rt.lock.Lock() - rt.csMux = newmux + rt.mux = newmux rt.lock.Unlock() logInfo(fmt.Sprintf("router: reloaded %d routes from content store", routeCount)) diff --git a/lib/load_routes_test.go b/lib/load_routes_test.go index ef1f6431..d414d4cd 100644 --- a/lib/load_routes_test.go +++ b/lib/load_routes_test.go @@ -12,7 +12,7 @@ import ( "github.com/pashagolub/pgxmock/v4" ) -var _ = Describe("loadRoutesFromCS", func() { +var _ = Describe("loadRoutes", func() { var ( mockPool pgxmock.PgxPoolIface mux *triemux.Mux @@ -53,7 +53,7 @@ var _ = Describe("loadRoutesFromCS", func() { mockPool.ExpectQuery("WITH").WillReturnRows(rows) - err := loadRoutesFromCS(mockPool, mux, backends) + err := loadRoutes(mockPool, mux, backends) Expect(err).NotTo(HaveOccurred()) }) @@ -88,7 +88,7 @@ var _ = Describe("loadRoutesFromCS", func() { mockPool.ExpectQuery("WITH").WillReturnRows(rows) - err := loadRoutesFromCS(mockPool, mux, backends) + err := loadRoutes(mockPool, mux, backends) Expect(err).NotTo(HaveOccurred()) }) @@ -146,7 +146,7 @@ var _ = Describe("loadRoutesFromCS", func() { AddRow(nil, stringPtr("/redirect-prefix-preserve"), stringPtr("prefix"), stringPtr("/redirected-prefix-preserve"), stringPtr("preserve"), stringPtr("redirect"), nil) mockPool.ExpectQuery("WITH").WillReturnRows(rows) - err := loadRoutesFromCS(mockPool, mux, backends) + err := loadRoutes(mockPool, mux, backends) Expect(err).NotTo(HaveOccurred()) }) @@ -207,7 +207,7 @@ var _ = Describe("loadRoutesFromCS", func() { }) var _ = Describe("Router", func() { - Describe("reloadCsRoutes", func() { + Describe("reloadRoutes", func() { var ( mockPool pgxmock.PgxPoolIface router *Router @@ -242,9 +242,9 @@ var _ = Describe("Router", func() { mockPool.ExpectQuery("WITH").WillReturnRows(rows) - router.reloadCsRoutes(mockPool) + router.reloadRoutes(mockPool) - Expect(router.csMux.RouteCount()).To(Equal(2)) + Expect(router.mux.RouteCount()).To(Equal(2)) }) It("should handle panic and log error", func() { @@ -252,7 +252,7 @@ var _ = Describe("Router", func() { mockPool.ExpectQuery("WITH").WillReturnError(fmt.Errorf("some error")) - Expect(func() { router.reloadCsRoutes(mockPool) }).NotTo(Panic()) + Expect(func() { router.reloadRoutes(mockPool) }).NotTo(Panic()) }) }) }) diff --git a/lib/router.go b/lib/router.go index 328958da..453c4682 100644 --- a/lib/router.go +++ b/lib/router.go @@ -28,14 +28,14 @@ const ( // Router is a wrapper around an HTTP multiplexer (trie.Mux) which retrieves its // routes from a postgres database. type Router struct { - backends map[string]http.Handler - csMux *triemux.Mux - lock sync.RWMutex - logger logger.Logger - opts Options - CsReloadChan chan bool - pool *pgxpool.Pool - csLastAttemptReloadTime time.Time + backends map[string]http.Handler + mux *triemux.Mux + lock sync.RWMutex + logger logger.Logger + opts Options + ReloadChan chan bool + pool *pgxpool.Pool + lastAttemptReloadTime time.Time } type Options struct { @@ -69,17 +69,17 @@ func NewRouter(o Options) (rt *Router, err error) { } logInfo("router: postgres connection pool created") - csReloadChan := make(chan bool, 1) + reloadChan := make(chan bool, 1) rt = &Router{ - backends: backends, - csMux: triemux.NewMux(), - logger: l, - opts: o, - CsReloadChan: csReloadChan, - pool: pool, + backends: backends, + mux: triemux.NewMux(), + logger: l, + opts: o, + ReloadChan: reloadChan, + pool: pool, } - rt.reloadCsRoutes(pool) + rt.reloadRoutes(pool) go func() { if err := rt.listenForContentStoreUpdates(context.Background()); err != nil { @@ -117,7 +117,7 @@ func (rt *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) { var mux *triemux.Mux rt.lock.RLock() - mux = rt.csMux + mux = rt.mux rt.lock.RUnlock() mux.ServeHTTP(w, req) diff --git a/lib/router_api.go b/lib/router_api.go index 3c09ef46..ab0a4fbe 100644 --- a/lib/router_api.go +++ b/lib/router_api.go @@ -22,7 +22,7 @@ func NewAPIHandler(rout *Router) (api http.Handler, err error) { // If the channel is already full, no message will be sent and the request // won't be blocked. select { - case rout.CsReloadChan <- true: + case rout.ReloadChan <- true: default: } logInfo("router: reload queued") diff --git a/lib/routes.go b/lib/routes.go index f7f8fe13..3b90f005 100644 --- a/lib/routes.go +++ b/lib/routes.go @@ -10,7 +10,7 @@ const ( HandlerTypeGone = "gone" ) -type CsRoute struct { +type Route struct { IncomingPath *string RouteType *string BackendID *string @@ -23,7 +23,7 @@ type CsRoute struct { // returns the backend which should be used for this route // if the route is a gone route, but has an explaination in the details field, // then route to the backend, or by default to government-frontend -func (route *CsRoute) backend() *string { +func (route *Route) backend() *string { if route.SchemaName != nil && *route.SchemaName == "gone" && !route.gone() { if route.BackendID != nil { return route.BackendID @@ -35,7 +35,7 @@ func (route *CsRoute) backend() *string { return route.BackendID } -func (route *CsRoute) handlerType() string { +func (route *Route) handlerType() string { switch { case route.redirect(): return HandlerTypeRedirect @@ -46,7 +46,7 @@ func (route *CsRoute) handlerType() string { } } -func (route *CsRoute) gone() bool { +func (route *Route) gone() bool { if route.SchemaName != nil && *route.SchemaName == "gone" { if route.Details == nil { // if the details field is empty, use a standard gone route @@ -72,11 +72,11 @@ func (route *CsRoute) gone() bool { return false } -func (route *CsRoute) redirect() bool { +func (route *Route) redirect() bool { return route.SchemaName != nil && *route.SchemaName == "redirect" } -func (route *CsRoute) segmentsMode() string { +func (route *Route) segmentsMode() string { if route.SegmentsMode == nil && route.SchemaName != nil && *route.SchemaName == "redirect" { if *route.RouteType == "prefix" { return "preserve" diff --git a/lib/routes_test.go b/lib/routes_test.go index 9bf48adf..3f0c742b 100644 --- a/lib/routes_test.go +++ b/lib/routes_test.go @@ -5,11 +5,11 @@ import ( . "github.com/onsi/gomega" ) -var _ = Describe("CsRoute", func() { - var route *CsRoute +var _ = Describe("Route", func() { + var route *Route BeforeEach(func() { - route = &CsRoute{} + route = &Route{} }) Describe("backend", func() { diff --git a/main.go b/main.go index ecc1ee65..319e5138 100644 --- a/main.go +++ b/main.go @@ -117,7 +117,7 @@ func main() { if err != nil { log.Fatal(err) } - go rout.PeriodicCSRouteUpdates() + go rout.PeriodicRouteUpdates() go listenAndServeOrFatal(pubAddr, rout, feReadTimeout, feWriteTimeout) log.Printf("router: listening for requests on %v", pubAddr)