-
Notifications
You must be signed in to change notification settings - Fork 0
Examples
Hunter Dolan edited this page Jul 13, 2018
·
2 revisions
requester.go
This is an example of a requester.
package main
import (
"fmt"
"math"
"math/rand"
"time"
"github.com/BlueprintProject/GridWorker"
)
// Do something arbitrarily complicated
var numberOfStringsToHash = int64(math.Pow(2, 10))
var numberOfTimesToSHA1 = int64(math.Pow(2, 20))
var stringLength = 25
// Begin Random String Generation
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
func randStringList(n int64) []string {
// Seed rand for random string generation
// (PS this isn't really random)
rand.Seed(time.Now().UnixNano())
strList := make([]string, n)
for i := range strList {
b := make([]rune, stringLength)
for i := range b {
b[i] = letterRunes[rand.Intn(len(letterRunes))]
}
strList[i] = string(b)
}
return strList
}
// End Random String Code
func main() {
// Generate the string list
stringList := randStringList(numberOfStringsToHash)
// Allocate a new GridWorker Instance
w := gridworker.NewDistributedWorker()
// Start the server for workers to connect to
go w.StartServer("0.0.0.0:5231")
fmt.Println("Queing", len(stringList), "tasks")
for _, s := range stringList {
// Create a new message to send to node
message := w.NewMessage()
// Set the name of the task
message.Command = "sha1"
// Provide arguments
message.SetString("str", s)
message.SetInt64("num", numberOfTimesToSHA1)
// Add the task to the task queue
r := w.Run(message)
// Asynchronously Wait for Response
go func(r *gridworker.Reciept, s string) {
// Get the response
result := <-r.Response
// Get & Print the result
out := result.GetString("out")
fmt.Println(s, "=>", out)
}(r, s)
}
// Loop Endlessly so we don't quit
for {
time.Sleep(500 * time.Second)
}
}
This requester will randomly generate 1024 strings, to be hashed SHA1 2^20 times. This is a completely arbitrarily complicated example, but it shows a good proof of concept. The 1024 tasks can be distributed across multiple workers. Yet the result is piped out in the requesters console.
worker.go
The worker actually performs the work
package main
import (
"crypto/sha1"
"encoding/hex"
"time"
"github.com/BlueprintProject/GridWorker"
)
func sha1Action(context *gridworker.Context) {
// Get the input arguments
str := context.GetString("str")
num := context.GetInt64("num")
// Sha1 in a loop
res := []byte(str)
for i := int64(0); i < num; i++ {
sum := sha1.Sum(res)
res = sum[:]
}
// Get the output
out := hex.EncodeToString(res)
// Set the output in a message
response := context.NewMessage()
response.SetString("out", out)
// Send the output
context.Response <- response
}
// Create the task object
var sha1Task = &gridworker.Task{
ID: "sha1",
Action: sha1Action,
}
func main() {
// Create a new distributed worker with 4 worker local workers
// This means this node can perform 4 tasks at a time
// Usually, if the task is computationally intensive this will
// equal the number of CPU cores the machine has. However, different
// types of tasks will utilize resources differently.
worker := gridworker.NewDistributedWorkerWithLocalWorkers(4)
// Register the Sha1 task so that it can be performed
worker.RegisterTask(sha1Task)
// Connect to our requester server
worker.ConnectToServer("ws://0.0.0.0:5231/socket")
// Loop endlessly so we don't quit
for {
time.Sleep(500 * time.Second)
}
}