Skip to content

Commit

Permalink
feat: use default global client when no use option (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
li-jin-gou authored Nov 8, 2022
1 parent e2419d5 commit 1045ce0
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions reverse_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ var hopHeaders = []string{
// NewSingleHostReverseProxy does not rewrite the Host header.
// To rewrite Host headers, use ReverseProxy directly with a custom
// director policy.
//
// Note: if no config.ClientOption is passed it will use the default global client.Client instance.
// When passing config.ClientOption it will initialize a local client.Client instance.
// Using ReverseProxy.SetClient if there is need for shared customized client.Client instance.
func NewSingleHostReverseProxy(target string, options ...config.ClientOption) (*ReverseProxy, error) {
r := &ReverseProxy{
Target: target,
Expand All @@ -105,11 +109,13 @@ func NewSingleHostReverseProxy(target string, options ...config.ClientOption) (*
req.Header.SetHostBytes(req.URI().Host())
},
}
c, err := client.NewClient(options...)
if err != nil {
return nil, err
if len(options) != 0 {
c, err := client.NewClient(options...)
if err != nil {
return nil, err
}
r.client = c
}
r.client = c
return r, nil
}

Expand Down Expand Up @@ -216,7 +222,11 @@ func (r *ReverseProxy) ServeHTTP(c context.Context, ctx *app.RequestContext) {
req.Header.Add("X-Forwarded-For", ip)
}
}
err := r.client.Do(c, req, resp)
fn := client.Do
if r.client != nil {
fn = r.client.Do
}
err := fn(c, req, resp)
if err != nil {
hlog.CtxErrorf(c, "HERTZ: Client request error: %#v", err.Error())
r.getErrorHandler()(ctx, err)
Expand Down

0 comments on commit 1045ce0

Please sign in to comment.