Skip to content

Commit

Permalink
config, api: add API for getting the config checksum (#203)
Browse files Browse the repository at this point in the history
  • Loading branch information
djshow832 authored Feb 1, 2023
1 parent f388efb commit df06ec4
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 8 deletions.
2 changes: 1 addition & 1 deletion lib/config/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@
package config

type HealthInfo struct {
ConfigVersion uint32 `json:"config_version"`
ConfigChecksum uint32 `json:"config_checksum"`
}
23 changes: 18 additions & 5 deletions pkg/manager/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package config

import (
"bytes"
"hash/crc32"
"os"
"time"

Expand Down Expand Up @@ -74,8 +76,9 @@ func (e *ConfigManager) SetTOMLConfig(data []byte) error {
return err
}

e.sts.current = base
e.sts.version++
if err := e.setCurrentConfig(base); err != nil {
return err
}

for _, list := range e.sts.listeners {
list <- base.Clone()
Expand All @@ -84,18 +87,28 @@ func (e *ConfigManager) SetTOMLConfig(data []byte) error {
return nil
}

func (e *ConfigManager) setCurrentConfig(cfg *config.Config) error {
e.sts.current = cfg
var buf bytes.Buffer
if err := toml.NewEncoder(&buf).Encode(cfg); err != nil {
return err
}
e.sts.checksum = crc32.ChecksumIEEE(buf.Bytes())
return nil
}

func (e *ConfigManager) GetConfig() *config.Config {
e.sts.Lock()
v := e.sts.current
e.sts.Unlock()
return v
}

func (e *ConfigManager) GetConfigVersion() uint32 {
func (e *ConfigManager) GetConfigChecksum() uint32 {
e.sts.Lock()
v := e.sts.version
c := e.sts.checksum
e.sts.Unlock()
return v
return c
}

func (e *ConfigManager) WatchConfig() <-chan *config.Config {
Expand Down
15 changes: 15 additions & 0 deletions pkg/manager/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,18 @@ func TestConfigRemove(t *testing.T) {
require.NoError(t, os.WriteFile(tmpcfg, []byte(`proxy.addr = "vv"`), 0644))
require.Eventually(t, func() bool { return cfgmgr.GetConfig().Proxy.Addr == "vv" }, time.Second, 100*time.Millisecond)
}

func TestChecksum(t *testing.T) {
cfgmgr, _ := testConfigManager(t, "")
c1 := cfgmgr.GetConfigChecksum()
require.NoError(t, cfgmgr.SetTOMLConfig([]byte(`proxy.addr = "gg"`)))
c2 := cfgmgr.GetConfigChecksum()
require.NoError(t, cfgmgr.SetTOMLConfig([]byte(`proxy.addr = "vv"`)))
c3 := cfgmgr.GetConfigChecksum()
require.NoError(t, cfgmgr.SetTOMLConfig([]byte(`proxy.addr="gg"`)))
c4 := cfgmgr.GetConfigChecksum()
require.Equal(t, c2, c4)
require.NotEqual(t, c1, c2)
require.NotEqual(t, c1, c3)
require.NotEqual(t, c2, c3)
}
2 changes: 1 addition & 1 deletion pkg/manager/config/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ type ConfigManager struct {
sync.Mutex
listeners []chan<- *config.Config
current *config.Config
version uint32
checksum uint32
}
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/server/api/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (

func (h *HTTPServer) DebugHealth(c *gin.Context) {
c.JSON(http.StatusOK, config.HealthInfo{
ConfigVersion: h.mgr.cfg.GetConfigVersion(),
ConfigChecksum: h.mgr.cfg.GetConfigChecksum(),
})
}

Expand Down

0 comments on commit df06ec4

Please sign in to comment.