gin
gin copied to clipboard
Does gin support parallel middlewares ?
hi, how to set the middlewares flow like below ?
no it does not.
consider wrap the 1,2,3
as 4
.
func midldleware1(c *gin.Context) {
//blalba
}
func midldleware2(c *gin.Context) {
//blalba
}
func Parrallel() gin.HandlerFunc {
return func(c *gin.Context) {
// you have to use sync.WaitGroup before
go midldleware1(c)
go midldleware2(c)
c.Next()
}
}
but it is not recommended 🗡️
consider wrap the
1,2,3
as4
.
yes it should be designed like this.
for code you may need this
func midldleware1(c *gin.Context) {
// blalba
}
func midldleware2(c *gin.Context) {
// blalba
}
func Parrallel(midldlewares ...gin.HandlerFunc) gin.HandlerFunc {
return func(c *gin.Context) {
wg := &sync.WaitGroup{}
for _, midldleware := range midldlewares {
go func() {
wg.Add(1)
defer wg.Done()
midldleware(c)
}
}
wg.Wait()
c.Next()
}
}
Parrallel(midldleware1,midldleware2)