Skip to content

Commit

Permalink
Add *Node argument to connect and reconnect function
Browse files Browse the repository at this point in the history
Signed-off-by: bill fort <[email protected]>
  • Loading branch information
billfort authored and yilunzhang committed Jun 8, 2023
1 parent ad192f3 commit ff43714
Show file tree
Hide file tree
Showing 7 changed files with 332 additions and 21 deletions.
40 changes: 20 additions & 20 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ type Client struct {
address string
addressID []byte
sigChainBlockHash string
reconnectChan chan struct{}
reconnectChan chan *Node
responseChannels *cache.Cache
resolvers []Resolver

Expand Down Expand Up @@ -145,7 +145,7 @@ func NewClient(account *Account, identifier string, config *ClientConfig) (*Clie
addressID: addressToID(addr),
OnConnect: NewOnConnect(1, nil),
OnMessage: NewOnMessage(int(config.MsgChanLen), nil),
reconnectChan: make(chan struct{}),
reconnectChan: make(chan *Node),
responseChannels: cache.New(time.Duration(config.MsgCacheExpiration)*time.Millisecond, time.Duration(config.MsgCacheCleanupInterval)*time.Millisecond),
sharedKeys: make(map[string]*[sharedKeySize]byte),
wallet: w,
Expand All @@ -155,7 +155,7 @@ func NewClient(account *Account, identifier string, config *ClientConfig) (*Clie

go c.handleReconnect()

err = c.connect(int(c.config.ConnectRetries))
err = c.connect(int(c.config.ConnectRetries), nil)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -423,10 +423,7 @@ func (c *Client) handleMessage(msgType int, data []byte) error {
return err
}
go func() {
err := c.connectToNode(&node)
if err != nil {
c.Reconnect()
}
c.Reconnect(&node)
}()
} else if action == setClientAction {
c.Close()
Expand Down Expand Up @@ -670,7 +667,7 @@ func (c *Client) connectToNode(node *Node) error {
c.lock.Unlock()
if err != nil {
log.Println(err)
c.Reconnect()
c.Reconnect(nil)
return
}
case <-done:
Expand Down Expand Up @@ -704,7 +701,7 @@ func (c *Client) connectToNode(node *Node) error {
c.lock.Unlock()
if err != nil {
log.Println(err)
c.Reconnect()
c.Reconnect(nil)
return
}
}()
Expand All @@ -719,7 +716,7 @@ func (c *Client) connectToNode(node *Node) error {
msgType, data, err := conn.ReadMessage()
if err != nil {
log.Println(err)
c.Reconnect()
c.Reconnect(nil)
return
}

Expand All @@ -736,7 +733,7 @@ func (c *Client) connectToNode(node *Node) error {
return nil
}

func (c *Client) connect(maxRetries int) error {
func (c *Client) connect(maxRetries int, node *Node) error {
retryInterval := c.config.MinReconnectInterval
for retry := 1; maxRetries < 0 || retry <= maxRetries; retry++ {
if retry > 1 {
Expand All @@ -748,10 +745,13 @@ func (c *Client) connect(maxRetries int) error {
}
}

node, err := GetWsAddr(c.Address(), c.config)
if err != nil {
log.Println(err)
continue
var err error
if node == nil {
node, err = GetWsAddr(c.Address(), c.config)
if err != nil {
log.Println(err)
continue
}
}

err = c.connectToNode(node)
Expand All @@ -767,26 +767,26 @@ func (c *Client) connect(maxRetries int) error {
}

// Reconnect forces the client to find node and connect again.
func (c *Client) Reconnect() {
func (c *Client) Reconnect(node *Node) {
if c.IsClosed() {
return
}
select {
case c.reconnectChan <- struct{}{}:
case c.reconnectChan <- node:
default:
}
}

func (c *Client) handleReconnect() {
for range c.reconnectChan {
for node := range c.reconnectChan {
if c.IsClosed() {
return
}

log.Printf("Reconnect in %v ms...", c.config.MinReconnectInterval)
time.Sleep(time.Duration(c.config.MinReconnectInterval) * time.Millisecond)

err := c.connect(-1)
err := c.connect(-1, node)
if err != nil {
log.Println(err)
c.Close()
Expand All @@ -805,7 +805,7 @@ func (c *Client) writeMessage(buf []byte, writeTimeout time.Duration) error {
err := c.conn.WriteMessage(websocket.BinaryMessage, buf)
c.lock.Unlock()
if err != nil {
c.Reconnect()
c.Reconnect(nil)
}
return err
}
Expand Down
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,18 @@ require (
github.com/nknorg/nkn/v2 v2.1.7
github.com/nknorg/nkngomobile v0.0.0-20220615081414-671ad1afdfa9
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/stretchr/testify v1.8.1
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e
golang.org/x/mobile v0.0.0-20230301163155-e0f57694e12c
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/hashicorp/errwrap v1.0.0 // indirect
github.com/itchyny/base58-go v0.0.5 // indirect
github.com/pbnjay/memory v0.0.0-20190104145345-974d429e7ae4 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/sys v0.1.0 // indirect
google.golang.org/protobuf v1.26.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ github.com/cloudflare/cloudflare-go v0.10.2/go.mod h1:qhVI5MKwBGhdNU89ZRz2plgYut
github.com/cpu/goacmedns v0.0.2/go.mod h1:4MipLkI+qScwqtVxcNO6okBhbgRrr7/tKXUSgSL0teQ=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/dimchansky/utfbom v1.1.0/go.mod h1:rO41eb7gLfo8SF1jd9F8HplJm1Fewwi4mQvIirEdv+8=
Expand Down Expand Up @@ -210,8 +211,10 @@ github.com/kolo/xmlrpc v0.0.0-20200310150728-e0350524596b/go.mod h1:o03bZfuBwAXH
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/labbsr0x/bindman-dns-webhook v1.0.2/go.mod h1:p6b+VCXIR8NYKpDr8/dg1HKfQoRHCdcsROXKvmoehKA=
github.com/labbsr0x/goh v1.0.1/go.mod h1:8K2UhVoaWXcCU7Lxoa2omWnC8gyW8px7/lmO61c027w=
Expand Down Expand Up @@ -275,6 +278,7 @@ github.com/pkg/errors v0.0.0-20190227000051-27936f6d90f9/go.mod h1:bwawxfHBFNV+L
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pquerna/otp v1.2.0/go.mod h1:dkJfzwRKNiegxyNb54X/3fLwhCynbMspSyWKnvi1AEg=
github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
Expand Down Expand Up @@ -317,6 +321,7 @@ github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81P
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/syndtr/goleveldb v1.0.1-0.20210305035536-64b5b1c73954/go.mod h1:u2MKkTVTVJWe5D1rCvame8WqhBd88EuIwODJZ1VHCPM=
github.com/templexxx/cpufeat v0.0.0-20180724012125-cef66df7f161/go.mod h1:wM7WEvslTq+iOEAMDLSzhVuOt5BRZ05WirO+b09GHQU=
Expand Down Expand Up @@ -578,6 +583,7 @@ google.golang.org/protobuf v1.26.0 h1:bxAC2xTBsZGibn2RTntX0oH50xLsqy1OxA9tTL3p/l
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
Expand Down
2 changes: 1 addition & 1 deletion multiclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -812,7 +812,7 @@ func (m *MultiClient) Reconnect() {
}

for _, client := range m.clients {
client.Reconnect()
client.Reconnect(nil)
}
}

Expand Down
15 changes: 15 additions & 0 deletions tests/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package tests

const (
numMsgs = 100
numClients = 4
numExitMsgs = 10
exitMsg = "exit"
recvMsgTimeout = 10000 // milli-second

listenerId = "Bob"
dialerId = "Alice"
seedHex = "e68e046d13dd911594576ba0f4a196e9666790dc492071ad9ea5972c0b940435"
listenerAddr = "Bob.be285ff9330122cea44487a9618f96603fde6d37d5909ae1c271616772c349fe"
dialerAddr = "Alice.be285ff9330122cea44487a9618f96603fde6d37d5909ae1c271616772c349fe"
)
Loading

0 comments on commit ff43714

Please sign in to comment.