-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f798766
commit 622c7a7
Showing
16 changed files
with
319 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package agent | ||
|
||
import "time" | ||
|
||
type AgentConfiguration struct { | ||
ReportingIntervalMiliseconds uint | ||
} | ||
|
||
func (self *AgentConfiguration) GetReportingIntervalDuration() time.Duration { | ||
return time.Duration(self.ReportingIntervalMiliseconds) * time.Millisecond | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,46 @@ | ||
package agent | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/distantmagic/paddler/goroutine" | ||
"github.com/distantmagic/paddler/llamacpp" | ||
"github.com/distantmagic/paddler/management" | ||
"github.com/hashicorp/go-hclog" | ||
) | ||
|
||
type LlamaCppObserver struct { | ||
AgentConfiguration *AgentConfiguration | ||
LlamaCppClient *llamacpp.LlamaCppClient | ||
Logger hclog.Logger | ||
ManagementClient *management.Client | ||
} | ||
|
||
func (self *LlamaCppObserver) ObserveAndReport( | ||
serverEventsChannel chan goroutine.ResultMessage, | ||
) { | ||
llamaCppHealthStatusChannel := make(chan llamacpp.LlamaCppHealthStatus) | ||
|
||
defer close(llamaCppHealthStatusChannel) | ||
|
||
ticker := time.NewTicker(self.AgentConfiguration.GetReportingIntervalDuration()) | ||
|
||
go self.RunTickerInterval(llamaCppHealthStatusChannel, ticker) | ||
|
||
for llamaCppHealthStatus := range llamaCppHealthStatusChannel { | ||
go self.ManagementClient.ReportLlamaCppHealthStatus( | ||
serverEventsChannel, | ||
self.LlamaCppClient.LlamaCppConfiguration, | ||
&llamaCppHealthStatus, | ||
) | ||
} | ||
} | ||
|
||
func (self *LlamaCppObserver) RunTickerInterval( | ||
llamaCppHealthStatusChannel chan llamacpp.LlamaCppHealthStatus, | ||
ticker *time.Ticker, | ||
) { | ||
for range ticker.C { | ||
go self.LlamaCppClient.GetHealth(llamaCppHealthStatusChannel) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,49 @@ | ||
package cmd | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/distantmagic/paddler/agent" | ||
"github.com/distantmagic/paddler/goroutine" | ||
"github.com/distantmagic/paddler/llamacpp" | ||
"github.com/distantmagic/paddler/management" | ||
"github.com/distantmagic/paddler/reverseproxy" | ||
"github.com/hashicorp/go-hclog" | ||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
type Agent struct { | ||
Logger hclog.Logger | ||
LlamaCppConfiguration *llamacpp.LlamaCppConfiguration | ||
ReverseProxyConfiguration *reverseproxy.ReverseProxyConfiguration | ||
AgentConfiguration *agent.AgentConfiguration | ||
Logger hclog.Logger | ||
LlamaCppConfiguration *llamacpp.LlamaCppConfiguration | ||
ManagementServerConfiguration *management.ManagementServerConfiguration | ||
ReverseProxyConfiguration *reverseproxy.ReverseProxyConfiguration | ||
} | ||
|
||
func (self *Agent) Action(cliContext *cli.Context) error { | ||
serverEventsChannel := make(chan goroutine.ResultMessage) | ||
|
||
llamaCppObserver := &agent.LlamaCppObserver{ | ||
AgentConfiguration: self.AgentConfiguration, | ||
LlamaCppClient: &llamacpp.LlamaCppClient{ | ||
HttpClient: http.DefaultClient, | ||
LlamaCppConfiguration: self.LlamaCppConfiguration, | ||
}, | ||
Logger: self.Logger.Named("LlamaCppObserver"), | ||
ManagementClient: &management.Client{ | ||
HttpClient: http.DefaultClient, | ||
ManagementServerConfiguration: self.ManagementServerConfiguration, | ||
}, | ||
} | ||
|
||
go llamaCppObserver.ObserveAndReport(serverEventsChannel) | ||
|
||
for serverEvent := range serverEventsChannel { | ||
self.Logger.Info( | ||
"server event", | ||
"event", serverEvent, | ||
) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
package loadbalancer | ||
|
||
type LoadBalancerStatus struct { | ||
RegisteredTargets int | ||
RegisteredTargets int `json:"registred_targets"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package loadbalancer | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httputil" | ||
|
||
"github.com/distantmagic/paddler/goroutine" | ||
"github.com/distantmagic/paddler/reverseproxy" | ||
"github.com/hashicorp/go-hclog" | ||
) | ||
|
||
type ReverseProxyServer struct { | ||
LoadBalancer *LoadBalancer | ||
Logger hclog.Logger | ||
ReverseProxyConfiguration *reverseproxy.ReverseProxyConfiguration | ||
} | ||
|
||
func (self *ReverseProxyServer) Serve(serverEventsChannel chan goroutine.ResultMessage) { | ||
self.Logger.Debug( | ||
"listen", | ||
"host", self.ReverseProxyConfiguration.HttpAddress.GetHostWithPort(), | ||
) | ||
|
||
reverseProxy := &httputil.ReverseProxy{ | ||
ErrorLog: self.Logger.Named("ReverseProxy").StandardLogger(&hclog.StandardLoggerOptions{ | ||
InferLevels: true, | ||
}), | ||
Rewrite: func(request *httputil.ProxyRequest) { | ||
targetUrl, err := self.LoadBalancer.Balance(request.In) | ||
|
||
if err != nil { | ||
serverEventsChannel <- goroutine.ResultMessage{ | ||
Comment: "failed to balance request", | ||
Error: err, | ||
} | ||
|
||
return | ||
} | ||
|
||
request.SetURL(targetUrl) | ||
request.SetXForwarded() | ||
}, | ||
} | ||
|
||
err := http.ListenAndServe( | ||
self.ReverseProxyConfiguration.HttpAddress.GetHostWithPort(), | ||
reverseProxy, | ||
) | ||
|
||
if err != nil { | ||
serverEventsChannel <- goroutine.ResultMessage{ | ||
Comment: "failed to listen", | ||
Error: err, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.