Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions internal/api/v1/handler_list_instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package v1
import (
"net/http"

"github.com/formancehq/go-libs/v3/bun/bunpaginate"
"github.com/formancehq/orchestration/internal/workflow"

api "github.com/formancehq/orchestration/internal/api"
Expand All @@ -13,7 +14,16 @@ import (
func listInstances(backend api.Backend) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {

// Bound the query: without a page size, bunpaginate applies no LIMIT,
// loading the entire workflow_instances table per request.
pageSize, err := bunpaginate.GetPageSize(r)
if err != nil {
sharedapi.BadRequest(w, "VALIDATION", err)
return
}

runs, err := backend.ListInstances(r.Context(), workflow.ListInstancesQuery{
PageSize: pageSize,
Options: workflow.ListInstancesOptions{
WorkflowID: r.URL.Query().Get("workflowID"),
Running: sharedapi.QueryParamBool(r, "running"),
Expand Down
35 changes: 35 additions & 0 deletions internal/api/v1/handler_list_instances_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,38 @@ func TestListInstances(t *testing.T) {
require.Len(t, instances, 0)
})
}

func TestListInstancesIsBounded(t *testing.T) {
ctx := logging.TestingContext()

test(t, func(router *chi.Mux, m api.Backend, db *bun.DB) {
w := workflow.New(workflow.Config{})
_, err := db.NewInsert().Model(&w).Exec(ctx)
require.NoError(t, err)

for i := 0; i < 20; i++ {
instance := workflow.NewInstance(uuid.NewString(), w.ID)
_, err := db.NewInsert().Model(&instance).Exec(ctx)
require.NoError(t, err)
}

// Without a page size the default (15) bounds the result instead of
// loading the whole table.
req := httptest.NewRequest(http.MethodGet, "/instances", nil)
rec := httptest.NewRecorder()
router.ServeHTTP(rec, req)
require.Equal(t, http.StatusOK, rec.Result().StatusCode)
instances := make([]workflow.Instance, 0)
sharedapi.ReadResponse(t, rec, &instances)
require.Len(t, instances, 15)

// An explicit page size is honoured.
req = httptest.NewRequest(http.MethodGet, "/instances?pageSize=5", nil)
rec = httptest.NewRecorder()
router.ServeHTTP(rec, req)
require.Equal(t, http.StatusOK, rec.Result().StatusCode)
instances = make([]workflow.Instance, 0)
sharedapi.ReadResponse(t, rec, &instances)
require.Len(t, instances, 5)
})
}
12 changes: 11 additions & 1 deletion internal/api/v1/handler_list_workflows.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,17 @@ import (
func listWorkflows(backend api2.Backend) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {

workflows, err := backend.ListWorkflows(r.Context(), bunpaginate.OffsetPaginatedQuery[any]{})
// Bound the query: without a page size, bunpaginate applies no LIMIT,

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR applies the v2 pagination pattern to /v1/workflows and /v1/instances, but /v1/triggers/{triggerID}/occurrences still builds ListTriggersOccurrencesQuery without PageSize, while the v2 handler already calls bunpaginate.GetPageSize. Since this fix is meant to bound the v1 list endpoints, please include the occurrences handler too; otherwise that endpoint still runs with PageSize == 0 and no LIMIT.

// loading the entire workflows table per request.
pageSize, err := bunpaginate.GetPageSize(r)
if err != nil {
api.BadRequest(w, "VALIDATION", err)
return
}

workflows, err := backend.ListWorkflows(r.Context(), bunpaginate.OffsetPaginatedQuery[any]{
PageSize: pageSize,
})
if err != nil {
api.InternalServerError(w, r, err)
return
Expand Down
Loading