Created
August 3, 2017 15:51
-
-
Save aryszka/f77007879adecf5b732e201c12ce9e49 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"log" | |
"math/rand" | |
"os" | |
"time" | |
) | |
/* spec | |
- take tasks from nextTask() | |
- process them with process() using N workers where N is an initial, fix parameter | |
- save the results of process() with saveResult() | |
- assume that process() can take long varying duration | |
- optimize for throughput | |
*/ | |
type ( | |
T struct{ value int } | |
R struct{ value string } | |
) | |
func process(t T) R { | |
time.Sleep(time.Duration(rand.Intn(500)) * time.Millisecond) | |
return R{fmt.Sprint(t.value * 2)} | |
} | |
func nextTask() T { return T{rand.Int()} } | |
func saveResult(r R) { println(r.value) } | |
/* eo spec */ | |
func dispatcher(n int) { | |
// implementation | |
} | |
func main() { | |
var n int | |
if _, err := fmt.Sscanf(os.Args[1], "%d", &n); err != nil { | |
log.Fatal(err) | |
} | |
dispatcher(n) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment