Skip to content

Instantly share code, notes, and snippets.

@aryszka
Created August 3, 2017 15:51
Show Gist options
  • Save aryszka/f77007879adecf5b732e201c12ce9e49 to your computer and use it in GitHub Desktop.
Save aryszka/f77007879adecf5b732e201c12ce9e49 to your computer and use it in GitHub Desktop.
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