-
Notifications
You must be signed in to change notification settings - Fork 491
/
publish.go
49 lines (39 loc) · 1.18 KB
/
publish.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Copyright 2014 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package peergrouper
import (
"reflect"
"sort"
"sync"
"github.com/juju/errors"
"github.com/juju/juju/core/network"
)
// CachingAPIHostPortsSetter is an APIHostPortsSetter that caches the
// most recently set values, suppressing further calls to the underlying
// setter if any call's arguments match those of the preceding call.
type CachingAPIHostPortsSetter struct {
APIHostPortsSetter
mu sync.Mutex
last []network.SpaceHostPorts
}
func (s *CachingAPIHostPortsSetter) SetAPIHostPorts(apiServers []network.SpaceHostPorts) error {
if len(apiServers) == 0 {
return errors.Errorf("no API servers specified")
}
sorted := make([]network.SpaceHostPorts, len(apiServers))
for i, hostPorts := range apiServers {
sorted[i] = append(network.SpaceHostPorts{}, hostPorts...)
sort.Sort(sorted[i])
}
s.mu.Lock()
defer s.mu.Unlock()
if reflect.DeepEqual(sorted, s.last) {
logger.Debugf("API host ports have not changed")
return nil
}
if err := s.APIHostPortsSetter.SetAPIHostPorts(sorted); err != nil {
return errors.Annotate(err, "setting API host ports")
}
s.last = sorted
return nil
}