Skip to content

Instantly share code, notes, and snippets.

@xreiju
Last active March 21, 2017 11:42
Show Gist options
  • Save xreiju/6483bda812594b1c450132bf7e8fb0bd to your computer and use it in GitHub Desktop.
Save xreiju/6483bda812594b1c450132bf7e8fb0bd to your computer and use it in GitHub Desktop.
import std.stdio : writeln;
import std.json : parseJSON, JSONValue;
import std.net.curl : get;
import std.parallelism : parallel;
import core.atomic : atomicOp;
import std.array : uninitializedArray;
void main()
{
immutable string KNH_URL = "http://api.knh.uno:8080/v1/challenge.json";
immutable int DESIRED_SUCCESS_COUNT = 10;
shared int count = 0;
shared int all_count = 0;
shared int[] success_times = [];
bool is_burned = false;
foreach(i; uninitializedArray!(int[])(20).parallel()) {
while(!is_burned) {
atomicOp!"+="(count, 1);
atomicOp!"+="(all_count, 1);
auto content = get(KNH_URL);
writeln(content);
JSONValue j = parseJSON(content);
if(j["state"].str == "FAILED") {
if(count % 100 == 0) {
writeln("waiting... : ", count);
}
} else {
writeln("SOMETHING HAPPENED: ", count, content);
success_times ~= count;
count = 0;
if(success_times.length == DESIRED_SUCCESS_COUNT) {
is_burned = true;
}
}
}
}
writeln("result: ", success_times, all_count);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment