forked from loxilb-io/loxilib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserviceprobe.go
46 lines (38 loc) · 964 Bytes
/
serviceprobe.go
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
45
46
// SPDX-License-Identifier: Apache 2.0
// Copyright (c) 2022 NetLOX Inc
package loxilib
import (
"net"
"net/http"
"time"
)
// HTTPProber - Do a http probe for given url
// returns true/false depending on whether probing was successful
func HTTPProber(urls string) bool {
timeout := time.Duration(2 * time.Second)
client := http.Client{Timeout: timeout}
r, e := client.Head(urls)
return e == nil && r.StatusCode == 200
}
// L4ServiceProber - Do a probe for L4 service end-points
// sType is "tcp" or "udp"
// sName is end-point IP address in string format
// returns true/false depending on whether probing was successful
func L4ServiceProber(sType string, sName string) bool {
sOk := false
timeout := 1 * time.Second
if sType != "tcp" && sType != "udp" {
// Unsupported
return true
}
c, err := net.DialTimeout(sType, sName, timeout)
if err != nil {
sOk = false
} else {
sOk = true
}
if c != nil {
defer c.Close()
}
return sOk
}