From 43db3a0d645e9d342e72d0184bac6a46351ea9d6 Mon Sep 17 00:00:00 2001 From: mappu Date: Wed, 4 May 2016 19:15:02 +1200 Subject: [PATCH] option to disable autoreconnect, move magic to constant --- HubConnection.go | 15 ++++++++++----- HubConnectionOptions.go | 16 ++++++++++------ libnmdc.go | 1 + 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/HubConnection.go b/HubConnection.go index 31a9022..09df6ad 100644 --- a/HubConnection.go +++ b/HubConnection.go @@ -21,9 +21,10 @@ type HubConnection struct { OnEvent chan HubEvent // Private state - conn net.Conn // this is an interface - connValid bool - sentOurHello bool + conn net.Conn // this is an interface + connValid bool + sentOurHello bool + autoReconnect bool } func (this *HubConnection) SayPublic(message string) { @@ -268,8 +269,12 @@ func (this *HubConnection) worker() { this.connValid = false this.processEvent(HubEvent{EventType: EVENT_CONNECTION_STATE_CHANGED, StateChange: CONNECTIONSTATE_DISCONNECTED, Message: err.Error()}) - time.Sleep(30 * time.Second) // Wait before reconnect - continue + if this.autoReconnect { + time.Sleep(AUTO_RECONNECT_AFTER) // Wait before reconnect + continue + } else { + return // leave the worker for good + } } } diff --git a/HubConnectionOptions.go b/HubConnectionOptions.go index 092fa9c..cc52ecc 100644 --- a/HubConnectionOptions.go +++ b/HubConnectionOptions.go @@ -1,10 +1,11 @@ package libnmdc type HubConnectionOptions struct { - Address HubAddress - SkipVerifyTLS bool // using a negative verb, because bools default to false - Self UserInfo - NickPassword string + Address HubAddress + SkipVerifyTLS bool // using a negative verb, because bools default to false + SkipAutoReconnect bool // as above + Self UserInfo + NickPassword string // Returning messages in async mode NumEventsToBuffer uint @@ -23,13 +24,16 @@ func (this *HubConnectionOptions) prepareConnection() *HubConnection { HubName: DEFAULT_HUB_NAME, State: CONNECTIONSTATE_DISCONNECTED, Users: make(map[string]UserInfo), + + autoReconnect: !this.SkipAutoReconnect, } return &hc } // Connects to an NMDC server, and spawns a background goroutine to handle -// protocol messages. Client code should select on all the interface channels. +// protocol messages. Events will be sent by channel to the returned hc.OnEvent, +// the client is responsible for selecting off this. func (this *HubConnectionOptions) Connect() *HubConnection { if this.NumEventsToBuffer < 1 { @@ -47,7 +51,7 @@ func (this *HubConnectionOptions) Connect() *HubConnection { } // Connects to an NMDC server, and blocks forever to handle protocol messages. -// Client code should supply an event handling function. +// Client code should supply an event handling function as hco.OnEventSync. func (this *HubConnectionOptions) ConnectSync() { hc := this.prepareConnection() hc.worker() diff --git a/libnmdc.go b/libnmdc.go index ca4496e..00b390b 100644 --- a/libnmdc.go +++ b/libnmdc.go @@ -13,6 +13,7 @@ const ( DEFAULT_CLIENT_TAG string = "libnmdc.go" DEFAULT_HUB_NAME string = "(unknown)" SEND_KEEPALIVE_EVERY time.Duration = 29 * time.Second + AUTO_RECONNECT_AFTER time.Duration = 30 * time.Second ) var rx_protocolMessage *regexp.Regexp