Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
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
31 changes: 27 additions & 4 deletions pkg/docker/api_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ func StartAPIServerMode(ctx context.Context, dr *resolver.DependencyResolver) er
portNum := strconv.FormatUint(uint64(wfAPIServer.PortNum), 10)
hostPort := ":" + portNum

// Create a semaphore channel to limit to 1 active connection
semaphore := make(chan struct{}, 1)

router := gin.Default()

if len(wfTrustedProxies) > 0 {
Expand All @@ -140,7 +143,7 @@ func StartAPIServerMode(ctx context.Context, dr *resolver.DependencyResolver) er
}
}

setupRoutes(router, ctx, wfAPIServer.Routes, dr)
setupRoutes(router, ctx, wfAPIServer.Routes, dr, semaphore)

dr.Logger.Printf("Starting API server on port %s", hostPort)
go func() {
Expand All @@ -152,14 +155,14 @@ func StartAPIServerMode(ctx context.Context, dr *resolver.DependencyResolver) er
return nil
}

func setupRoutes(router *gin.Engine, ctx context.Context, routes []*apiserver.APIServerRoutes, dr *resolver.DependencyResolver) {
func setupRoutes(router *gin.Engine, ctx context.Context, routes []*apiserver.APIServerRoutes, dr *resolver.DependencyResolver, semaphore chan struct{}) {
for _, route := range routes {
if route == nil || route.Path == "" {
dr.Logger.Error("route configuration is invalid", "route", route)
continue
}

handler := APIServerHandler(ctx, route, dr)
handler := APIServerHandler(ctx, route, dr, semaphore)
for _, method := range route.Methods {
switch method {
case http.MethodGet:
Expand All @@ -185,10 +188,30 @@ func setupRoutes(router *gin.Engine, ctx context.Context, routes []*apiserver.AP
}
}

func APIServerHandler(ctx context.Context, route *apiserver.APIServerRoutes, baseDr *resolver.DependencyResolver) gin.HandlerFunc {
func APIServerHandler(ctx context.Context, route *apiserver.APIServerRoutes, baseDr *resolver.DependencyResolver, semaphore chan struct{}) gin.HandlerFunc {
allowedMethods := route.Methods

return func(c *gin.Context) {
// Try to acquire the semaphore (non-blocking)
select {
case semaphore <- struct{}{}:
// Successfully acquired the semaphore
defer func() { <-semaphore }() // Release the semaphore when done
default:
// Semaphore is full, return 429 Too Many Requests
resp := APIResponse{
Success: false,
Errors: []ErrorResponse{
{
Code: http.StatusTooManyRequests,
Message: "Only one active connection is allowed",
},
},
}
c.AbortWithStatusJSON(http.StatusTooManyRequests, resp)
return
}

graphID := uuid.New().String()
baseLogger := logging.GetLogger()
logger := baseLogger.With("requestID", graphID) // Now returns *logging.Logger
Expand Down
1 change: 0 additions & 1 deletion pkg/docker/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,6 @@ RUN chmod +x /cache/anaconda*
dockerFile.WriteString(fmt.Sprintf(`
ARG DEBIAN_FRONTEND=noninteractive
ENV TZ=%s
RUN apt-get install -y tzdata
`, timezone))

// Install Necessary Tools
Expand Down
2 changes: 1 addition & 1 deletion pkg/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
var (
cachedVersion string
once sync.Once
specifiedVersion string = "0.2.11" // Default specified version
specifiedVersion string = "0.2.12" // Default specified version
UseLatest bool = false
)

Expand Down
4 changes: 2 additions & 2 deletions pkg/schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ func TestSchemaVersion(t *testing.T) {
t.Parallel()
ctx := context.Background()

const mockLockedVersion = "0.2.11" // Define the version once and reuse it
const mockVersion = "0.2.11" // Define the version once and reuse it
const mockLockedVersion = "0.2.12" // Define the version once and reuse it
const mockVersion = "0.2.12" // Define the version once and reuse it

// Save the original value of UseLatest to avoid test interference
originalUseLatest := UseLatest
Expand Down
Loading