-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: decouple detection and change propagation (#210)
* feat: decouple transmitting updates from detecting them * fix: init map * fix: run reconciler in goroutine * fix: range gotcha
- Loading branch information
1 parent
b74e01d
commit c687a2d
Showing
10 changed files
with
288 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package client | ||
|
||
import ( | ||
"errors" | ||
"github.com/rs/zerolog/log" | ||
"github.com/soerenschneider/dyndns/internal/common" | ||
"github.com/soerenschneider/dyndns/internal/events" | ||
"github.com/soerenschneider/dyndns/internal/metrics" | ||
"sync" | ||
"time" | ||
) | ||
|
||
type Reconciler struct { | ||
env *common.Envelope | ||
dispatchers map[string]events.EventDispatch | ||
mutex sync.Mutex | ||
|
||
pendingChanges map[string]events.EventDispatch | ||
} | ||
|
||
func NewReconciler(dispatchers map[string]events.EventDispatch) (*Reconciler, error) { | ||
if len(dispatchers) < 1 { | ||
return nil, errors.New("no dispatchers supplied") | ||
} | ||
|
||
return &Reconciler{ | ||
dispatchers: dispatchers, | ||
mutex: sync.Mutex{}, | ||
}, nil | ||
} | ||
|
||
func (r *Reconciler) RegisterUpdate(env *common.Envelope) { | ||
if env == nil { | ||
return | ||
} | ||
|
||
r.mutex.Lock() | ||
r.env = env | ||
|
||
r.pendingChanges = make(map[string]events.EventDispatch, len(r.dispatchers)) | ||
for i, dispatcher := range r.dispatchers { | ||
r.pendingChanges[i] = dispatcher | ||
} | ||
metrics.ReconcilersActive.WithLabelValues(env.PublicIp.Host).Set(float64(len(r.pendingChanges))) | ||
|
||
r.mutex.Unlock() | ||
r.dispatch() | ||
} | ||
|
||
func (r *Reconciler) dispatch() { | ||
r.mutex.Lock() | ||
defer r.mutex.Unlock() | ||
|
||
if len(r.pendingChanges) == 0 { | ||
return | ||
} | ||
|
||
metrics.ReconcilerTimestamp.WithLabelValues(r.env.PublicIp.Host).SetToCurrentTime() | ||
log.Info().Msgf("Reconciling %d dispatchers", len(r.pendingChanges)) | ||
|
||
timeStart := time.Now() | ||
wg := sync.WaitGroup{} | ||
wg.Add(len(r.pendingChanges)) | ||
for key, _ := range r.pendingChanges { | ||
dispatcher := r.pendingChanges[key] | ||
go func(key string) { | ||
err := dispatcher.Notify(r.env) | ||
if err == nil { | ||
r.pendingChanges[key] = nil | ||
delete(r.pendingChanges, key) | ||
metrics.UpdatesDispatched.Inc() | ||
log.Info().Msgf("Reconciliation for dispatcher %s successful", key) | ||
} else { | ||
log.Warn().Msgf("Reconciliation for dispatcher %s failed: %v", key, err) | ||
} | ||
wg.Done() | ||
}(key) | ||
} | ||
|
||
wg.Wait() | ||
timeSpent := time.Now().Sub(timeStart) | ||
|
||
log.Info().Msgf("Spent %v on reconciliation (%d dispatchers)", timeSpent, len(r.dispatchers)) | ||
metrics.ReconcilersActive.WithLabelValues(r.env.PublicIp.Host).Set(float64(len(r.pendingChanges))) | ||
} | ||
|
||
func (r *Reconciler) Run() { | ||
interval := 1 * time.Minute | ||
ticker := time.NewTicker(interval) | ||
|
||
for { | ||
select { | ||
case <-ticker.C: | ||
r.dispatch() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
//go:build client | ||
|
||
package mqtt | ||
|
||
import ( | ||
"crypto/tls" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
mqtt "github.com/eclipse/paho.mqtt.golang" | ||
"github.com/rs/zerolog/log" | ||
"github.com/soerenschneider/dyndns/internal/common" | ||
"github.com/soerenschneider/dyndns/internal/metrics" | ||
"time" | ||
) | ||
|
||
const publishWaitTimeout = 2 * time.Minute | ||
|
||
type MqttClientBus struct { | ||
client mqtt.Client | ||
notificationTopic string | ||
} | ||
|
||
var onConnectHandler = func(c mqtt.Client) { | ||
opts := c.OptionsReader() | ||
log.Info().Msgf("Connected to brokers %v", opts.Servers()) | ||
mutex.Lock() | ||
metrics.MqttBrokersConnectedTotal.Add(1) | ||
mutex.Unlock() | ||
} | ||
|
||
func NewMqttClient(broker string, clientId, notificationTopic string, tlsConfig *tls.Config) (*MqttClientBus, error) { | ||
opts := mqtt.NewClientOptions() | ||
opts.AddBroker(broker) | ||
opts.SetClientID(clientId) | ||
|
||
opts.OnConnectionLost = connectLostHandler | ||
opts.OnConnect = onConnectHandler | ||
opts.AutoReconnect = true | ||
|
||
if tlsConfig != nil { | ||
opts.SetTLSConfig(tlsConfig) | ||
} | ||
|
||
client := mqtt.NewClient(opts) | ||
token := client.Connect() | ||
if token.WaitTimeout(60*time.Second) && token.Error() != nil { | ||
return nil, fmt.Errorf("could not connect to %s: %v", broker, token.Error()) | ||
} | ||
|
||
return &MqttClientBus{ | ||
client: client, | ||
notificationTopic: notificationTopic, | ||
}, nil | ||
} | ||
|
||
func (d *MqttClientBus) Notify(msg *common.Envelope) error { | ||
payload, err := json.Marshal(msg) | ||
if err != nil { | ||
return fmt.Errorf("could not marshal envelope: %v", err) | ||
} | ||
opts := d.client.OptionsReader() | ||
log.Debug().Msgf("Sending %v to %v", string(payload), opts.Servers()) | ||
|
||
token := d.client.Publish(d.notificationTopic, 1, true, payload) | ||
ok := token.WaitTimeout(publishWaitTimeout) | ||
if !ok { | ||
return errors.New("received timeout when trying to publish the message") | ||
} | ||
log.Debug().Msgf("Dispatched message to %v", opts.Servers()) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package mqtt | ||
|
||
import ( | ||
mqtt "github.com/eclipse/paho.mqtt.golang" | ||
"github.com/rs/zerolog/log" | ||
"github.com/soerenschneider/dyndns/internal/metrics" | ||
"sync" | ||
) | ||
|
||
var mutex sync.Mutex | ||
|
||
var connectLostHandler mqtt.ConnectionLostHandler = func(client mqtt.Client, err error) { | ||
opts := client.OptionsReader() | ||
log.Info().Msgf("Connection lost from %v: %v", opts.Servers(), err) | ||
metrics.MqttConnectionsLostTotal.Inc() | ||
mutex.Lock() | ||
defer mutex.Unlock() | ||
metrics.MqttBrokersConnectedTotal.Sub(1) | ||
} |
Oops, something went wrong.