Skip to content

Examples

Hunter Dolan edited this page Jul 13, 2018 · 2 revisions

Examples

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)
	}
}

The output on the requester console will look something like the following

Queing 1024 tasks
lctvcWHrKMmKhSWECPxQOBQhk => e1291e3f286b6e87b5835c85b1906d24f65a8806
pokUFPdobWtZiVHMlPqpOVGQE => fc489bda16cf33af9dd1e29dda60b999dc352ebb
sFuPMTOJtQcHmbcQCgvJhwtNn => bd2727cf9b2cbdab5bc5e22255063d1f74462080
PLrDOWXbolXDvlHzSbXEsFBMP => 3fbc4939ea246b90cddab2fa4169a712344f03fb
YMRdyhXpWIBghPXWzOzcMbFzG => b84976df48e98559e02d6f5cf2ce6c64174062d8
dWZvMlTpswwCEkHkLtcTrRmOi => 700018030708aa89b438de0ef98dfbd673fd57ef
VlOxwAAbYqxIRIpfOgFboVHGG => 202d0b6d192b69d3d7e8c14cfa498b00f1c69faa
oOAehNklLViFFziCIGnTGMGXK => c9cc0f546cf02356808f4feb8be9893934b4a9a0
PlLmkrRkjGEEDkxPWRmhkHqNq => ab9673cc7d2d7f78a5585469957c5e23ec1595f0
hesPQgfaImwLONwRtEyrrSQzX => 7b0e56ab7704269f5798be6470a4b3a59627db92
eIOvisqpPGgWZYehoeNVkMhGr => d21d9e81fbde8d72c3e13222f8bfffb1eaf0c101
SAnCMYeJFJYTrCRnCvcfFuwZp => 2877efc83dc7ef50ddfe84d33b2a407f8a2ef181
Clone this wiki locally