浏览代码

哈哈,又找到你啦!美味的小孩!

Yeuoly 1 年之前
父节点
当前提交
87997c0ddf
共有 4 个文件被更改,包括 9 次插入29 次删除
  1. 1 1
      internal/controller/router.go
  2. 0 11
      internal/controller/run.go
  3. 8 16
      internal/middleware/cocrrent.go
  4. 0 1
      internal/server/server.go

+ 1 - 1
internal/controller/router.go

@@ -9,7 +9,7 @@ import (
 func Setup(eng *gin.Engine) {
 	eng.Use(middleware.MaxRequest(static.GetCoshubGlobalConfigurations().MaxRequests))
 	eng.Use(middleware.Auth())
-	eng.Use(middleware.MaxWoker(static.GetCoshubGlobalConfigurations().MaxWorkers))
+	eng.Use(middleware.MaxWorker(static.GetCoshubGlobalConfigurations().MaxWorkers))
 
 	eng.POST("/v1/sandbox/run", RunSandboxController)
 }

+ 0 - 11
internal/controller/run.go

@@ -3,20 +3,9 @@ package controller
 import (
 	"github.com/gin-gonic/gin"
 	"github.com/langgenius/dify-sandbox/internal/service"
-	"github.com/langgenius/dify-sandbox/internal/static"
 	"github.com/langgenius/dify-sandbox/internal/types"
 )
 
-var (
-	queue chan bool
-)
-
-func InitSandBoxQueue() {
-	if queue == nil {
-		queue = make(chan bool, static.GetCoshubGlobalConfigurations().MaxWorkers)
-	}
-}
-
 func RunSandboxController(c *gin.Context) {
 	BindRequest(c, func(req struct {
 		Language string `json:"language" form:"language" binding:"required"`

+ 8 - 16
internal/middleware/cocrrent.go

@@ -9,24 +9,16 @@ import (
 	"github.com/langgenius/dify-sandbox/internal/utils/log"
 )
 
-func MaxWoker(max int) gin.HandlerFunc {
-	queue := make(chan *gin.Context, max)
-
-	for i := 0; i < max; i++ {
-		i := i
-		go func() {
-			log.Info("code runner worker %d started", i)
-			for {
-				select {
-				case c := <-queue:
-					c.Next()
-				}
-			}
-		}()
-	}
+func MaxWorker(max int) gin.HandlerFunc {
+	log.Info("setting max workers to %d", max)
+	sem := make(chan struct{}, max)
 
 	return func(c *gin.Context) {
-		queue <- c
+		sem <- struct{}{}
+		defer func() {
+			<-sem
+		}()
+		c.Next()
 	}
 }
 

+ 0 - 1
internal/server/server.go

@@ -26,7 +26,6 @@ func initServer() {
 
 	r := gin.Default()
 	controller.Setup(r)
-	controller.InitSandBoxQueue()
 
 	r.Run(fmt.Sprintf(":%d", config.App.Port))
 }