8
8
"net/http"
9
9
_ "net/http/pprof"
10
10
"os"
11
- "strconv"
12
11
"strings"
13
12
"sync"
13
+ "sync/atomic"
14
14
"time"
15
15
16
16
armlog "github.com/jumboframes/armorigo/log"
@@ -32,7 +32,8 @@ func main() {
32
32
count := pflag .Int ("count" , 10000 , "edges to dial" )
33
33
topic := pflag .String ("topic" , "test" , "topic to specific" )
34
34
nseconds := pflag .Int ("nseconds" , 10 , "publish message every n seconds for every edge" )
35
- sourceIPs := pflag .String ("source_ips" , "" , "source ips to dial, if your " )
35
+ msg := pflag .String ("msg" , "testtesttest" , "the message content to publish" )
36
+ sourceIPs := pflag .String ("source_ips" , "" , "source ips to dial, if you want dial more than \" 65535\" source ports" )
36
37
pprof := pflag .String ("pprof" , "" , "pprof addr to listen" )
37
38
pflag .Parse ()
38
39
@@ -43,7 +44,8 @@ func main() {
43
44
}
44
45
45
46
ips := []string {}
46
- idx := 0
47
+ idx := int64 (0 )
48
+
47
49
if * sourceIPs != "" {
48
50
ips = strings .Split (* sourceIPs , "," )
49
51
idx = 0
@@ -52,9 +54,11 @@ func main() {
52
54
53
55
dialer := func () (net.Conn , error ) {
54
56
if len (ips ) != 0 {
55
- for retry := 0 ; retry < 2 ; retry ++ {
57
+ for retry := 0 ; retry < 3 ; retry ++ {
58
+ thisidx := atomic .LoadInt64 (& idx )
59
+ ip := ips [int (thisidx )% len (ips )]
56
60
localAddr := & net.TCPAddr {
57
- IP : net .ParseIP (ips [ idx ] ),
61
+ IP : net .ParseIP (ip ),
58
62
}
59
63
dialer := & net.Dialer {
60
64
LocalAddr : localAddr ,
@@ -66,14 +70,10 @@ func main() {
66
70
}
67
71
if strings .Contains (err .Error (), "cannot assign requested address" ) ||
68
72
strings .Contains (err .Error (), "address already in use" ) {
69
- fmt .Println ("source ip:" , localAddr .IP .String (), localAddr .Port , err )
70
- idx += 1
71
- if idx >= len (ips ) {
72
- return nil , err
73
- }
73
+ atomic .AddInt64 (& idx , 1 )
74
74
continue
75
75
}
76
- fmt .Println ( " source ip:" , localAddr .IP .String (), localAddr .Port )
76
+ fmt .Printf ( "unknown dial err: %s, source ip: %s:%d \n " , err , localAddr .IP .String (), localAddr .Port )
77
77
return nil , err
78
78
}
79
79
}
@@ -96,7 +96,11 @@ func main() {
96
96
go func (i int ) {
97
97
defer wg .Done ()
98
98
// avoid congestion of connection
99
- random := rand .Intn (* count / 100 ) + 1
99
+ n := * count / 100
100
+ if n == 0 {
101
+ n = 1
102
+ }
103
+ random := rand .Intn (n ) + 1
100
104
time .Sleep (time .Second * time .Duration (random ))
101
105
// new edge connection
102
106
cli , err := edge .NewEdge (dialer ,
@@ -112,9 +116,8 @@ func main() {
112
116
mtx .Unlock ()
113
117
// publish message in loop
114
118
for {
115
- str := strconv .FormatInt (int64 (i ), 10 )
116
- msg := cli .NewMessage ([]byte (str ))
117
- err := cli .Publish (context .TODO (), * topic , msg )
119
+ gmsg := cli .NewMessage ([]byte (* msg ))
120
+ err := cli .Publish (context .TODO (), * topic , gmsg )
118
121
if err != nil {
119
122
fmt .Println ("publish err" , err )
120
123
break
0 commit comments