Skip to content

Commit

Permalink
block-waiting for a connection after the client has been started
Browse files Browse the repository at this point in the history
  • Loading branch information
xtaci committed Sep 19, 2016
1 parent 0788aa8 commit 09aea30
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,17 @@ func main() {
return session, nil
}

// wait until a connection is ready
waitConn := func() *smux.Session {
for {
if session, err := createConn(); err == nil {
return session
} else {
time.Sleep(time.Second)
}
}
}

numconn := uint16(config.Conn)
muxes := make([]struct {
session *smux.Session
Expand Down Expand Up @@ -367,22 +378,18 @@ func main() {
OPEN_P2:
// do auto expiration
if config.AutoExpire > 0 && time.Now().After(muxes[idx].ttl) {
if sess, err := createConn(); err == nil {
chScavenger <- muxes[idx].session
muxes[idx].session = sess
muxes[idx].ttl = time.Now().Add(time.Duration(config.AutoExpire) * time.Second)
}
chScavenger <- muxes[idx].session
muxes[idx].session = waitConn()
muxes[idx].ttl = time.Now().Add(time.Duration(config.AutoExpire) * time.Second)
}

// do session open
p2, err := muxes[idx].session.OpenStream()
if err != nil { // mux failure
if sess, err := createConn(); err == nil {
chScavenger <- muxes[idx].session
muxes[idx].session = sess
muxes[idx].ttl = time.Now().Add(time.Duration(config.AutoExpire) * time.Second)
goto OPEN_P2
}
chScavenger <- muxes[idx].session
muxes[idx].session = waitConn()
muxes[idx].ttl = time.Now().Add(time.Duration(config.AutoExpire) * time.Second)
goto OPEN_P2
}
go handleClient(p1, p2)
rr++
Expand Down

0 comments on commit 09aea30

Please sign in to comment.