@@ -17,9 +17,6 @@ import (
1717)
1818
1919var (
20- // ErrTooManyReconnects is returned when the number of reconnections
21- // has reached MaxReconnectionsTotal within MaxElapsedTime.
22- ErrTooManyReconnects = errors .New ("websocket cannot reconnect right now (too many attemps)" )
2320 // ErrNotDialed is returned when WriteMessage is called, but
2421 // the websocket has not been created yet (call Dial).
2522 ErrNotDailed = errors .New ("websocket not created yet, please call Dial()" )
@@ -48,36 +45,25 @@ type Conn struct {
4845 // MaxElapsedTime is the amount of time after which the ExponentialBackOff
4946 // returns Stop. It never stops if MaxElapsedTime == 0.
5047 MaxElapsedTime time.Duration
51- // MaxReconnectionsTotal is the maximum number of reconnections that can
52- // happen within MaxReconnectionsTime.
53- MaxReconnectionsTotal int
54- // MaxReconnectionsTime is the time period during which the number of
55- // reconnections must be less than MaxReconnectionsTotal to allow a
56- // reconnection atttempt.
57- MaxReconnectionsTime time.Duration
58- dialer websocket.Dialer
59- ws * websocket.Conn
60- url url.URL
61- header http.Header
62- dialMessage interface {}
63- ticker time.Ticker
64- mu sync.Mutex
65- reconnections int
66- isDialed bool
67- isConnected bool
68- stop chan bool
48+ dialer websocket.Dialer
49+ ws * websocket.Conn
50+ url url.URL
51+ header http.Header
52+ dialMessage interface {}
53+ ticker time.Ticker
54+ mu sync.Mutex
55+ isDialed bool
56+ isConnected bool
6957}
7058
7159// NewConn creates a new Conn with default values.
7260func NewConn () * Conn {
7361 c := & Conn {
74- InitialInterval : static .BackoffInitialInterval ,
75- RandomizationFactor : static .BackoffRandomizationFactor ,
76- Multiplier : static .BackoffMultiplier ,
77- MaxInterval : static .BackoffMaxInterval ,
78- MaxElapsedTime : static .BackoffMaxElapsedTime ,
79- MaxReconnectionsTotal : static .MaxReconnectionsTotal ,
80- MaxReconnectionsTime : static .MaxReconnectionsTime ,
62+ InitialInterval : static .BackoffInitialInterval ,
63+ RandomizationFactor : static .BackoffRandomizationFactor ,
64+ Multiplier : static .BackoffMultiplier ,
65+ MaxInterval : static .BackoffMaxInterval ,
66+ MaxElapsedTime : static .BackoffMaxElapsedTime ,
8167 }
8268 return c
8369}
@@ -101,23 +87,9 @@ func (c *Conn) Dial(address string, header http.Header, dialMsg interface{}) err
10187 }
10288 c .url = * u
10389 c .dialMessage = dialMsg
104- c .stop = make (chan bool )
10590 c .header = header
10691 c .dialer = websocket.Dialer {}
10792 c .isDialed = true
108- c .ticker = * time .NewTicker (c .MaxReconnectionsTime )
109- go func (c * Conn ) {
110- for {
111- select {
112- case <- c .stop :
113- c .ticker .Stop ()
114- return
115- case <- c .ticker .C :
116- c .resetReconnections ()
117- }
118- }
119- }(c )
120-
12193 return c .connect ()
12294}
12395
@@ -129,8 +101,7 @@ func (c *Conn) Dial(address string, header http.Header, dialMsg interface{}) err
129101// The write will fail under the following conditions:
130102// 1. The client has not called Dial (ErrNotDialed).
131103// 2. The connection is disconnected and it was not able to
132- // reconnect (ErrTooManyReconnects or an internal connection
133- // error).
104+ // reconnect.
134105// 3. The write call in the websocket package failed
135106// (gorilla/websocket error).
136107func (c * Conn ) WriteMessage (messageType int , data interface {}) error {
@@ -160,39 +131,22 @@ func (c *Conn) IsConnected() bool {
160131 return c .isConnected
161132}
162133
163- // CanConnect checks whether it is possible to reconnect
164- // given the recent number of attempts.
165- func (c * Conn ) CanConnect () bool {
166- c .mu .Lock ()
167- defer c .mu .Unlock ()
168- return c .reconnections < c .MaxReconnectionsTotal
169- }
170-
171134// Close closes the network connection and cleans up private
172135// resources after the connection is done.
173136func (c * Conn ) Close () error {
174137 if c .isDialed {
175138 c .isDialed = false
176- c .stop <- true
177139 }
178140 return c .close ()
179141}
180142
181- // resetReconnections sets the number of disconnects followed
182- // by reconnects.
183- func (c * Conn ) resetReconnections () {
184- c .mu .Lock ()
185- defer c .mu .Unlock ()
186- c .reconnections = 0
187- }
188-
189- // closeAndReconnect calls close and reconnect.
143+ // closeAndReconnect calls close and reconnects.
190144func (c * Conn ) closeAndReconnect () error {
191145 err := c .close ()
192146 if err != nil {
193147 return err
194148 }
195- return c .reconnect ()
149+ return c .connect ()
196150}
197151
198152// close closes the underlying network connection without
@@ -207,18 +161,6 @@ func (c *Conn) close() error {
207161 return nil
208162}
209163
210- // reconnect updates the number of reconnections and
211- // re-establishes the connection.
212- func (c * Conn ) reconnect () error {
213- if ! c .CanConnect () {
214- return ErrTooManyReconnects
215- }
216- c .mu .Lock ()
217- c .reconnections ++
218- c .mu .Unlock ()
219- return c .connect ()
220- }
221-
222164// connect creates a new client connection and sends the
223165// registration message.
224166// In case of failure, it uses an exponential backoff to
0 commit comments