~go-bot/juju-core/trunk

« back to all changes in this revision

Viewing changes to state/api/apiclient.go

[r=hduran-8] Use localhost only if pressent for opening state

Recently a change was made so localhost is added in the list
of addresses to open state if we are servingstatus.
In that change the addresses where sorted to favor localhost
ones, with this new change, if localhost is present, its the
only one used and the other ones are discarded.

https://codereview.appspot.com/103820044/

R=jameinel, thumper

Show diffs side-by-side

added added

removed removed

Lines of Context:
8
8
        "crypto/x509"
9
9
        "fmt"
10
10
        "io"
11
 
        "sort"
12
11
        "strings"
13
12
        "time"
14
13
 
110
109
        }
111
110
}
112
111
 
113
 
type LocalFirst []string
114
 
 
115
 
func (l LocalFirst) Len() int {
116
 
        return len(l)
117
 
}
118
 
func (l LocalFirst) Swap(i, j int) {
119
 
        l[i], l[j] = l[j], l[i]
120
 
}
121
 
func (l LocalFirst) Less(i, j int) bool {
122
 
        return strings.HasPrefix(l[i], "localhost") && !strings.HasPrefix(l[j], "localhost")
123
 
}
124
 
 
125
112
func Open(info *Info, opts DialOpts) (*State, error) {
126
113
        if len(info.Addrs) == 0 {
127
114
                return nil, fmt.Errorf("no API addresses to connect to")
137
124
        try := parallel.NewTry(0, nil)
138
125
        defer try.Kill()
139
126
        var addrs []string
140
 
        addrs = append(addrs, info.Addrs...)
141
 
        sort.Sort(LocalFirst(addrs))
 
127
        for _, addr := range info.Addrs {
 
128
                if strings.HasPrefix(addr, "localhost:") {
 
129
                        addrs = append(addrs, addr)
 
130
                        break
 
131
                }
 
132
        }
 
133
        if len(addrs) == 0 {
 
134
                addrs = info.Addrs
 
135
        }
142
136
        for _, addr := range addrs {
143
137
                err := dialWebsocket(addr, opts, pool, try)
144
138
                if err == parallel.ErrStopped {