Prevent spurious diffs in config file by sorting Client objects before writing

This commit is contained in:
Keith Collister 2021-12-12 11:46:13 +01:00
parent 86cffcd168
commit 774937728b
1 changed files with 13 additions and 2 deletions

View File

@ -222,6 +222,9 @@ func (clients *clientsContainer) addFromConfig(objects []clientObject) {
// WriteDiskConfig - write configuration // WriteDiskConfig - write configuration
func (clients *clientsContainer) WriteDiskConfig(objects *[]clientObject) { func (clients *clientsContainer) WriteDiskConfig(objects *[]clientObject) {
clients.lock.Lock() clients.lock.Lock()
defer clients.lock.Unlock()
clientObjects := []clientObject{}
for _, cli := range clients.list { for _, cli := range clients.list {
cy := clientObject{ cy := clientObject{
Name: cli.Name, Name: cli.Name,
@ -238,9 +241,17 @@ func (clients *clientsContainer) WriteDiskConfig(objects *[]clientObject) {
cy.BlockedServices = stringutil.CloneSlice(cli.BlockedServices) cy.BlockedServices = stringutil.CloneSlice(cli.BlockedServices)
cy.Upstreams = stringutil.CloneSlice(cli.Upstreams) cy.Upstreams = stringutil.CloneSlice(cli.Upstreams)
*objects = append(*objects, cy) clientObjects = append(clientObjects, cy)
} }
clients.lock.Unlock()
// Maps aren't guaranteed to iterate in the same order each time, so the
// above loop can generate different orderings when writing to the
// config file: this produces lots of diffs in config files, so sort
// objects by name before writing.
sort.Slice(clientObjects, func(i, j int) bool {
return clientObjects[i].Name < clientObjects[j].Name
})
*objects = append(*objects, clientObjects...)
} }
func (clients *clientsContainer) periodicUpdate() { func (clients *clientsContainer) periodicUpdate() {