Skip to content

Commit eef3eee

Browse files
ws: fix retry
1 parent 3bc8465 commit eef3eee

File tree

1 file changed

+21
-14
lines changed

1 file changed

+21
-14
lines changed

exec/ws/main.go

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,23 @@ import (
66
"encoding/binary"
77
"encoding/hex"
88
"fmt"
9+
"io"
10+
"log"
11+
"math/rand"
12+
"net/http"
13+
_ "net/http/pprof"
14+
"os"
15+
"time"
16+
917
"github.com/BurntSushi/toml"
1018
"github.com/gin-gonic/gin"
1119
"github.com/gin-gonic/gin/binding"
1220
"github.com/gorilla/websocket"
1321
json "github.com/json-iterator/go"
14-
"io/ioutil"
15-
"log"
16-
"math/rand"
17-
"net/http"
22+
1823
"network-measure/bind"
1924
"network-measure/tool"
2025
"network-measure/tool/icmp"
21-
"os"
22-
"time"
2326
)
2427

2528
var config Config
@@ -237,6 +240,10 @@ func getUserAgent() string {
237240
}
238241

239242
func main() {
243+
// go func() {
244+
// log.Println(http.ListenAndServe("localhost:6060", nil))
245+
// }()
246+
//
240247
var dialer = websocket.Dialer{
241248
HandshakeTimeout: 10 * time.Second,
242249
ReadBufferSize: 1048576,
@@ -265,7 +272,7 @@ func main() {
265272
log.Printf("Can't establish connection to server %s: %s\n", config.Conn.Remote, err)
266273
goto CleanUp
267274
} else {
268-
log.Fatalf("Can't establish connection to server %s: %s\n", config.Conn.Remote, err)
275+
log.Fatalf("Can't establish connection to server %s: %s, Exit.\n", config.Conn.Remote, err)
269276
}
270277
}
271278

@@ -279,7 +286,6 @@ func main() {
279286
case <-tick.C:
280287
_ = conn.WriteMessage(websocket.PingMessage, []byte("keep-alive"))
281288
case <-stopper:
282-
stopper <- struct{}{}
283289
return
284290
}
285291
}()
@@ -296,7 +302,6 @@ func main() {
296302
log.Printf("Failed writing response: %s\n", err)
297303
}
298304
case <-stopper:
299-
stopper <- struct{}{}
300305
return
301306
}
302307
}
@@ -318,10 +323,10 @@ func main() {
318323
}
319324

320325
if t == websocket.CloseMessage {
321-
payload, err := ioutil.ReadAll(r)
326+
payload, err := io.ReadAll(r)
322327
if err != nil {
323328
log.Printf("Connection closed. Can't read reason: %s\n", err)
324-
stopper <- struct{}{}
329+
close(stopper)
325330
goto CleanUp
326331
}
327332

@@ -361,7 +366,7 @@ func main() {
361366
p := handler(reqBuffer)
362367
select {
363368
case <-stopper:
364-
stopper <- struct{}{}
369+
return
365370
case pChan <- Response{
366371
Type: qType,
367372
Payload: p,
@@ -374,12 +379,14 @@ func main() {
374379
}
375380

376381
CleanUp:
382+
close(stopper)
377383
if conn != nil {
378-
stopper <- struct{}{}
379384
_ = conn.Close()
380385
}
381386
icmp.GetICMPManager().Flush()
382387

383-
time.Sleep(time.Duration(config.Conn.Interval) * time.Second)
388+
interval := time.Duration(config.Conn.Interval) * time.Second
389+
log.Printf("Wait %s before retry...\n", interval.String())
390+
time.Sleep(interval)
384391
}
385392
}

0 commit comments

Comments
 (0)