Permalink
Browse files

refine test connection logic

  • Loading branch information...
樊尚享
樊尚享 committed Jan 18, 2018
1 parent a4a7158 commit 67631d87fe73a1e858e154972f3e50e4dfa634dd
Showing with 58 additions and 35 deletions.
  1. +20 −0 LICENSE
  2. +38 −35 backends/metad/client.go
View
20 LICENSE
@@ -17,3 +17,23 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Copyright (c) 2018 Yunify
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
View
@@ -26,20 +26,6 @@ type Connection struct {
errTimes uint32
}
func (c *Connection) testConnection() error {
var err error
maxTime := 15 * time.Second
for i := 1 * time.Second; i < maxTime; i *= time.Duration(2) {
if _, err = c.makeMetaDataRequest("/"); err == nil {
return nil // OK
}
time.Sleep(i)
}
return err
}
func (c *Connection) makeMetaDataRequest(path string) ([]byte, error) {
req, err := http.NewRequest("GET", strings.Join([]string{c.url, path}, ""), nil)
if err != nil {
@@ -98,31 +84,48 @@ func NewMetadClient(backendNodes []string) (*Client, error) {
}
func (c *Client) selectConnection() error {
//random start
if c.current == nil {
rand.Seed(time.Now().Unix())
r := rand.Intn(c.connections.Len())
c.connections = c.connections.Move(r)
}
c.connections = c.connections.Next()
conn := c.connections.Value.(*Connection)
startConn := conn
err := conn.testConnection()
for err != nil {
log.Error("Connection to [%s], error: [%s]", conn.url, err.Error())
var err error
maxTime := 15 * time.Second
i := 1 * time.Second
for ; i < maxTime; i *= time.Duration(2) {
//random start
if c.current == nil {
rand.Seed(time.Now().Unix())
r := rand.Intn(c.connections.Len())
c.connections = c.connections.Move(r)
}
c.connections = c.connections.Next()
conn = c.connections.Value.(*Connection)
if conn == startConn {
return errors.New("Fail to connect any backend.")
conn := c.connections.Value.(*Connection)
startConn := conn
//iterate through connection
_, err = conn.makeMetaDataRequest("/")
for err != nil {
log.Error("Connection to [%s], error: [%s]", conn.url, err.Error())
c.connections = c.connections.Next()
conn = c.connections.Value.(*Connection)
if conn == startConn {
break
}
_, err = conn.makeMetaDataRequest("/")
}
err = conn.testConnection()
if err == nil {
//found available conn
if c.current != nil {
atomic.StoreUint32(&c.current.errTimes, 0)
}
c.current = conn
break
}
time.Sleep(i)
}
if c.current != nil {
atomic.StoreUint32(&c.current.errTimes, 0)
if i < maxTime {
log.Info("Using Metad URL: " + c.current.url)
return nil
} else {
return fmt.Errorf("Fail to connect any backend.")
}
c.current = conn
log.Info("Using Metad URL: " + c.current.url)
return nil
}
func (c *Client) GetValues(keys []string) (map[string]string, error) {

0 comments on commit 67631d8

Please sign in to comment.