-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
44 lines (40 loc) · 1004 Bytes
/
server.go
File metadata and controls
44 lines (40 loc) · 1004 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package main
import (
"fmt"
"net/http"
"net/http/httputil"
"net/url"
"time"
)
type server struct {
Name string
URL string
ReverseProxy *httputil.ReverseProxy
Health bool
}
func newServer(name, urlStr string) *server {
urlParsed, err := url.Parse(urlStr)
if err != nil {
panic(fmt.Sprintf("failed to parse server URL '%s': %v", urlStr, err))
}
reverseProxy := httputil.NewSingleHostReverseProxy(urlParsed)
return &server{
Name: name,
URL: urlStr,
ReverseProxy: reverseProxy,
Health: true, // Assume server is healthy initially
}
}
func (s *server) checkHealth(timeout time.Duration) {
client := http.Client{
Timeout: timeout,
}
resp, err := client.Head(s.URL) // Simple HEAD request to check if the server is up
mutex.Lock() // Ensure thread-safe access to the Health attribute
defer mutex.Unlock()
if err != nil || resp.StatusCode != http.StatusOK {
s.Health = false
} else {
s.Health = true
}
}