Skip to content

Commit

Permalink
Drop "CS" prefix in naming
Browse files Browse the repository at this point in the history
As we no longer need to distingish code from the version that loads from
the Content Store.
  • Loading branch information
theseanything committed Dec 9, 2024
1 parent 041b6f3 commit 2c46c40
Show file tree
Hide file tree
Showing 24 changed files with 51 additions and 51 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
28 changes: 14 additions & 14 deletions lib/load_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -148,26 +148,26 @@ 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:
}
}
}
}

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"}
Expand All @@ -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}
Expand All @@ -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
Expand All @@ -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))
Expand Down
16 changes: 8 additions & 8 deletions lib/load_routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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())
})

Expand Down Expand Up @@ -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())
})

Expand Down Expand Up @@ -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())
})

Expand Down Expand Up @@ -207,7 +207,7 @@ var _ = Describe("loadRoutesFromCS", func() {
})

var _ = Describe("Router", func() {
Describe("reloadCsRoutes", func() {
Describe("reloadRoutes", func() {
var (
mockPool pgxmock.PgxPoolIface
router *Router
Expand Down Expand Up @@ -242,17 +242,17 @@ 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() {
defer GinkgoRecover()

mockPool.ExpectQuery("WITH").WillReturnError(fmt.Errorf("some error"))

Expect(func() { router.reloadCsRoutes(mockPool) }).NotTo(Panic())
Expect(func() { router.reloadRoutes(mockPool) }).NotTo(Panic())
})
})
})
34 changes: 17 additions & 17 deletions lib/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lib/router_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
12 changes: 6 additions & 6 deletions lib/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const (
HandlerTypeGone = "gone"
)

type CsRoute struct {
type Route struct {
IncomingPath *string
RouteType *string
BackendID *string
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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"
Expand Down
6 changes: 3 additions & 3 deletions lib/routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 2c46c40

Please sign in to comment.