@@ -17,6 +17,7 @@ package quickfix
1717
1818import (
1919 "bufio"
20+ "context"
2021 "crypto/tls"
2122 "strings"
2223 "sync"
@@ -50,7 +51,7 @@ func (i *Initiator) Start() (err error) {
5051 return
5152 }
5253
53- var dialer proxy.Dialer
54+ var dialer proxy.ContextDialer
5455 if dialer , err = loadDialerConfig (settings ); err != nil {
5556 return
5657 }
@@ -142,7 +143,7 @@ func (i *Initiator) waitForReconnectInterval(reconnectInterval time.Duration) bo
142143 return true
143144}
144145
145- func (i * Initiator ) handleConnection (session * session , tlsConfig * tls.Config , dialer proxy.Dialer ) {
146+ func (i * Initiator ) handleConnection (session * session , tlsConfig * tls.Config , dialer proxy.ContextDialer ) {
146147 var wg sync.WaitGroup
147148 wg .Add (1 )
148149 go func () {
@@ -162,14 +163,27 @@ func (i *Initiator) handleConnection(session *session, tlsConfig *tls.Config, di
162163 return
163164 }
164165
166+ ctx , cancel := context .WithCancel (context .Background ())
167+
168+ // We start a goroutine in order to be able to cancel the dialer mid-connection
169+ // on receiving a stop signal to stop the initiator.
170+ go func () {
171+ select {
172+ case <- i .stopChan :
173+ cancel ()
174+ case <- ctx .Done ():
175+ return
176+ }
177+ }()
178+
165179 var disconnected chan interface {}
166180 var msgIn chan fixIn
167181 var msgOut chan []byte
168182
169183 address := session .SocketConnectAddress [connectionAttempt % len (session .SocketConnectAddress )]
170184 session .log .OnEventf ("Connecting to: %v" , address )
171185
172- netConn , err := dialer .Dial ( "tcp" , address )
186+ netConn , err := dialer .DialContext ( ctx , "tcp" , address )
173187 if err != nil {
174188 session .log .OnEventf ("Failed to connect: %v" , err )
175189 goto reconnect
@@ -208,13 +222,19 @@ func (i *Initiator) handleConnection(session *session, tlsConfig *tls.Config, di
208222 close (disconnected )
209223 }()
210224
225+ // This ensures we properly cleanup the goroutine and context used for
226+ // dial cancelation after successful connection.
227+ cancel ()
228+
211229 select {
212230 case <- disconnected :
213231 case <- i .stopChan :
214232 return
215233 }
216234
217235 reconnect:
236+ cancel ()
237+
218238 connectionAttempt ++
219239 session .log .OnEventf ("Reconnecting in %v" , session .ReconnectInterval )
220240 if ! i .waitForReconnectInterval (session .ReconnectInterval ) {
0 commit comments