Skip to content

Commit

Permalink
feat: add worker limit for gemini
Browse files Browse the repository at this point in the history
  • Loading branch information
plutoless committed Dec 13, 2024
1 parent 70e6557 commit 41a05db
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
2 changes: 2 additions & 0 deletions server/internal/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ const (
tokenExpirationInSeconds = uint32(86400)

WORKER_TIMEOUT_INFINITY = -1

MAX_GEMINI_WORKER_COUNT = 3
)

var (
Expand Down
20 changes: 20 additions & 0 deletions server/internal/http_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,25 @@ func (s *HttpServer) handlerStart(c *gin.Context) {
return
}

// Check if the graphName contains "gemini"
if strings.Contains(req.GraphName, "gemini") {
// Count existing workers with the same graphName
graphNameCount := 0
for _, channelName := range workers.Keys() {
worker := workers.Get(channelName).(*Worker)
if worker.GraphName == req.GraphName {
graphNameCount++
}
}

// Reject if more than 3 workers are using the same graphName
if graphNameCount >= MAX_GEMINI_WORKER_COUNT {
slog.Error("handlerStart graphName workers exceed limit", "graphName", req.GraphName, "graphNameCount", graphNameCount, logTag)
s.output(c, codeErrWorkersLimit, http.StatusTooManyRequests)
return
}
}

req.WorkerHttpServerPort = getHttpServerPort()
propertyJsonFile, logFile, err := s.processProperty(&req)
if err != nil {
Expand All @@ -222,6 +241,7 @@ func (s *HttpServer) handlerStart(c *gin.Context) {

worker := newWorker(req.ChannelName, logFile, s.config.Log2Stdout, propertyJsonFile)
worker.HttpServerPort = req.WorkerHttpServerPort
worker.GraphName = req.GraphName // Save graphName in the Worker instance

if req.QuitTimeoutSeconds > 0 {
worker.QuitTimeoutSeconds = req.QuitTimeoutSeconds
Expand Down
1 change: 1 addition & 0 deletions server/internal/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type Worker struct {
LogFile string
Log2Stdout bool
PropertyJsonFile string
GraphName string // New field to store the graphName
Pid int
QuitTimeoutSeconds int
CreateTs int64
Expand Down

0 comments on commit 41a05db

Please sign in to comment.