Skip to content

Commit

Permalink
fix(connect): Ensure Connect instance runs only once
Browse files Browse the repository at this point in the history
  • Loading branch information
robinbraemer committed Aug 22, 2023
1 parent 5af2c7d commit 73d0ab8
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 15 deletions.
9 changes: 5 additions & 4 deletions .web/docs/guide/config/reload.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Auto Config Reload

_Gate watches your file for updates._
_Gate watches your config file for updates._

---

Expand Down Expand Up @@ -36,6 +36,7 @@ This feature is always enabled by default, given that you have a config file.

## How to disable it

This can not be disabled.
If you feel like you need to disable it, please [open an issue](
https://github.com/minekube/gate/issues/new?title=Disable%20auto%20config%20reload&body=I%20want%20to%20disable%20auto%20config%20reload%20because%20...).
Please note that the auto config reload feature cannot be disabled.
If you feel a compelling need to do so, please don't hesitate to [open an issue](
https://github.com/minekube/gate/issues/new?title=Disable%20auto%20config%20reload&body=I%20want%20to%20disable%20auto%20config%20reload%20because%20...)
on our GitHub repository.
45 changes: 34 additions & 11 deletions pkg/gate/connect.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package gate

import (
"bytes"
"context"
"crypto/sha1"
"encoding/json"
"sync"

"github.com/go-logr/logr"
Expand All @@ -26,29 +29,38 @@ func setupConnect(
var (
mu sync.Mutex
stopConnect context.CancelFunc
name string
// keep track of current config hash to avoid unnecessary restarts when config didn't change
currentConfigHash []byte
)
trigger := func(c *config.Config) {
cfg := c.Connect
if cfg.Enabled && !c.Editions.Java.Enabled {
connect := c.Connect
if connect.Enabled && !c.Editions.Java.Enabled {
log.Info("Connect is only supported for Java edition")
return
}

newConfigHash, err := jsonHash(connect)
if err != nil {
log.Error(err, "error hashing Connect config")
return
}

mu.Lock()
defer mu.Unlock()
if (!cfg.Enabled && stopConnect != nil) ||
(cfg.Enabled && (stopConnect == nil || cfg.Name != name)) {

if stopConnect != nil {
stopConnect()
stopConnect = nil
}
// check if config changed
if bytes.Equal(newConfigHash, currentConfigHash) {
return // no change
}
currentConfigHash = newConfigHash

name = cfg.Name
// stop current Connect if running
if stopConnect != nil {
stopConnect()
stopConnect = nil
}

runnable, err := connectcfg.New(cfg, instance)
runnable, err := connectcfg.New(connect, instance)
if err != nil {
log.Error(err, "error setting up Connect")
return
Expand All @@ -58,6 +70,7 @@ func setupConnect(
runCtx, stopConnect = context.WithCancel(ctx)

go func() {
defer stopConnect()
if err = runnable.Start(runCtx); err != nil {
log.Error(err, "error with Connect")
return
Expand All @@ -76,3 +89,13 @@ func setupConnect(
return nil
}))
}

// jsonHash returns the sha1 hash of the JSON representation of v.
func jsonHash(v any) ([]byte, error) {
j, err := json.Marshal(v)
if err != nil {
return nil, err
}
h := sha1.Sum(j)
return h[:], nil
}

0 comments on commit 73d0ab8

Please sign in to comment.