Skip to content

Commit

Permalink
feat: option to rewrite the host header
Browse files Browse the repository at this point in the history
  • Loading branch information
mcharytoniuk committed Sep 13, 2024
1 parent c6e8921 commit ded12e4
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 10 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,15 @@ You can enable dashboard to see the status of the agents with
`--management-dashboard-enable=true` flag. If enabled, it is available at the
management server address under `/dashboard` path.

#### Rewriting the `Host` Header

> [!NOTE]
> Available since v0.8.0
In some cases (see: [#20](https://github.com/distantmagic/paddler/issues/20)), you might want to rewrite the `Host` header.

In such cases, you can use the `--rewrite-host-header` flag. If used, Paddler will use the `external` host provided by agents instead of the balancer host when forwarding the requests.

## Feature Highlights

### Aggregated Health Status
Expand Down
3 changes: 3 additions & 0 deletions cmd/Balancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ func (self *Balancer) Action(cliContext *cli.Context) error {
return err
}

self.Logger.Debug("rewrite-host-header", "enabled", self.LoadBalancerConfiguration.RewriteHostHeader)

bufferedRequestsStats := &loadbalancer.BufferedRequestsStats{}

managementServer := &management.Server{
Expand All @@ -61,6 +63,7 @@ func (self *Balancer) Action(cliContext *cli.Context) error {
RespondToRegisterTarget: &management.RespondToRegisterTarget{
LoadBalancerTargetRegistrar: &loadbalancer.LoadBalancerTargetRegistrar{
HttpClient: http.DefaultClient,
LoadBalancerConfiguration: self.LoadBalancerConfiguration,
LoadBalancerTargetCollection: loadBalancer.LoadBalancerTargetCollection,
Logger: self.Logger,
},
Expand Down
36 changes: 36 additions & 0 deletions loadbalancer/CreateLlamaCppTargetReverseProxy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package loadbalancer

import (
"net/http"
"net/http/httputil"

"github.com/hashicorp/go-hclog"
)

func CreateLlamaCppTargetReverseProxy(
logger hclog.Logger,
loadBalancerConfiguration *LoadBalancerConfiguration,
targetConfiguration *LlamaCppTargetConfiguration,
) *httputil.ReverseProxy {
reverseProxy := httputil.NewSingleHostReverseProxy(
targetConfiguration.LlamaCppConfiguration.HttpAddress.GetBaseUrl(),
)

reverseProxy.ErrorLog = logger.StandardLogger(&hclog.StandardLoggerOptions{
InferLevels: true,
})

if !loadBalancerConfiguration.RewriteHostHeader {
return reverseProxy
}

originalDirector := reverseProxy.Director

reverseProxy.Director = func(req *http.Request) {
originalDirector(req)

req.Host = targetConfiguration.LlamaCppConfiguration.HttpAddress.GetHostWithPort()
}

return reverseProxy
}
1 change: 1 addition & 0 deletions loadbalancer/LoadBalancerConfiguration.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ import "time"
type LoadBalancerConfiguration struct {
RequestBufferSize uint
RequestBufferTimeout time.Duration
RewriteHostHeader bool
}
1 change: 1 addition & 0 deletions loadbalancer/LoadBalancerTargetCollection_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func BenchmarkLoadBalancerTargetCollection(b *testing.B) {

loadBalancerTargetRegistrar := &LoadBalancerTargetRegistrar{
HttpClient: http.DefaultClient,
LoadBalancerConfiguration: &LoadBalancerConfiguration{},
LoadBalancerTargetCollection: NewLoadBalancerTargetCollection(llamaCppHealthStatusAggregate),
Logger: hclog.NewNullLogger(),
}
Expand Down
16 changes: 6 additions & 10 deletions loadbalancer/LoadBalancerTargetRegistrar.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package loadbalancer

import (
"net/http"
"net/http/httputil"
"time"

"github.com/distantmagic/paddler/llamacpp"
Expand All @@ -11,6 +10,7 @@ import (

type LoadBalancerTargetRegistrar struct {
HttpClient *http.Client
LoadBalancerConfiguration *LoadBalancerConfiguration
LoadBalancerTargetCollection *LoadBalancerTargetCollection
Logger hclog.Logger
}
Expand Down Expand Up @@ -43,14 +43,6 @@ func (self *LoadBalancerTargetRegistrar) registerTarget(
"host", targetConfiguration.LlamaCppConfiguration.HttpAddress.GetHostWithPort(),
)

reverseProxy := httputil.NewSingleHostReverseProxy(
targetConfiguration.LlamaCppConfiguration.HttpAddress.GetBaseUrl(),
)

reverseProxy.ErrorLog = self.Logger.Named("ReverseProxy").StandardLogger(&hclog.StandardLoggerOptions{
InferLevels: true,
})

self.LoadBalancerTargetCollection.RegisterTarget(&LlamaCppTarget{
LastUpdate: time.Now(),
LlamaCppClient: &llamacpp.LlamaCppClient{
Expand All @@ -60,6 +52,10 @@ func (self *LoadBalancerTargetRegistrar) registerTarget(
LlamaCppSlotsAggregatedStatus: llamaCppSlotsAggregatedStatus,
LlamaCppTargetConfiguration: targetConfiguration,
RemainingTicksUntilRemoved: 3,
ReverseProxy: reverseProxy,
ReverseProxy: CreateLlamaCppTargetReverseProxy(
self.Logger.Named("ReverseProxy"),
self.LoadBalancerConfiguration,
targetConfiguration,
),
})
}
1 change: 1 addition & 0 deletions loadbalancer/LoadBalancerTargetRegistrar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func TestTargetOrderIsPreserved(t *testing.T) {

loadBalancerTargetRegistrar := &LoadBalancerTargetRegistrar{
HttpClient: http.DefaultClient,
LoadBalancerConfiguration: &LoadBalancerConfiguration{},
LoadBalancerTargetCollection: NewLoadBalancerTargetCollection(llamaCppHealthStatusAggregate),
Logger: hclog.NewNullLogger(),
}
Expand Down
1 change: 1 addition & 0 deletions loadbalancer/LoadBalancerTemporalManager_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func BenchmarkLoadBalancerTemporalManager(b *testing.B) {

loadBalancerTargetRegistrar := &LoadBalancerTargetRegistrar{
HttpClient: http.DefaultClient,
LoadBalancerConfiguration: &LoadBalancerConfiguration{},
LoadBalancerTargetCollection: loadBalancerTargetCollection,
Logger: hclog.NewNullLogger(),
}
Expand Down
5 changes: 5 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,11 @@ func main() {
Value: 60 * time.Second,
Destination: &balancer.LoadBalancerConfiguration.RequestBufferTimeout,
},
&cli.BoolFlag{
Name: "rewrite-host-header",
Value: false,
Destination: &balancer.LoadBalancerConfiguration.RewriteHostHeader,
},
},
},
// {
Expand Down

0 comments on commit ded12e4

Please sign in to comment.