Skip to content

Commit

Permalink
Merge pull request #7 from yunify/refine_test_connection
Browse files Browse the repository at this point in the history
refine test connection logic
  • Loading branch information
Martin@qingcloud authored Jan 19, 2018
2 parents a4a7158 + b99dff9 commit 9713b9d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 24 deletions.
20 changes: 20 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -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.
50 changes: 26 additions & 24 deletions backends/metad/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -98,6 +84,27 @@ func NewMetadClient(backendNodes []string) (*Client, error) {
}

func (c *Client) selectConnection() error {
maxTime := 15 * time.Second
i := 1 * time.Second
for ; i < maxTime; i *= time.Duration(2) {
if conn, err := c.testConnection(); err == nil {
//found available conn
if c.current != nil {
atomic.StoreUint32(&c.current.errTimes, 0)
}
c.current = conn
break
}
time.Sleep(i)
}
if i >= maxTime {
return fmt.Errorf("fail to connect any backend.")
}
log.Info("Using Metad URL: " + c.current.url)
return nil
}

func (c *Client) testConnection() (*Connection, error) {
//random start
if c.current == nil {
rand.Seed(time.Now().Unix())
Expand All @@ -107,22 +114,17 @@ func (c *Client) selectConnection() error {
c.connections = c.connections.Next()
conn := c.connections.Value.(*Connection)
startConn := conn
err := conn.testConnection()
_, err := conn.makeMetaDataRequest("/")
for err != nil {
log.Error("Connection to [%s], error: [%s]", conn.url, err.Error())
log.Error("connection to [%s], error: [%v]", conn.url, err)
c.connections = c.connections.Next()
conn = c.connections.Value.(*Connection)
if conn == startConn {
return errors.New("Fail to connect any backend.")
break
}
err = conn.testConnection()
}
if c.current != nil {
atomic.StoreUint32(&c.current.errTimes, 0)
_, err = conn.makeMetaDataRequest("/")
}
c.current = conn
log.Info("Using Metad URL: " + c.current.url)
return nil
return conn, err
}

func (c *Client) GetValues(keys []string) (map[string]string, error) {
Expand Down

0 comments on commit 9713b9d

Please sign in to comment.