Skip to content

Commit

Permalink
Clarified the semantics of the TryGet funcs in futures - ADHOC TESTED
Browse files Browse the repository at this point in the history
	- was returning ok bool (true) to indicate NOT timedout
	- changed to timedout bool (true) to indicate timedout
  • Loading branch information
Joubin Houshyar committed Feb 13, 2012
1 parent 12d94cd commit f753430
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 93 deletions.
4 changes: 2 additions & 2 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -574,11 +574,11 @@ func heartbeatTask(c *asyncConnHdl, ctl workerCtl) (sig *interrupt_code, te *tas
if e != nil {
return nil, &taskStatus{reqerr, e}
}
stat, re, ok := response.future.(FutureBool).TryGet(1 * time.Second)
stat, re, timedout := response.future.(FutureBool).TryGet(1 * time.Second)
if re != nil {
log.Println("ERROR: Heartbeat recieved error response on PING")
return nil, &taskStatus{error_, re}
} else if !ok {
} else if timedout {
log.Println("Warning: Heartbeat timeout on get PING response.")
} else {
// flytrap
Expand Down
103 changes: 42 additions & 61 deletions future.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,27 +57,26 @@ func receive(c chan result) (v interface{}, error Error) {

// using a timer blocks on the channel until a result is received.
// or timeout period expires.
// if timedout, returns ok==false.
// if timedout, returns timedout==true.
// otherwise,
// If the received result reference's e (error) field is not null,
// it will return it.
//
// For now presumably a nil result is OK and if error is nil, the return
// value v is the intended result, even if nil.
// REVU(jh): rename ok to timeout and flip its semantics.

func tryReceive(c chan result, ns time.Duration) (v interface{}, error Error, ok bool) {
func tryReceive(c chan result, ns time.Duration) (v interface{}, error Error, timedout bool) {
// timer := NewTimer(ns)
select {
case fv := <-c:
ok = true
if fv.e != nil {
error = fv.e
} else {
v = fv.v
}
// case to := <-timer:
case to := <-time.After(ns):
timedout = true
if debug() {
log.Println("resultchan.TryGet() -- timedout waiting for futurevaluechan | timeout: ", to)
}
Expand All @@ -97,7 +96,7 @@ func tryReceive(c chan result, ns time.Duration) (v interface{}, error Error, ok
// Future? references can only be used until a value, or an error is obtained.
// If a value is obtained from a Future? reference, any further calls to Get()
// will block indefinitely. It is OK, of course, to use TryGet(..) repeatedly
// until it returns with a true 'ok' return out param.
// until it returns with a false 'timedout' return out param.

// FutureResult
//
Expand All @@ -113,29 +112,26 @@ type FutureBytes interface {
// onError (Error);
set([]byte)
Get() (vale []byte, error Error)
TryGet(timeoutnano time.Duration) (value []byte, error Error, ok bool)
TryGet(timeoutnano time.Duration) (value []byte, error Error, timedout bool)
}
type _byteslicefuture chan result

func newFutureBytes() FutureBytes { return make(_byteslicefuture, 1) }
func (fvc _byteslicefuture) onError(e Error) { send(fvc, nil, e) }
func (fvc _byteslicefuture) set(v []byte) { send(fvc, v, nil) }
func (fvc _byteslicefuture) Get() (v []byte, error Error) {
func (fvc _byteslicefuture) Get() ([]byte, Error) {
gv, err := receive(fvc)
if err != nil {
return nil, err
}
return gv.([]byte), err
}
func (fvc _byteslicefuture) TryGet(ns time.Duration) (v []byte, error Error, ok bool) {
gv, err, ok := tryReceive(fvc, ns)
if !ok {
return nil, nil, ok
func (fvc _byteslicefuture) TryGet(ns time.Duration) ([]byte, Error, bool) {
gv, err, timedout := tryReceive(fvc, ns)
if timedout || err != nil {
return nil, err, timedout
}
if err != nil {
return nil, err, ok
}
return gv.([]byte), err, ok
return gv.([]byte), err, timedout
}

// FutureBytesArray (for [][]byte)
Expand All @@ -144,7 +140,7 @@ type FutureBytesArray interface {
// onError (Error);
set([][]byte)
Get() (vale [][]byte, error Error)
TryGet(timeoutnano time.Duration) (value [][]byte, error Error, ok bool)
TryGet(timeoutnano time.Duration) (value [][]byte, error Error, timedout bool)
}
type _bytearrayslicefuture chan result

Expand All @@ -162,15 +158,12 @@ func (fvc _bytearrayslicefuture) Get() (v [][]byte, error Error) {
}
return gv.([][]byte), err
}
func (fvc _bytearrayslicefuture) TryGet(ns time.Duration) (v [][]byte, error Error, ok bool) {
gv, err, ok := tryReceive(fvc, ns)
if !ok {
return nil, nil, ok
}
if err != nil {
return nil, err, ok
func (fvc _bytearrayslicefuture) TryGet(ns time.Duration) ([][]byte, Error, bool) {
gv, err, timedout := tryReceive(fvc, ns)
if timedout || err != nil {
return nil, err, timedout
}
return gv.([][]byte), err, ok
return gv.([][]byte), err, timedout
}

// FutureBool
Expand All @@ -179,7 +172,7 @@ type FutureBool interface {
// onError (Error);
set(bool)
Get() (val bool, error Error)
TryGet(timeoutnano time.Duration) (value bool, error Error, ok bool)
TryGet(timeoutnano time.Duration) (value bool, error Error, timedout bool)
}
type _boolfuture chan result

Expand All @@ -193,15 +186,12 @@ func (fvc _boolfuture) Get() (v bool, error Error) {
}
return gv.(bool), err
}
func (fvc _boolfuture) TryGet(ns time.Duration) (v bool, error Error, ok bool) {
gv, err, ok := tryReceive(fvc, ns)
if !ok {
return false, nil, ok
func (fvc _boolfuture) TryGet(ns time.Duration) (bool, Error, bool) {
gv, err, timedout := tryReceive(fvc, ns)
if timedout || err != nil {
return false, err, timedout
}
if err != nil {
return false, err, ok
}
return gv.(bool), err, ok
return gv.(bool), err, timedout
}

// FutureString
Expand All @@ -210,7 +200,7 @@ type FutureString interface {
// onError (execErr Error);
set(v string)
Get() (string, Error)
TryGet(timeoutnano time.Duration) (value string, error Error, ok bool)
TryGet(timeoutnano time.Duration) (value string, error Error, timedout bool)
}
type _futurestring chan result

Expand All @@ -224,15 +214,12 @@ func (fvc _futurestring) Get() (v string, error Error) {
}
return gv.(string), err
}
func (fvc _futurestring) TryGet(ns time.Duration) (v string, error Error, ok bool) {
gv, err, ok := tryReceive(fvc, ns)
if !ok {
return "", nil, ok
func (fvc _futurestring) TryGet(ns time.Duration) (string, Error, bool) {
gv, err, timedout := tryReceive(fvc, ns)
if timedout || err != nil {

This comment has been minimized.

Copy link
@alphazero

alphazero Feb 13, 2012

Owner

use default value here.

return "", err, timedout
}
if err != nil {
return "", err, ok
}
return gv.(string), err, ok
return gv.(string), err, timedout
}

// FutureInt64
Expand All @@ -241,7 +228,7 @@ type FutureInt64 interface {
// onError (execErr Error);
set(v int64)
Get() (int64, Error)
TryGet(timeoutnano time.Duration) (value int64, error Error, ok bool)
TryGet(timeoutnano time.Duration) (value int64, error Error, timedout bool)
}
type _futureint64 chan result

Expand All @@ -255,22 +242,19 @@ func (fvc _futureint64) Get() (v int64, error Error) {
}
return gv.(int64), err
}
func (fvc _futureint64) TryGet(ns time.Duration) (v int64, error Error, ok bool) {
gv, err, ok := tryReceive(fvc, ns)
if !ok {
return -1, nil, ok
}
if err != nil {
return -1, err, ok
func (fvc _futureint64) TryGet(ns time.Duration) (int64, Error, bool) {
gv, err, timedout := tryReceive(fvc, ns)
if timedout || err != nil {
return 0, err, timedout
}
return gv.(int64), err, ok
return gv.(int64), err, timedout
}

// FutureFloat64
//
type FutureFloat64 interface {
Get() (float64, Error)
TryGet(timeoutnano time.Duration) (v float64, error Error, ok bool)
TryGet(timeoutnano time.Duration) (v float64, error Error, timedout bool)
}
type _futurefloat64 struct {
future FutureBytes
Expand All @@ -287,14 +271,11 @@ func (fvc _futurefloat64) Get() (v float64, error Error) {
v, err = Btof64(gv)
return v, nil
}
func (fvc _futurefloat64) TryGet(ns time.Duration) (v float64, error Error, ok bool) {
gv, err, ok := fvc.future.TryGet(ns)
if !ok {
return 0, nil, ok
func (fvc _futurefloat64) TryGet(ns time.Duration) (float64, Error, bool) {
gv, err, timedout := fvc.future.TryGet(ns)
if timedout || err != nil {
return float64(0), err, timedout

This comment has been minimized.

Copy link
@alphazero

alphazero Feb 13, 2012

Owner

use default value here

}
if err != nil {
return 0, err, ok
}
v, err = Btof64(gv)
return v, nil, ok
v, err := Btof64(gv)
return v, nil, timedout
}
49 changes: 19 additions & 30 deletions redisfutures.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import "time"
//
type FutureKeys interface {
Get() ([]string, Error)
TryGet(timeoutnano time.Duration) (keys []string, error Error, ok bool)
TryGet(timeoutnano time.Duration) (keys []string, error Error, timedout bool)
}
type _futurekeys struct {
future FutureBytes
Expand All @@ -37,23 +37,19 @@ func (fvc _futurekeys) Get() (v []string, error Error) {
v = convAndSplit(gv)
return v, nil
}
func (fvc _futurekeys) TryGet(ns time.Duration) (v []string, error Error, ok bool) {
gv, err, ok := fvc.future.TryGet(ns)
if !ok {
return nil, nil, ok
func (fvc _futurekeys) TryGet(ns time.Duration) ([]string, Error, bool) {
gv, err, timedout := fvc.future.TryGet(ns)
if timedout || err != nil {
return nil, err, timedout
}
if err != nil {
return nil, err, ok
}
v = convAndSplit(gv)
return v, nil, ok
return convAndSplit(gv), nil, timedout
}

// FutureInfo
//
type FutureInfo interface {
Get() (map[string]string, Error)
TryGet(timeoutnano time.Duration) (keys map[string]string, error Error, ok bool)
TryGet(timeoutnano time.Duration) (keys map[string]string, error Error, timedout bool)
}
type _futureinfo struct {
future FutureBytes
Expand All @@ -70,23 +66,19 @@ func (fvc _futureinfo) Get() (v map[string]string, error Error) {
v = parseInfo(gv)
return v, nil
}
func (fvc _futureinfo) TryGet(ns time.Duration) (v map[string]string, error Error, ok bool) {
gv, err, ok := fvc.future.TryGet(ns)
if !ok {
return nil, nil, ok
}
if err != nil {
return nil, err, ok
func (fvc _futureinfo) TryGet(ns time.Duration) (map[string]string, Error, bool) {
gv, err, timedout := fvc.future.TryGet(ns)
if timedout || err != nil {
return nil, err, timedout
}
v = parseInfo(gv)
return v, nil, ok
return parseInfo(gv), nil, timedout
}

// FutureKeyType
//
type FutureKeyType interface {
Get() (KeyType, Error)
TryGet(timeoutnano time.Duration) (keys KeyType, error Error, ok bool)
TryGet(timeoutnano time.Duration) (keys KeyType, error Error, timedout bool)
}
type _futurekeytype struct {
future FutureString
Expand All @@ -103,14 +95,11 @@ func (fvc _futurekeytype) Get() (v KeyType, error Error) {
v = GetKeyType(gv)
return v, nil
}
func (fvc _futurekeytype) TryGet(ns time.Duration) (v KeyType, error Error, ok bool) {
gv, err, ok := fvc.future.TryGet(ns)
if !ok {
return RT_NONE, nil, ok
}
if err != nil {
return RT_NONE, err, ok
func (fvc _futurekeytype) TryGet(ns time.Duration) (KeyType, Error, bool) {
gv, err, timedout := fvc.future.TryGet(ns)
if timedout || err != nil {
var defv KeyType
return defv, err, timedout
}
v = GetKeyType(gv)
return v, nil, ok
return GetKeyType(gv), nil, timedout
}

0 comments on commit f753430

Please sign in to comment.