Last active
May 22, 2020 06:56
-
-
Save karupanerura/d553b66efddc812b53d1828e71d40e88 to your computer and use it in GitHub Desktop.
TCP stuck example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"io" | |
"log" | |
"net" | |
"sync" | |
"time" | |
) | |
func main() { | |
conn, err := net.Dial("tcp4", "localhost:8888") | |
if err != nil { | |
log.Fatal(err) | |
} | |
var wg sync.WaitGroup | |
go func() { | |
wg.Add(1) | |
defer wg.Done() | |
scope := time.Now().Truncate(time.Second) | |
var avg time.Duration | |
var cnt time.Duration | |
for { | |
startAt := time.Now() | |
startScope := startAt.Truncate(time.Second) | |
_, err := io.WriteString(conn, "Hello!\n") | |
if err != nil { | |
log.Printf("%v (startAt:%v)\n", err, startAt) | |
return | |
} | |
elapsed := time.Now().Sub(startAt) | |
if scope.Equal(startScope) { | |
avg = (cnt*avg + elapsed) / (cnt + 1) | |
cnt++ | |
} else { | |
log.Printf("- %v (%d)\n", avg, cnt) | |
scope = startScope | |
avg = elapsed | |
cnt = 1 | |
} | |
time.Sleep(300 * time.Microsecond) | |
} | |
}() | |
func() { | |
wg.Add(1) | |
defer wg.Done() | |
bufr := bufio.NewReader(conn) | |
for { | |
line, err := bufr.ReadString('\n') | |
if err != nil { | |
log.Println(err) | |
return | |
} | |
log.Printf("received: %s", line) | |
} | |
}() | |
wg.Wait() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2020/05/20 18:14:05 - 21.816µs (2186) | |
2020/05/20 18:14:06 - 21.257µs (2472) | |
2020/05/20 18:14:07 - 21.423µs (2472) | |
2020/05/20 18:14:07 received: Hello! | |
2020/05/20 18:14:08 - 21.736µs (2474) | |
2020/05/20 18:14:09 - 21.771µs (2476) | |
2020/05/20 18:14:10 - 21.649µs (2473) | |
2020/05/20 18:14:10 received: Hello! | |
2020/05/20 18:14:11 - 21.389µs (2448) | |
2020/05/20 18:14:12 - 21.682µs (2473) | |
2020/05/20 18:14:13 - 21.505µs (2482) | |
2020/05/20 18:14:13 received: Hello! | |
2020/05/20 18:14:14 - 21.673µs (2473) | |
2020/05/20 18:14:15 - 21.43µs (2481) | |
2020/05/20 18:14:16 - 21.533µs (2461) | |
2020/05/20 18:14:16 received: Hello! | |
2020/05/20 18:14:17 - 21.527µs (2469) | |
2020/05/20 18:14:18 - 22.538µs (2457) | |
2020/05/20 18:14:19 - 21.3µs (2477) | |
2020/05/20 18:14:19 received: Hello! | |
2020/05/20 18:14:20 - 22.237µs (2468) | |
2020/05/20 18:14:21 - 22.061µs (2469) | |
2020/05/20 18:14:22 - 22.01µs (2463) | |
2020/05/20 18:14:22 received: Hello! | |
2020/05/20 18:14:23 - 21.711µs (2483) | |
2020/05/20 18:14:24 - 22.5µs (2469) | |
2020/05/20 18:14:25 - 21.953µs (2467) | |
2020/05/20 18:14:25 received: Hello! | |
2020/05/20 18:14:26 - 22.39µs (2457) | |
2020/05/20 18:14:27 - 22.322µs (2468) | |
2020/05/20 18:14:28 - 20.327µs (2470) | |
2020/05/20 18:14:28 received: Hello! | |
2020/05/20 18:14:29 - 12.092µs (2524) | |
2020/05/20 18:14:30 - 12.423µs (2515) | |
2020/05/20 18:14:31 - 12.026µs (2520) | |
2020/05/20 18:14:31 received: Hello! | |
2020/05/20 18:14:32 - 12.367µs (2519) | |
2020/05/20 18:14:33 - 12.221µs (2581) | |
2020/05/20 18:14:34 - 12.579µs (2537) | |
2020/05/20 18:14:34 received: Hello! | |
2020/05/20 18:14:35 - 12.464µs (2521) | |
2020/05/20 18:14:36 - 12.734µs (2521) | |
2020/05/20 18:14:37 received: Hello! | |
2020/05/20 18:14:40 received: Hello! | |
2020/05/20 18:14:43 received: Hello! | |
2020/05/20 18:14:46 received: Hello! | |
2020/05/20 18:14:49 received: Hello! | |
2020/05/20 18:14:52 received: Hello! | |
2020/05/20 18:14:55 received: Hello! | |
2020/05/20 18:14:57 read tcp4 127.0.0.1:59345->127.0.0.1:8888: read: connection reset by peer | |
2020/05/20 18:14:57 write tcp4 127.0.0.1:59345->127.0.0.1:8888: write: broken pipe (startAt:2020-05-20 18:14:36.058589 +0900 JST m=+31.945740248) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"io" | |
"log" | |
"net" | |
"time" | |
) | |
func main() { | |
l, err := net.Listen("tcp4", "localhost:8888") | |
if err != nil { | |
log.Fatal(err) | |
} | |
for { | |
c, err := l.Accept() | |
if err != nil { | |
log.Println(err) | |
return | |
} | |
go func(c net.Conn) { | |
bufr := bufio.NewReader(c) | |
for { | |
line, err := bufr.ReadString('\n') | |
if err != nil { | |
log.Println(err) | |
break | |
} | |
time.Sleep(3 * time.Second) # emulate to delay receiving/processing the data | |
_, err = io.WriteString(c, line) | |
if err != nil { | |
log.Println(err) | |
break | |
} | |
} | |
err := c.Close() | |
if err != nil { | |
log.Println(err) | |
} | |
}(c) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment