Skip to content

Commit

Permalink
fix: solve data race in middleware/proxy's test
Browse files Browse the repository at this point in the history
  • Loading branch information
li-jin-gou committed Oct 13, 2022
1 parent f3c7c42 commit d6f40ac
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions middleware/proxy/proxy.go
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"net/url"
"strings"
"sync"

"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/utils"
Expand Down Expand Up @@ -107,6 +108,8 @@ var client = &fasthttp.Client{
DisablePathNormalizing: true,
}

var lock sync.RWMutex

// WithTlsConfig update http client with a user specified tls.config
// This function should be called before Do and Forward.
// Deprecated: use WithClient instead.
Expand All @@ -117,6 +120,8 @@ func WithTlsConfig(tlsConfig *tls.Config) {
// WithClient sets the global proxy client.
// This function should be called before Do and Forward.
func WithClient(cli *fasthttp.Client) {
lock.Lock()
defer lock.Unlock()
client = cli
}

Expand All @@ -137,7 +142,9 @@ func Do(c *fiber.Ctx, addr string, clients ...*fasthttp.Client) error {
cli = clients[0]
} else {
// Set global client
lock.RLock()
cli = client
lock.RUnlock()
}
req := c.Request()
res := c.Response()
Expand Down

0 comments on commit d6f40ac

Please sign in to comment.