Skip to content

Commit

Permalink
add wait after invoke goroutine pool, fix #391
Browse files Browse the repository at this point in the history
  • Loading branch information
ffdfgdfg committed Feb 8, 2020
1 parent 72c695d commit 632bd12
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
6 changes: 5 additions & 1 deletion lib/conn/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"net/url"
"strconv"
"strings"
"sync"
"time"

"ehang.io/nps/lib/common"
Expand Down Expand Up @@ -371,7 +372,10 @@ func CopyWaitGroup(conn1, conn2 net.Conn, crypt bool, snappy bool, rate *rate.Ra
//if flow != nil {
// flow.Add(in, out)
//}
err := goroutine.CopyConnsPool.Invoke(goroutine.NewConns(connHandle, conn2, flow))
wg := new(sync.WaitGroup)
wg.Add(1)
err := goroutine.CopyConnsPool.Invoke(goroutine.NewConns(connHandle, conn2, flow, wg))
wg.Wait()
if err != nil {
logs.Error(err)
}
Expand Down
5 changes: 4 additions & 1 deletion lib/goroutine/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,15 @@ type Conns struct {
conn1 io.ReadWriteCloser // mux connection
conn2 net.Conn // outside connection
flow *file.Flow
wg *sync.WaitGroup
}

func NewConns(c1 io.ReadWriteCloser, c2 net.Conn, flow *file.Flow) Conns {
func NewConns(c1 io.ReadWriteCloser, c2 net.Conn, flow *file.Flow, wg *sync.WaitGroup) Conns {
return Conns{
conn1: c1,
conn2: c2,
flow: flow,
wg: wg,
}
}

Expand All @@ -67,6 +69,7 @@ func copyConns(group interface{}) {
if conns.flow != nil {
conns.flow.Add(in, out)
}
conns.wg.Done()
}

var connCopyPool, _ = ants.NewPoolWithFunc(200000, copyConnGroup, ants.WithNonblocking(false))
Expand Down

0 comments on commit 632bd12

Please sign in to comment.