Skip to content

Commit

Permalink
fix: cache HTTP routes (#2868)
Browse files Browse the repository at this point in the history
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
stuartwdouglas and github-actions[bot] authored Sep 27, 2024
1 parent 0e4d7f6 commit 06cd968
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 28 deletions.
26 changes: 13 additions & 13 deletions backend/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,18 +344,13 @@ func New(ctx context.Context, conn *sql.DB, config Config, devel bool, runnerSca

func (s *Service) ServeHTTP(w http.ResponseWriter, r *http.Request) {
start := time.Now()
routes, err := s.dal.GetIngressRoutes(r.Context(), r.Method)
if err != nil {
if errors.Is(err, libdal.ErrNotFound) {
http.NotFound(w, r)
observability.Ingress.Request(r.Context(), r.Method, r.URL.Path, optional.None[*schemapb.Ref](), start, optional.Some("route not found in dal"))
return
}
http.Error(w, err.Error(), http.StatusInternalServerError)
observability.Ingress.Request(r.Context(), r.Method, r.URL.Path, optional.None[*schemapb.Ref](), start, optional.Some("failed to resolve route from dal"))
requestKey := model.NewRequestKey(model.OriginIngress, fmt.Sprintf("%s %s", r.Method, r.URL.Path))
routes := s.schemaState.Load().httpRoutes[r.Method]
if len(routes) == 0 {
http.NotFound(w, r)
observability.Ingress.Request(r.Context(), r.Method, r.URL.Path, optional.None[*schemapb.Ref](), start, optional.Some("route not found in dal"))
return
}
requestKey := model.NewRequestKey(model.OriginIngress, fmt.Sprintf("%s %s", r.Method, r.URL.Path))
ingress.Handle(start, s.schemaState.Load().schema, requestKey, routes, w, r, s.timeline, s.callWithRequest)
}

Expand Down Expand Up @@ -1828,12 +1823,16 @@ func (s *Service) syncRoutesAndSchema(ctx context.Context) (ret time.Duration, e
}
}

httpRoutes, err := s.dal.GetIngressRoutes(ctx)
if err != nil {
return 0, fmt.Errorf("failed to get ingress routes: %w", err)
}
orderedModules := maps.Values(modulesByName)
sort.SliceStable(orderedModules, func(i, j int) bool {
return orderedModules[i].Name < orderedModules[j].Name
})
combined := &schema.Schema{Modules: orderedModules}
s.schemaState.Store(schemaState{schema: combined, routes: newRoutes})
s.schemaState.Store(schemaState{schema: combined, routes: newRoutes, httpRoutes: httpRoutes})
return time.Second, nil
}

Expand Down Expand Up @@ -1904,8 +1903,9 @@ type Route struct {
}

type schemaState struct {
schema *schema.Schema
routes map[string]Route
schema *schema.Schema
routes map[string]Route
httpRoutes map[string][]dalmodel.IngressRoute
}

func (r Route) String() string {
Expand Down
12 changes: 5 additions & 7 deletions backend/controller/dal/dal.go
Original file line number Diff line number Diff line change
Expand Up @@ -723,24 +723,22 @@ func (d *DAL) CreateRequest(ctx context.Context, key model.RequestKey, addr stri
return nil
}

func (d *DAL) GetIngressRoutes(ctx context.Context, method string) ([]dalmodel.IngressRoute, error) {
routes, err := d.db.GetIngressRoutes(ctx, method)
func (d *DAL) GetIngressRoutes(ctx context.Context) (map[string][]dalmodel.IngressRoute, error) {
routes, err := d.db.GetIngressRoutes(ctx)
if err != nil {
return nil, libdal.TranslatePGError(err)
}
if len(routes) == 0 {
return nil, libdal.ErrNotFound
}
return slices.Map(routes, func(row dalsql.GetIngressRoutesRow) dalmodel.IngressRoute {
return slices.GroupBy(slices.Map(routes, func(row dalsql.GetIngressRoutesRow) dalmodel.IngressRoute {
return dalmodel.IngressRoute{
Runner: row.RunnerKey,
Deployment: row.DeploymentKey,
Endpoint: row.Endpoint,
Path: row.Path,
Module: row.Module,
Verb: row.Verb,
Method: row.Method,
}
}), nil
}), func(route dalmodel.IngressRoute) string { return route.Method }), nil
}

func (d *DAL) UpsertController(ctx context.Context, key model.ControllerKey, addr string) (int64, error) {
Expand Down
2 changes: 1 addition & 1 deletion backend/controller/dal/internal/sql/querier.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions backend/controller/dal/internal/sql/queries.sql
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,10 @@ VALUES ((SELECT id FROM deployments WHERE key = sqlc.arg('key')::deployment_key

-- name: GetIngressRoutes :many
-- Get the runner endpoints corresponding to the given ingress route.
SELECT r.key AS runner_key, d.key AS deployment_key, endpoint, ir.path, ir.module, ir.verb
SELECT r.key AS runner_key, d.key AS deployment_key, endpoint, ir.path, ir.module, ir.verb, ir.method
FROM ingress_routes ir
INNER JOIN runners r ON ir.deployment_id = r.deployment_id
INNER JOIN deployments d ON ir.deployment_id = d.id
WHERE ir.method = $1;
INNER JOIN deployments d ON ir.deployment_id = d.id;

-- name: GetActiveIngressRoutes :many
SELECT d.key AS deployment_key, ir.module, ir.verb, ir.method, ir.path
Expand Down
9 changes: 5 additions & 4 deletions backend/controller/dal/internal/sql/queries.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions backend/controller/dal/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ type IngressRoute struct {
Path string
Module string
Verb string
Method string
}

type IngressRouteEntry struct {
Expand Down

0 comments on commit 06cd968

Please sign in to comment.