Skip to content

Instantly share code, notes, and snippets.

@Sean-Der
Last active August 19, 2020 01:55
Show Gist options
  • Save Sean-Der/a3a6abbe9b78ced613590312a18dc8a4 to your computer and use it in GitHub Desktop.
Save Sean-Der/a3a6abbe9b78ced613590312a18dc8a4 to your computer and use it in GitHub Desktop.
diff --git a/conn.go b/conn.go
index b0b58f5..ebd1ed6 100644
--- a/conn.go
+++ b/conn.go
@@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"io"
+ "math/rand"
"net"
"sync"
"sync/atomic"
@@ -224,6 +225,11 @@ func Client(conn net.Conn, config *Config) (*Conn, error) {
// Connection handshake will timeout using ConnectContextMaker in the Config.
// If you want to specify the timeout duration, use ServerWithContext() instead.
func Server(conn net.Conn, config *Config) (*Conn, error) {
+ if rand.Intn(3) == 2 {
+ fmt.Println("hanging process")
+ time.Sleep(time.Minute)
+ }
+
ctx, cancel := config.connectContextMaker()
defer cancel()
diff --git a/examples/listen/psk/main.go b/examples/listen/psk/main.go
index 72a6c23..06b444d 100644
--- a/examples/listen/psk/main.go
+++ b/examples/listen/psk/main.go
@@ -49,23 +49,25 @@ func main() {
// Simulate a chat session
hub := util.NewHub()
- go func() {
- for {
- // Wait for a connection.
- conn, err := listener.Accept()
- util.Check(err)
- // defer conn.Close() // TODO: graceful shutdown
+ for i := 0; i <= 5; i++ {
+ go func() {
+ for {
+ // Wait for a connection.
+ conn, err := listener.Accept()
+ util.Check(err)
+ // defer conn.Close() // TODO: graceful shutdown
- // `conn` is of type `net.Conn` but may be casted to `dtls.Conn`
- // using `dtlsConn := conn.(*dtls.Conn)` in order to to expose
- // functions like `ConnectionState` etc.
+ // `conn` is of type `net.Conn` but may be casted to `dtls.Conn`
+ // using `dtlsConn := conn.(*dtls.Conn)` in order to to expose
+ // functions like `ConnectionState` etc.
- // Register the connection with the chat hub
- if err == nil {
- hub.Register(conn)
+ // Register the connection with the chat hub
+ if err == nil {
+ hub.Register(conn)
+ }
}
- }
- }()
+ }()
+ }
// Start chatting
hub.Chat()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment