mirror of
https://github.com/0xJacky/nginx-ui.git
synced 2025-05-11 02:15:48 +02:00
chore: update nginx config for demo
This commit is contained in:
parent
d9d8bee5d0
commit
384c2ae8aa
2 changed files with 58 additions and 50 deletions
|
@ -1,83 +1,83 @@
|
||||||
package upstream
|
package upstream
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net"
|
"net"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
const MaxTimeout = 5 * time.Second
|
const MaxTimeout = 5 * time.Second
|
||||||
const MaxConcurrentWorker = 10
|
const MaxConcurrentWorker = 10
|
||||||
|
|
||||||
type Status struct {
|
type Status struct {
|
||||||
Online bool `json:"online"`
|
Online bool `json:"online"`
|
||||||
Latency float32 `json:"latency"`
|
Latency float32 `json:"latency"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func AvailabilityTest(body []string) (result map[string]*Status) {
|
func AvailabilityTest(body []string) (result map[string]*Status) {
|
||||||
result = make(map[string]*Status)
|
result = make(map[string]*Status)
|
||||||
|
|
||||||
wg := sync.WaitGroup{}
|
wg := sync.WaitGroup{}
|
||||||
wg.Add(len(body))
|
wg.Add(len(body))
|
||||||
c := make(chan struct{}, MaxConcurrentWorker)
|
c := make(chan struct{}, MaxConcurrentWorker)
|
||||||
for _, socket := range body {
|
for _, socket := range body {
|
||||||
c <- struct{}{}
|
c <- struct{}{}
|
||||||
s := &Status{}
|
s := &Status{}
|
||||||
go testLatency(c, &wg, socket, s)
|
go testLatency(c, &wg, socket, s)
|
||||||
result[socket] = s
|
result[socket] = s
|
||||||
}
|
}
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func testLatency(c chan struct{}, wg *sync.WaitGroup, socket string, status *Status) {
|
func testLatency(c chan struct{}, wg *sync.WaitGroup, socket string, status *Status) {
|
||||||
defer func() {
|
defer func() {
|
||||||
wg.Done()
|
wg.Done()
|
||||||
<-c
|
<-c
|
||||||
}()
|
}()
|
||||||
|
|
||||||
scopedWg := sync.WaitGroup{}
|
scopedWg := sync.WaitGroup{}
|
||||||
scopedWg.Add(2)
|
scopedWg.Add(2)
|
||||||
go testTCPLatency(&scopedWg, socket, status)
|
go testTCPLatency(&scopedWg, socket, status)
|
||||||
go testUnixSocketLatency(&scopedWg, socket, status)
|
go testUnixSocketLatency(&scopedWg, socket, status)
|
||||||
scopedWg.Wait()
|
scopedWg.Wait()
|
||||||
}
|
}
|
||||||
|
|
||||||
func testTCPLatency(wg *sync.WaitGroup, socket string, status *Status) {
|
func testTCPLatency(wg *sync.WaitGroup, socket string, status *Status) {
|
||||||
defer func() {
|
defer func() {
|
||||||
wg.Done()
|
wg.Done()
|
||||||
}()
|
}()
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
conn, err := net.DialTimeout("tcp", socket, MaxTimeout)
|
conn, err := net.DialTimeout("tcp", socket, MaxTimeout)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
|
|
||||||
end := time.Now()
|
end := time.Now()
|
||||||
|
|
||||||
status.Online = true
|
status.Online = true
|
||||||
status.Latency = float32(end.Sub(start)) / float32(time.Millisecond)
|
status.Latency = float32(end.Sub(start)) / float32(time.Millisecond)
|
||||||
}
|
}
|
||||||
|
|
||||||
func testUnixSocketLatency(wg *sync.WaitGroup, socket string, status *Status) {
|
func testUnixSocketLatency(wg *sync.WaitGroup, socket string, status *Status) {
|
||||||
defer func() {
|
defer func() {
|
||||||
wg.Done()
|
wg.Done()
|
||||||
}()
|
}()
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
conn, err := net.DialTimeout("unix", socket, MaxTimeout)
|
conn, err := net.DialTimeout("unix", socket, MaxTimeout)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
|
|
||||||
end := time.Now()
|
end := time.Now()
|
||||||
|
|
||||||
status.Online = true
|
status.Online = true
|
||||||
status.Latency = float32(end.Sub(start)) / float32(time.Millisecond)
|
status.Latency = float32(end.Sub(start)) / float32(time.Millisecond)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +1,11 @@
|
||||||
|
upstream api-1 {
|
||||||
|
server 127.0.0.1:9000;
|
||||||
|
server 127.0.0.1:443;
|
||||||
|
}
|
||||||
|
upstream api-2 {
|
||||||
|
server 127.0.0.1:9003;
|
||||||
|
server 127.0.0.1:9005;
|
||||||
|
}
|
||||||
server {
|
server {
|
||||||
listen 80;
|
listen 80;
|
||||||
listen [::]:80;
|
listen [::]:80;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue