simplify and multiple retry

This commit is contained in:
Barak Michener 2014-10-08 21:53:15 -04:00
parent dbac2e8f15
commit 9b35ca3a52

View File

@ -108,7 +108,7 @@ func TestCheckCluster(t *testing.T) {
c := &clientWithResp{rs: rs} c := &clientWithResp{rs: rs}
d := discovery{cluster: cluster, id: 1, c: c} d := discovery{cluster: cluster, id: 1, c: c}
cRetry := &clientWithRetry{} cRetry := &clientWithRetry{failTimes: 2}
cRetry.rs = rs cRetry.rs = rs
dRetry := discovery{cluster: cluster, id: 1, c: cRetry, timeoutTimescale: time.Millisecond * 2} dRetry := discovery{cluster: cluster, id: 1, c: cRetry, timeoutTimescale: time.Millisecond * 2}
@ -199,9 +199,10 @@ func TestWaitNodes(t *testing.T) {
}, },
}) })
} }
cRetry := &clientWithRetry{} cRetry := &clientWithResp{
cRetry.rs = retryScanResp rs: retryScanResp,
cRetry.w = &watcherWithRetry{tt.rs, false} w: &watcherWithRetry{rs: tt.rs, failTimes: 2},
}
dRetry := &discovery{ dRetry := &discovery{
cluster: "1000", cluster: "1000",
c: cRetry, c: cRetry,
@ -312,7 +313,7 @@ func (c *clientWithResp) Get(key string) (*client.Response, error) {
return &client.Response{}, client.ErrKeyNoExist return &client.Response{}, client.ErrKeyNoExist
} }
r := c.rs[0] r := c.rs[0]
c.rs = c.rs[1:] c.rs = append(c.rs[1:], r)
return r, nil return r, nil
} }
@ -369,12 +370,13 @@ func (w *watcherWithErr) Next() (*client.Response, error) {
// Fails every other time // Fails every other time
type clientWithRetry struct { type clientWithRetry struct {
clientWithResp clientWithResp
haveFailed bool failCount int
failTimes int
} }
func (c *clientWithRetry) Create(key string, value string, ttl time.Duration) (*client.Response, error) { func (c *clientWithRetry) Create(key string, value string, ttl time.Duration) (*client.Response, error) {
if !c.haveFailed { if c.failCount < c.failTimes {
c.haveFailed = true c.failCount++
return nil, client.ErrTimeout return nil, client.ErrTimeout
} }
if len(c.rs) == 0 { if len(c.rs) == 0 {
@ -386,26 +388,22 @@ func (c *clientWithRetry) Create(key string, value string, ttl time.Duration) (*
} }
func (c *clientWithRetry) Get(key string) (*client.Response, error) { func (c *clientWithRetry) Get(key string) (*client.Response, error) {
if !c.haveFailed { if c.failCount < c.failTimes {
c.haveFailed = true c.failCount++
return nil, client.ErrTimeout return nil, client.ErrTimeout
} }
if len(c.rs) == 0 { return c.clientWithResp.Get(key)
return &client.Response{}, client.ErrKeyNoExist
}
r := c.rs[0]
c.rs = c.rs[1:]
return r, nil
} }
type watcherWithRetry struct { type watcherWithRetry struct {
rs []*client.Response rs []*client.Response
haveFailed bool failCount int
failTimes int
} }
func (w *watcherWithRetry) Next() (*client.Response, error) { func (w *watcherWithRetry) Next() (*client.Response, error) {
if !w.haveFailed { if w.failCount < w.failTimes {
w.haveFailed = true w.failCount++
return nil, client.ErrTimeout return nil, client.ErrTimeout
} }
if len(w.rs) == 0 { if len(w.rs) == 0 {