Skip to content

Instantly share code, notes, and snippets.

@twexler
Last active October 20, 2016 02:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save twexler/ef68ccfcb609cd95ec223573ea692a0f to your computer and use it in GitHub Desktop.
Save twexler/ef68ccfcb609cd95ec223573ea692a0f to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"log"
"net"
"time"
)
func main() {
n := 0
t := time.NewTicker(time.Second)
c, err := net.Dial("tcp", "localhost:8888")
if err != nil {
log.Fatal(err)
}
for {
select {
case <-t.C:
fmt.Println(n, "req/sec")
n = 0
default:
fmt.Fprintln(c, "10")
n = n + 1
}
}
}
package main
import (
"bufio"
"fmt"
"log"
"net"
"strconv"
)
func main() {
ln, err := net.Listen("tcp", ":8888")
if err != nil {
log.Fatal(err)
}
for {
c, err := ln.Accept()
if err == nil {
go serve(c)
}
}
}
func fib(n int) int {
if n <= 2 {
return 1
} else {
return fib(n-1) + fib(n-2)
}
}
func serve(c net.Conn) {
bs := bufio.NewScanner(c)
for bs.Scan() {
n, _ := strconv.Atoi(bs.Text())
go func() {
res := fib(n)
fmt.Fprintf(c, "%d: %s\n", n, strconv.Itoa(res))
}()
}
if bs.Err() != nil {
log.Println("error")
}
}
panic: net: inconsistent fdMutex
goroutine 1145194 [running]:
panic(0x126cc0, 0xc94cf02860)
/usr/local/Cellar/go/1.6/libexec/src/runtime/panic.go:464 +0x3e6
net.(*fdMutex).RWLock(0xc8200620e0, 0x0, 0xc94d37f61e)
/usr/local/Cellar/go/1.6/libexec/src/net/fd_mutex.go:136 +0x20c
net.(*netFD).writeLock(0xc8200620e0, 0x0, 0x0)
/usr/local/Cellar/go/1.6/libexec/src/net/fd_unix.go:194 +0x39
net.(*netFD).Write(0xc8200620e0, 0xc94cf02848, 0x7, 0x8, 0x0, 0x0, 0x0)
/usr/local/Cellar/go/1.6/libexec/src/net/fd_unix.go:319 +0x6c
net.(*conn).Write(0xc820088020, 0xc94cf02848, 0x7, 0x8, 0x0, 0x0, 0x0)
/usr/local/Cellar/go/1.6/libexec/src/net/net.go:184 +0xe4
fmt.Fprintf(0x440028, 0xc820088020, 0x18bf18, 0x7, 0xc94d9e0f70, 0x2, 0x2, 0x0, 0x0, 0x0)
/usr/local/Cellar/go/1.6/libexec/src/fmt/print.go:189 +0xb2
main.serve.func1(0xa, 0x489440, 0xc820088020)
/Users/twexler/dev/tmp/fib.go:38 +0x1d1
created by main.serve
/Users/twexler/dev/tmp/fib.go:39 +0x18d
exit status 2
@twexler
Copy link
Author

twexler commented Jul 9, 2016

Interestingly enough, the script that broke it continued sending data to a socket that wasn't being listened on anymore:

⏚ [twexler:~/dev/tmp] 1 $ go run breakit.go
46110 req/sec
38331 req/sec
42625 req/sec
52618 req/sec
62030 req/sec
58850 req/sec
59278 req/sec
90486 req/sec
39302 req/sec
59480 req/sec
53790 req/sec
61662 req/sec
43035 req/sec
61134 req/sec
58688 req/sec
82457 req/sec
33726 req/sec
42164 req/sec
44805 req/sec
42312 req/sec
41992 req/sec
38499 req/sec
56298 req/sec
132634 req/sec
144863 req/sec
150092 req/sec
130998 req/sec
165556 req/sec

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment