Last active
December 27, 2015 10:39
-
-
Save astanway/7312364 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 app | |
import ( | |
"encoding/json" | |
"log" | |
"net/http" | |
"net/url" | |
"time" | |
) | |
func Henchman(schema interface{}, callback string, deadHenchmen chan int) { | |
// Pummeler stuff | |
maxPummelers := 100 | |
currentPummelers := 0 | |
pummelers := make(chan int, maxPummelers*2) | |
// Client dead stuff | |
maxFailedAttempts := 100 | |
currentFailedAttempts := 0 | |
failedAttempts := make(chan bool, maxFailedAttempts) | |
// Pummel! | |
for { | |
// Kill the Henchman if the client is dead | |
select { | |
case <-failedAttempts: | |
currentFailedAttempts += 1 | |
if currentFailedAttempts >= maxFailedAttempts { | |
log.Println(currentFailedAttempts) | |
deadHenchmen <- 1 | |
return | |
} | |
default: | |
} | |
// Launch a Pummeler maybe | |
select { | |
case val := <-pummelers: | |
currentPummelers += val | |
log.Println(currentPummelers) | |
if currentPummelers < maxPummelers { | |
go Pummeler(schema, callback, failedAttempts, pummelers) | |
} | |
default: | |
go Pummeler(schema, callback, failedAttempts, pummelers) | |
} | |
} | |
} | |
func Pummeler(schema interface{}, callback string, failedAttempts chan bool, pummelers chan int) { | |
pummelers <- 1 | |
// Format request | |
jsonEncoded, err := json.Marshal(schema) | |
if err != nil { | |
log.Println("Could not encode JSON") | |
return | |
} | |
// TODO: Generate unique data on the fly | |
// Format post request | |
values := make(url.Values) | |
values.Set("data", string(jsonEncoded)) | |
// Send data to client | |
resp, err := http.PostForm(callback, values) | |
if resp.StatusCode != 200 || err != nil { | |
failedAttempts <- true | |
return | |
} | |
pummelers <- -1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment