Documentation
¶
Overview ¶
Example (Cancel) ¶
package main
import (
"fmt"
future "github.com/capitalone/go-future-context"
"time"
)
func main() {
inVal := 200
ThingThatTakesALongTimeToCalculate := func(inVal int) (int, error) {
//this does something but it's not that important
time.Sleep(5 * time.Second)
return inVal * 2, nil
}
f := future.New(func() (interface{}, error) {
return ThingThatTakesALongTimeToCalculate(inVal)
})
go func() {
time.Sleep(2 * time.Second)
f.Cancel()
}()
result, err := f.Get()
fmt.Println(result, err, f.IsCancelled())
}
Output: <nil> <nil> true
Example (Context) ¶
package main
import (
"context"
"fmt"
"time"
future "github.com/capitalone/go-future-context"
)
func main() {
inVal := 200
ThingThatTakesALongTimeToCalculate := func(inVal int) (int, error) {
//this does something but it's not that important
time.Sleep(5 * time.Second)
return inVal * 2, nil
}
ctx, cancelFunc := context.WithTimeout(context.Background(), 2*time.Second)
f := future.NewWithContext(ctx, func() (interface{}, error) {
return ThingThatTakesALongTimeToCalculate(inVal)
})
result, err := f.Get()
fmt.Println(result, err, f.IsCancelled())
cancelFunc()
}
Output: <nil> <nil> true
Example (Get) ¶
package main
import (
"fmt"
future "github.com/capitalone/go-future-context"
"time"
)
func main() {
inVal := 200
ThingThatTakesALongTimeToCalculate := func(inVal int) (int, error) {
//this does something but it's not that important
time.Sleep(5 * time.Second)
return inVal * 2, nil
}
f := future.New(func() (interface{}, error) {
return ThingThatTakesALongTimeToCalculate(inVal)
})
result, err := f.Get()
fmt.Println(result, err)
// second call, results are instantaneous
result, err = f.Get()
fmt.Println(result, err)
}
Output: 400 <nil> 400 <nil>
Example (GetUntil) ¶
package main
import (
"fmt"
"time"
future "github.com/capitalone/go-future-context"
)
func main() {
inVal := 200
ThingThatTakesALongTimeToCalculate := func(inVal int) (int, error) {
//this does something but it's not that important
time.Sleep(5 * time.Second)
return inVal * 2, nil
}
f := future.New(func() (interface{}, error) {
return ThingThatTakesALongTimeToCalculate(inVal)
})
// times out
result, timeout, err := f.GetUntil(2 * time.Second)
fmt.Println(result, timeout, err)
// completes before 10s passes
result, timeout, err = f.GetUntil(10 * time.Second)
fmt.Println(result, timeout, err)
// results are instantaneous
result, timeout, err = f.GetUntil(10 * time.Second)
fmt.Println(result, timeout, err)
}
Output: <nil> true <nil> 400 false <nil> 400 false <nil>
Example (Then) ¶
package main
import (
"fmt"
future "github.com/capitalone/go-future-context"
"time"
)
func main() {
inVal := 200
ThingThatTakesALongTimeToCalculate := func(inVal int) (int, error) {
//this does something but it's not that important
time.Sleep(5 * time.Second)
return inVal * 2, nil
}
f := future.New(func() (interface{}, error) {
return ThingThatTakesALongTimeToCalculate(inVal)
}).Then(func(i interface{}) (interface{}, error) {
return ThingThatTakesALongTimeToCalculate(i.(int))
})
result, err := f.Get()
fmt.Println(result, err)
// results are instantaneous
result, err = f.Get()
fmt.Println(result, err)
f2 := future.New(func() (interface{}, error) {
return ThingThatTakesALongTimeToCalculate(inVal)
}).Then(func(i interface{}) (interface{}, error) {
return ThingThatTakesALongTimeToCalculate(i.(int))
})
// times out during first step
result, timeout, err := f2.GetUntil(2 * time.Second)
fmt.Println(result, timeout, err)
f3 := future.New(func() (interface{}, error) {
return ThingThatTakesALongTimeToCalculate(inVal)
}).Then(func(i interface{}) (interface{}, error) {
return ThingThatTakesALongTimeToCalculate(i.(int))
})
// times out during second step
result, timeout, err = f3.GetUntil(7 * time.Second)
fmt.Println(result, timeout, err)
f4 := future.New(func() (interface{}, error) {
return ThingThatTakesALongTimeToCalculate(inVal)
}).Then(func(i interface{}) (interface{}, error) {
return ThingThatTakesALongTimeToCalculate(i.(int))
})
// completes both steps
result, timeout, err = f4.GetUntil(20 * time.Second)
fmt.Println(result, timeout, err)
}
Output: 800 <nil> 800 <nil> <nil> true <nil> <nil> true <nil> 800 false <nil>
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Interface ¶
type Interface interface {
// Cancel prevents a future that hasn't completed from returning a
// value. Any current or future calls to Get or GetUntil will return
// immediately.
//
// If the future has already completed or has already been
// cancelled, calling Cancel will do nothing.
// After a successful cancel, IsCancelled returns true.
//
// Calling Cancel on a future that has not completed does not stop the
// currently running function. However, any chained functions will not
// be run and the values returned by the current function are not accessible.
Cancel()
// IsCancelled indicates if a future terminated due to cancellation.
// If Cancel was called and the future's work was not completed, IsCancelled
// returns true. Otherwise, it returns false
IsCancelled() bool
// Get returns the values calculated by the future. It will pause until
// the future is cancelled or until the value is calculated.
// If Get is invoked multiple times, the same value will be returned each time.
// Subsequent calls to Get will return instantaneously.
//
// When the future is cancelled, nil is returned for both the value and the error.
Get() (interface{}, error)
// GetUntil waits for up to Duration d for the future to complete. If the
// future completes before the Duration completes, the value and error are returned
// and timeout is returned as false. If the Duration completes before the future
// returns, nil is returned for the value and the error and timeout is returned
// as true.
//
// When the future is cancelled, nil is returned for both the value and the error.
GetUntil(d time.Duration) (interface{}, bool, error)
// Then allows multiple function calls to be chained together into a single future.
// Each call is run in order, with the output of the previous call passed into
// the next function in the chain. If an error occurs at any step in the chain,
// processing ceases and the error is returned via Get or GetUntil.
//
// If Cancel is called before the chain completes, the currently running function
// will complete silently in the background and all unexecuted functions will
// not run.
Then(func(interface{}) (interface{}, error)) Interface
}
Interface represents a future. No concrete implementation is exposed; all access to a future is via this interface.
Click to show internal directories.
Click to hide internal directories.