Skip to content

Commit

Permalink
fix: double multipart max memory, increase deadline & error logging (#30
Browse files Browse the repository at this point in the history
)

* perf: increase context timeout, double max multipart memory to 64mb

* refactor: proper logging of context deadline, increase to 10s

* chore: update docs
  • Loading branch information
jarvis8x7b authored Oct 16, 2024
1 parent af55662 commit 366a26a
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 19 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Dojo Subnet API
# Dojo Worker API

Repository for our Dojo Subnet APIs. Check request.REST file for developed APIs to test out.(make sure that REST client extension is already installed)
Extension ID: humao.rest-client
Expand Down
15 changes: 8 additions & 7 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@ package main

import (
"context"
"dojo-api/pkg/api"
"dojo-api/pkg/cache"
"dojo-api/pkg/orm"
"dojo-api/utils"
"net/http"
"os"
"os/signal"
"strings"
"syscall"
"time"

"dojo-api/pkg/api"
"dojo-api/pkg/cache"
"dojo-api/pkg/orm"
"dojo-api/utils"

_ "dojo-api/docs"

"github.com/gin-contrib/cors"
Expand All @@ -23,9 +24,9 @@ import (
ginSwagger "github.com/swaggo/gin-swagger"
)

// @title Dojo Worker API
// @version 1.0
// @description This is the worker API for the Dojo project.
// @title Dojo Worker API
// @version 1.0
// @description This is the worker API for the Dojo project.

func main() {
loadEnvVars()
Expand Down
4 changes: 2 additions & 2 deletions pkg/api/controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,14 +173,14 @@ func CreateTasksController(c *gin.Context) {
}

taskService := task.NewTaskService()
tasks, errors := taskService.CreateTasks(requestBody, minerUser.ID)
tasks, errors := taskService.CreateTasksWithTimeout(requestBody, minerUser.ID, 10*time.Second)

log.Info().Msg("Tasks created successfully")
if len(tasks) == 0 {
c.AbortWithStatusJSON(http.StatusBadRequest, defaultErrorResponse(errors))
return
}

log.Info().Msg("Tasks created successfully")
taskIds := make([]string, 0, len(tasks))
for _, task := range tasks {
taskIds = append(taskIds, task.ID)
Expand Down
54 changes: 45 additions & 9 deletions pkg/task/task_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ package task

import (
"context"
"dojo-api/db"
"dojo-api/pkg/orm"
"dojo-api/pkg/sandbox"
"dojo-api/utils"
"encoding/json"
"errors"
"fmt"
Expand All @@ -16,6 +12,11 @@ import (
"strconv"
"time"

"dojo-api/db"
"dojo-api/pkg/orm"
"dojo-api/pkg/sandbox"
"dojo-api/utils"

"github.com/gin-gonic/gin"
"github.com/rs/zerolog/log"
)
Expand Down Expand Up @@ -196,11 +197,41 @@ func IsValidCriteriaType(criteriaType CriteriaType) bool {
}
}

// create task
func (s *TaskService) CreateTasks(request CreateTaskRequest, minerUserId string) ([]*db.TaskModel, []error) {
ctxWithTimeout, cancel := context.WithTimeout(context.Background(), 3*time.Second)
func (s *TaskService) CreateTasksWithTimeout(request CreateTaskRequest, minerUserId string, timeout time.Duration) ([]*db.TaskModel, []error) {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()

type result struct {
tasks []*db.TaskModel
errs []error
}

resultChan := make(chan result, 1)

go func() {
tasks, errs := s.CreateTasks(ctx, request, minerUserId)
resultChan <- result{tasks: tasks, errs: errs}
}()

select {
case <-ctx.Done():
if ctx.Err() == context.DeadlineExceeded {
log.Error().Dur("timeout", timeout).Msg("CreateTasks timed out due to deadline")
return nil, []error{fmt.Errorf("operation timed out after %v", timeout)}
}
log.Error().Err(ctx.Err()).Msg("Context canceled while creating tasks")
return nil, []error{ctx.Err()}
case res := <-resultChan:
if len(res.tasks) == 0 && len(res.errs) == 0 {
log.Warn().Msg("No tasks created and no errors reported")
return nil, []error{fmt.Errorf("no tasks were created and no errors were reported")}
}
return res.tasks, res.errs
}
}

// create task
func (s *TaskService) CreateTasks(ctx context.Context, request CreateTaskRequest, minerUserId string) ([]*db.TaskModel, []error) {
tasks := make([]*db.TaskModel, 0)
errors := make([]error, 0)

Expand Down Expand Up @@ -244,7 +275,7 @@ func (s *TaskService) CreateTasks(request CreateTaskRequest, minerUserId string)
taskToCreate.TotalReward = &request.TotalRewards
}

task, err := taskORM.CreateTask(ctxWithTimeout, taskToCreate, minerUserId)
task, err := taskORM.CreateTask(ctx, taskToCreate, minerUserId)
if err != nil {
log.Error().Msgf("Error creating task: %v", err)
errors = append(errors, err)
Expand Down Expand Up @@ -505,7 +536,6 @@ func ValidateTaskData(taskData TaskData) error {
return fmt.Errorf("invalid completion format: %v", taskresponse.Completion)
}
}

}

if len(taskData.Criteria) == 0 {
Expand Down Expand Up @@ -679,6 +709,12 @@ func (t *TaskService) GetCompletedTaskMap(ctx context.Context, workerId string)
}

func ProcessRequestBody(c *gin.Context) (CreateTaskRequest, error) {
// set max memory to 64 MB
if err := c.Request.ParseMultipartForm(64 << 20); err != nil {
log.Error().Err(err).Msg("Failed to parse multipart form")
return CreateTaskRequest{}, fmt.Errorf("failed to parse form: %w", err)
}

var reqbody CreateTaskRequest
title := c.PostForm("title")
body := c.PostForm("body")
Expand Down

0 comments on commit 366a26a

Please sign in to comment.