Browse Source

chore: update nginx config for demo

0xJacky 1 year ago
parent
commit
384c2ae8aa
2 changed files with 61 additions and 53 deletions
  1. 53 53
      internal/upstream/upstream.go
  2. 8 0
      resources/demo/ojbk.me

+ 53 - 53
internal/upstream/upstream.go

@@ -1,83 +1,83 @@
 package upstream
 
 import (
-    "net"
-    "sync"
-    "time"
+	"net"
+	"sync"
+	"time"
 )
 
 const MaxTimeout = 5 * time.Second
 const MaxConcurrentWorker = 10
 
 type Status struct {
-    Online  bool    `json:"online"`
-    Latency float32 `json:"latency"`
+	Online  bool    `json:"online"`
+	Latency float32 `json:"latency"`
 }
 
 func AvailabilityTest(body []string) (result map[string]*Status) {
-    result = make(map[string]*Status)
-
-    wg := sync.WaitGroup{}
-    wg.Add(len(body))
-    c := make(chan struct{}, MaxConcurrentWorker)
-    for _, socket := range body {
-        c <- struct{}{}
-        s := &Status{}
-        go testLatency(c, &wg, socket, s)
-        result[socket] = s
-    }
-    wg.Wait()
-
-    return
+	result = make(map[string]*Status)
+
+	wg := sync.WaitGroup{}
+	wg.Add(len(body))
+	c := make(chan struct{}, MaxConcurrentWorker)
+	for _, socket := range body {
+		c <- struct{}{}
+		s := &Status{}
+		go testLatency(c, &wg, socket, s)
+		result[socket] = s
+	}
+	wg.Wait()
+
+	return
 }
 
 func testLatency(c chan struct{}, wg *sync.WaitGroup, socket string, status *Status) {
-    defer func() {
-        wg.Done()
-        <-c
-    }()
-
-    scopedWg := sync.WaitGroup{}
-    scopedWg.Add(2)
-    go testTCPLatency(&scopedWg, socket, status)
-    go testUnixSocketLatency(&scopedWg, socket, status)
-    scopedWg.Wait()
+	defer func() {
+		wg.Done()
+		<-c
+	}()
+
+	scopedWg := sync.WaitGroup{}
+	scopedWg.Add(2)
+	go testTCPLatency(&scopedWg, socket, status)
+	go testUnixSocketLatency(&scopedWg, socket, status)
+	scopedWg.Wait()
 }
 
 func testTCPLatency(wg *sync.WaitGroup, socket string, status *Status) {
-    defer func() {
-        wg.Done()
-    }()
-    start := time.Now()
-    conn, err := net.DialTimeout("tcp", socket, MaxTimeout)
+	defer func() {
+		wg.Done()
+	}()
+	start := time.Now()
+	conn, err := net.DialTimeout("tcp", socket, MaxTimeout)
 
-    if err != nil {
-        return
-    }
+	if err != nil {
+		return
+	}
 
-    defer conn.Close()
+	defer conn.Close()
 
-    end := time.Now()
+	end := time.Now()
 
-    status.Online = true
-    status.Latency = float32(end.Sub(start)) / float32(time.Millisecond)
+	status.Online = true
+	status.Latency = float32(end.Sub(start)) / float32(time.Millisecond)
 }
 
 func testUnixSocketLatency(wg *sync.WaitGroup, socket string, status *Status) {
-    defer func() {
-        wg.Done()
-    }()
-    start := time.Now()
-    conn, err := net.DialTimeout("unix", socket, MaxTimeout)
+	defer func() {
+		wg.Done()
+	}()
+	start := time.Now()
+	conn, err := net.DialTimeout("unix", socket, MaxTimeout)
 
-    if err != nil {
-        return
-    }
+	if err != nil {
+		return
+	}
 
-    defer conn.Close()
+	defer conn.Close()
 
-    end := time.Now()
+	end := time.Now()
 
-    status.Online = true
-    status.Latency = float32(end.Sub(start)) / float32(time.Millisecond)
+	status.Online = true
+	status.Latency = float32(end.Sub(start)) / float32(time.Millisecond)
 }

+ 8 - 0
resources/demo/ojbk.me

@@ -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 {
 	listen 80;
 	listen [::]:80;