|
1 | 1 | package upstream
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "net" |
5 |
| - "sync" |
6 |
| - "time" |
| 4 | + "net" |
| 5 | + "sync" |
| 6 | + "time" |
7 | 7 | )
|
8 | 8 |
|
9 | 9 | const MaxTimeout = 5 * time.Second
|
10 | 10 | const MaxConcurrentWorker = 10
|
11 | 11 |
|
12 | 12 | type Status struct {
|
13 |
| - Online bool `json:"online"` |
14 |
| - Latency float32 `json:"latency"` |
| 13 | + Online bool `json:"online"` |
| 14 | + Latency float32 `json:"latency"` |
15 | 15 | }
|
16 | 16 |
|
17 | 17 | func AvailabilityTest(body []string) (result map[string]*Status) {
|
18 |
| - result = make(map[string]*Status) |
19 |
| - |
20 |
| - wg := sync.WaitGroup{} |
21 |
| - wg.Add(len(body)) |
22 |
| - c := make(chan struct{}, MaxConcurrentWorker) |
23 |
| - for _, socket := range body { |
24 |
| - c <- struct{}{} |
25 |
| - s := &Status{} |
26 |
| - go testLatency(c, &wg, socket, s) |
27 |
| - result[socket] = s |
28 |
| - } |
29 |
| - wg.Wait() |
30 |
| - |
31 |
| - return |
| 18 | + result = make(map[string]*Status) |
| 19 | + |
| 20 | + wg := sync.WaitGroup{} |
| 21 | + wg.Add(len(body)) |
| 22 | + c := make(chan struct{}, MaxConcurrentWorker) |
| 23 | + for _, socket := range body { |
| 24 | + c <- struct{}{} |
| 25 | + s := &Status{} |
| 26 | + go testLatency(c, &wg, socket, s) |
| 27 | + result[socket] = s |
| 28 | + } |
| 29 | + wg.Wait() |
| 30 | + |
| 31 | + return |
32 | 32 | }
|
33 | 33 |
|
34 | 34 | func testLatency(c chan struct{}, wg *sync.WaitGroup, socket string, status *Status) {
|
35 |
| - defer func() { |
36 |
| - wg.Done() |
37 |
| - <-c |
38 |
| - }() |
39 |
| - |
40 |
| - scopedWg := sync.WaitGroup{} |
41 |
| - scopedWg.Add(2) |
42 |
| - go testTCPLatency(&scopedWg, socket, status) |
43 |
| - go testUnixSocketLatency(&scopedWg, socket, status) |
44 |
| - scopedWg.Wait() |
| 35 | + defer func() { |
| 36 | + wg.Done() |
| 37 | + <-c |
| 38 | + }() |
| 39 | + |
| 40 | + scopedWg := sync.WaitGroup{} |
| 41 | + scopedWg.Add(2) |
| 42 | + go testTCPLatency(&scopedWg, socket, status) |
| 43 | + go testUnixSocketLatency(&scopedWg, socket, status) |
| 44 | + scopedWg.Wait() |
45 | 45 | }
|
46 | 46 |
|
47 | 47 | func testTCPLatency(wg *sync.WaitGroup, socket string, status *Status) {
|
48 |
| - defer func() { |
49 |
| - wg.Done() |
50 |
| - }() |
51 |
| - start := time.Now() |
52 |
| - conn, err := net.DialTimeout("tcp", socket, MaxTimeout) |
| 48 | + defer func() { |
| 49 | + wg.Done() |
| 50 | + }() |
| 51 | + start := time.Now() |
| 52 | + conn, err := net.DialTimeout("tcp", socket, MaxTimeout) |
53 | 53 |
|
54 |
| - if err != nil { |
55 |
| - return |
56 |
| - } |
| 54 | + if err != nil { |
| 55 | + return |
| 56 | + } |
57 | 57 |
|
58 |
| - defer conn.Close() |
| 58 | + defer conn.Close() |
59 | 59 |
|
60 |
| - end := time.Now() |
| 60 | + end := time.Now() |
61 | 61 |
|
62 |
| - status.Online = true |
63 |
| - status.Latency = float32(end.Sub(start)) / float32(time.Millisecond) |
| 62 | + status.Online = true |
| 63 | + status.Latency = float32(end.Sub(start)) / float32(time.Millisecond) |
64 | 64 | }
|
65 | 65 |
|
66 | 66 | func testUnixSocketLatency(wg *sync.WaitGroup, socket string, status *Status) {
|
67 |
| - defer func() { |
68 |
| - wg.Done() |
69 |
| - }() |
70 |
| - start := time.Now() |
71 |
| - conn, err := net.DialTimeout("unix", socket, MaxTimeout) |
| 67 | + defer func() { |
| 68 | + wg.Done() |
| 69 | + }() |
| 70 | + start := time.Now() |
| 71 | + conn, err := net.DialTimeout("unix", socket, MaxTimeout) |
72 | 72 |
|
73 |
| - if err != nil { |
74 |
| - return |
75 |
| - } |
| 73 | + if err != nil { |
| 74 | + return |
| 75 | + } |
76 | 76 |
|
77 |
| - defer conn.Close() |
| 77 | + defer conn.Close() |
78 | 78 |
|
79 |
| - end := time.Now() |
| 79 | + end := time.Now() |
80 | 80 |
|
81 |
| - status.Online = true |
82 |
| - status.Latency = float32(end.Sub(start)) / float32(time.Millisecond) |
| 81 | + status.Online = true |
| 82 | + status.Latency = float32(end.Sub(start)) / float32(time.Millisecond) |
83 | 83 | }
|
0 commit comments