Forráskód Böngészése

完成tcp性能优化

robert 1 hete
szülő
commit
f5673fe0f2
1 módosított fájl, 8 hozzáadás és 1 törlés
  1. 8 1
      example/tcp/main.go

+ 8 - 1
example/tcp/main.go

@@ -52,12 +52,19 @@ func main() {
 	}
 
 	for i := 0; i < *concurrency; i++ {
+		// Stagger connection attempts to avoid overwhelming the server's accept backlog
+		// which can cause timeouts with high concurrency (e.g. > 128 connections)
+		if i%10 == 0 {
+			time.Sleep(2 * time.Millisecond)
+		}
+
 		wg.Add(1)
 		go func(id int) {
 			defer wg.Done()
 
 			// Connect
-			conn, err := net.DialTimeout("tcp", *addr, 5*time.Second)
+			// Increased timeout to handle backlog delays
+			conn, err := net.DialTimeout("tcp", *addr, 10*time.Second)
 			if err != nil {
 				log.Printf("Worker %d failed to connect: %v", id, err)
 				atomic.AddInt64(&failCount, int64(reqsPerConn*2)) // *2 because SET+GET