Skip to content

Commit

Permalink
fix(gateway): send on closed chan - (*Handle).webRequestQ
Browse files Browse the repository at this point in the history
  • Loading branch information
Sidddddarth committed May 27, 2024
1 parent ba94ed4 commit c4434f1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
2 changes: 2 additions & 0 deletions gateway/handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"net/http"
"strings"
"sync"
"sync/atomic"
"time"

jsoniter "github.com/json-iterator/go"
Expand Down Expand Up @@ -94,6 +95,7 @@ type Handle struct {
// other state

backendConfigInitialised bool
inFlightRequestsCount *atomic.Uint64

trackCounterMu sync.Mutex // protects trackSuccessCount and trackFailureCount
trackSuccessCount int
Expand Down
18 changes: 18 additions & 0 deletions gateway/handle_lifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http/httputil"
"net/url"
"strconv"
"sync/atomic"
"time"

"github.com/rudderlabs/rudder-schemas/go/stream"
Expand Down Expand Up @@ -68,6 +69,7 @@ func (gw *Handle) Setup(
gw.versionHandler = versionHandler
gw.rsourcesService = rsourcesService
gw.sourcehandle = sourcehandle
gw.inFlightRequestsCount = new(atomic.Uint64)

// Port where GW is running
gw.conf.webPort = config.GetIntVar(8080, 1, "Gateway.webPort")
Expand Down Expand Up @@ -383,6 +385,18 @@ func (gw *Handle) StartWebHandler(ctx context.Context) error {
gw.logger.Child("rsources_failed_keys"),
)
srvMux.Use(
func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_ = gw.inFlightRequestsCount.Add(1)
h.ServeHTTP(w, r)
for {
prev := gw.inFlightRequestsCount.Load()
if gw.inFlightRequestsCount.CompareAndSwap(prev, prev-1) {
break
}
}
})
},
chiware.StatMiddleware(ctx, stats.Default, component),
middleware.LimitConcurrentRequests(gw.conf.maxConcurrentRequests),
middleware.UncompressMiddleware,
Expand Down Expand Up @@ -482,6 +496,10 @@ func (gw *Handle) Shutdown() error {
return err
}

for gw.inFlightRequestsCount.Load() != 0 {
time.Sleep(250 * time.Millisecond)

Check warning on line 500 in gateway/handle_lifecycle.go

View check run for this annotation

Codecov / codecov/patch

gateway/handle_lifecycle.go#L500

Added line #L500 was not covered by tests
}

// UserWebRequestWorkers
for _, worker := range gw.userWebRequestWorkers {
close(worker.webRequestQ)
Expand Down

0 comments on commit c4434f1

Please sign in to comment.