Skip to content

Instantly share code, notes, and snippets.

@RichyHBM
Last active August 21, 2016 15:38
Show Gist options
  • Save RichyHBM/f22f873635a044549b5cbeb6449f82f6 to your computer and use it in GitHub Desktop.
Save RichyHBM/f22f873635a044549b5cbeb6449f82f6 to your computer and use it in GitHub Desktop.
Simple goroutines wrapper to provide Scala-like futures functionality in Golang. Adapted from: http://labs.strava.com/blog/futures-in-golang/
package main
import (
"errors"
"time"
)
type FutureFunction func() (interface{}, error)
type FutureResult func(timeout ...time.Duration) (interface{}, error)
func Future(f FutureFunction) FutureResult {
var result interface{}
var err error
c := make(chan struct{}, 1)
go func() {
defer close(c)
result, err = f()
}()
return func(timeout ...time.Duration) (interface{}, error) {
if len(timeout) == 1 {
select {
case <-c:
return result, err
case <-time.After(timeout[0]):
return nil, errors.New("Future timeout")
}
} else {
<-c
return result, err
}
}
}
package main
import (
"fmt"
"time"
)
func DoLongProcessing() (interface{}, error) {
time.Sleep(time.Second * 5)
return 123, nil
}
func printResults(res interface{}, err error, start time.Duration, waiting time.Duration) {
if err != nil {
fmt.Printf("Error: '%s', %v since start, total wait time for this function: %v\n", err, start, waiting)
} else {
fmt.Printf("Success: '%d', %v since start, total wait time for this function: %v\n", res, start, waiting)
}
}
func main() {
start := time.Now()
future := Future(DoLongProcessing)
waiting := time.Now()
var res, err = future(time.Millisecond * 500)
printResults(res, err, time.Since(start), time.Since(waiting))
waiting = time.Now()
res, err = future()
printResults(res, err, time.Since(start), time.Since(waiting))
waiting = time.Now()
res, err = future()
printResults(res, err, time.Since(start), time.Since(waiting))
}
> go run .\main.go .\futures.go
Error: 'Future timeout', 500.1081ms since start, total wait time for this function: 500.1081ms
Success: '123', 5.0002842s since start, total wait time for this function: 4.5001761s
Success: '123', 5.0002842s since start, total wait time for this function: 0s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment