Skip to content

Commit 384c2ae

Browse files
committed
chore: update nginx config for demo
1 parent d9d8bee commit 384c2ae

File tree

2 files changed

+61
-53
lines changed

2 files changed

+61
-53
lines changed

internal/upstream/upstream.go

Lines changed: 53 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,83 @@
11
package upstream
22

33
import (
4-
"net"
5-
"sync"
6-
"time"
4+
"net"
5+
"sync"
6+
"time"
77
)
88

99
const MaxTimeout = 5 * time.Second
1010
const MaxConcurrentWorker = 10
1111

1212
type Status struct {
13-
Online bool `json:"online"`
14-
Latency float32 `json:"latency"`
13+
Online bool `json:"online"`
14+
Latency float32 `json:"latency"`
1515
}
1616

1717
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
3232
}
3333

3434
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()
4545
}
4646

4747
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)
5353

54-
if err != nil {
55-
return
56-
}
54+
if err != nil {
55+
return
56+
}
5757

58-
defer conn.Close()
58+
defer conn.Close()
5959

60-
end := time.Now()
60+
end := time.Now()
6161

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)
6464
}
6565

6666
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)
7272

73-
if err != nil {
74-
return
75-
}
73+
if err != nil {
74+
return
75+
}
7676

77-
defer conn.Close()
77+
defer conn.Close()
7878

79-
end := time.Now()
79+
end := time.Now()
8080

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)
8383
}

resources/demo/ojbk.me

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
upstream api-1 {
2+
server 127.0.0.1:9000;
3+
server 127.0.0.1:443;
4+
}
5+
upstream api-2 {
6+
server 127.0.0.1:9003;
7+
server 127.0.0.1:9005;
8+
}
19
server {
210
listen 80;
311
listen [::]:80;

0 commit comments

Comments
 (0)