Skip to content

Commit

Permalink
Adjust backend monitor
Browse files Browse the repository at this point in the history
  • Loading branch information
dalibormesaric committed Jul 14, 2024
1 parent b979649 commit f498fed
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 24 deletions.
14 changes: 10 additions & 4 deletions internal/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type Backends map[string][]*Backend

type Backend struct {
Name string
Url string
URL *url.URL
Proxy *httputil.ReverseProxy
Live bool
Monitor []MonitorFrame
Expand Down Expand Up @@ -51,12 +51,15 @@ func CreateBackends(nameUrlPairs string) (Backends, error) {
k := split[i-1]
_, ok := backends[k]
backendUrl := split[i]
b, _ := createBackend(k, backendUrl)
b, err := createBackend(k, backendUrl)
if err != nil {
return nil, err
}
if !ok {
backends[k] = []*Backend{b}
} else {
for _, existingBackend := range backends[k] {
if existingBackend.Url == backendUrl {
if existingBackend.URL.Host == b.URL.Host {
return nil, fmt.Errorf("url (%s) already exist in backend (%s)", backendUrl, k)
}
}
Expand All @@ -74,12 +77,15 @@ func createBackend(key, urlString string) (*Backend, error) {
if err != nil {
return nil, err
}
if urlParsed.Host == "" {
return nil, fmt.Errorf("empty host for url (%s) in backend (%s)", urlString, key)
}

proxy := httputil.NewSingleHostReverseProxy(urlParsed)

return &Backend{
string(strip([]byte(fmt.Sprintf("%s%s", key, urlString)))),
urlString,
urlParsed,
proxy,
false,
[]MonitorFrame{},
Expand Down
28 changes: 15 additions & 13 deletions internal/backend/backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@ func TestCreateBackends(t *testing.T) {
var tests = []struct {
nameUrlPairs string
key string
values []Backend
host string
}{
{"a,b", "a", []Backend{{Url: "b"}}},
{"a,http://b:1234", "a", "b:1234"},
}

for _, test := range tests {
result, _ := CreateBackends(test.nameUrlPairs)
for i, v := range result[test.key] {
if v.Url != test.values[i].Url {
t.Errorf("wrong backend url: want (%s) got (%s)\n", test.values[i].Url, v.Url)
}
be, err := CreateBackends(test.nameUrlPairs)
if err != nil {
t.Error(err)
}
b := be[test.key][0]
if b.URL.Host != test.host {
t.Errorf("wrong backend url: want (%s) got (%s)\n", test.host, b.URL.Host)
}
}
}
Expand All @@ -34,17 +36,17 @@ func TestCreateBackendsErrors(t *testing.T) {
{"a,b,c", fmt.Errorf("backends must be a comma-separated list containing even number of items")},
{",", fmt.Errorf("nameUrlPair at index 0 must have a value")},
{"a,", fmt.Errorf("nameUrlPair at index 1 must have a value")},
{"a,b,a,b", fmt.Errorf("url (b) already exist in backend (a)")},
{"a,b", nil},
{"a,b,a,b", fmt.Errorf("empty host for url (b) in backend (a)")},
{"a,http://b:1234,a,http://b:1234", fmt.Errorf("url (http://b:1234) already exist in backend (a)")},
}

for _, test := range tests {
_, err := CreateBackends(test.nameUrlPairs)
if test.err != nil && err.Error() != test.err.Error() {
t.Errorf("Want: %s\nGot: %s\n", test.err.Error(), err.Error())
if err == nil {
t.Errorf("was expecting an error\n")
}
if test.err == nil && err != nil {
t.Errorf("Was not expecting an error\n")
if test.err != nil && err.Error() != test.err.Error() {
t.Errorf("was expecting an error: want (%s)\ngot: (%s)\n", test.err.Error(), err.Error())
}
}
}
2 changes: 1 addition & 1 deletion internal/backend/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (b Backends) Monitor() chan interface{} {

func (be *Backend) monitor(messages chan interface{}) {
for {
latency := healthCheck(be.Url)
latency := healthCheck(be.URL.Host)
be.Live = latency > 0

colorCode := getColorCode(latency)
Expand Down
4 changes: 2 additions & 2 deletions internal/backend/monitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ func TestHealthCheckBackend(t *testing.T) {
}
latency := healthCheck(url.Host)
if latency == 0 {
t.Errorf("latency should be greated then 0, got (%v)", latency)
t.Errorf("wrong latency: want (>0) got (%v)\n", latency)
}
}

func TestHealthCheckNoBackend(t *testing.T) {
latency := healthCheck("notexists.local:1234")
if latency > 0 {
t.Errorf("latency should be 0, got (%v)", latency)
t.Errorf("wrong latency: want (0) got (%v)\n", latency)
}
}

Expand Down
2 changes: 1 addition & 1 deletion internal/dashboard/templates/monitor.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<div>
{{range $value}}
<div class="monitor" name="{{.Name}}">
<div class="monitor-name">{{.Url}}</div>
<div class="monitor-name">{{.URL.Host}}</div>
<div class="monitor-latency">Latency: <span name="{{.Name}}latency">unavailable</span></div>
<div class="monitor-grid">
{{range .Monitor}}
Expand Down
2 changes: 1 addition & 1 deletion internal/dashboard/templates/traffic.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
{{range $bvalue}}
<li>
<span class="cir" name="{{.Name}}cir"></span>
<div class="traffic-backend-name">{{.Url}}</div>
<div class="traffic-backend-name">{{.URL.String}}</div>
<div class="traffic-counter">Hits: <span name="{{.Name}}hits">{{.Hits}}</span></div>
</li>
{{end}}
Expand Down
4 changes: 3 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ docker compose -f sample/compose.yaml up --build

docker compose -f sample/compose.yaml down

for i in {1..10}; do curl localhost:8080; sleep 1; done;
for i in {1..10}; do curl -s localhost:8080 | grep h1; sleep 1; done;

seq 1000 | parallel -n0 -j8 "curl -s http://localhost:8080 | grep h1"
```

## Misc
Expand Down
2 changes: 1 addition & 1 deletion sample/compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ services:
dockerfile: Dockerfile
environment:
- FE=localhost,sample
- BE=sample,http://be1,sample,http://be2,sample,http://be3
- BE=sample,http://host.docker.internal:8081,sample,http://host.docker.internal:8082,sample,http://host.docker.internal:8083
ports:
- "8080:8080"
- "8000:8000"

0 comments on commit f498fed

Please sign in to comment.