Created
January 6, 2016 20:36
-
-
Save eapache/81162d4420dd6b01d533 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 main | |
import ( | |
"bufio" | |
"encoding/json" | |
"flag" | |
"fmt" | |
"net/http" | |
"net/url" | |
"os" | |
"strconv" | |
) | |
var messages chan string | |
// reads json and submits stats to the server | |
func main() { | |
var lines = flag.String("lines", "1", "the number of lines") | |
flag.Parse() | |
var foo int | |
var bar int | |
baz := 0 | |
messages = make(chan string) | |
go work() | |
real_lines, err := strconv.Atoi(*lines) // get the real lines | |
if err == nil { | |
reader := bufio.NewReader(os.Stdin) | |
for i := 0; i < real_lines; i++ { | |
var object map[string]interface{} | |
object = make(map[string]interface{}) | |
textLine, _ := reader.ReadString('\n') | |
json.Unmarshal([]byte(textLine), &object) | |
realType := object["type"].(string) | |
if realType == "foo" { | |
foo++ | |
messages <- realType | |
} else if realType == "bar" { | |
bar++ | |
messages <- realType | |
} else if realType == "baz" { | |
baz++ | |
messages <- realType | |
} | |
} | |
close(messages) | |
} | |
fmt.Println("Processed", foo, "foos") | |
fmt.Println("Processed", bar, "bars") | |
fmt.Println("Processed", baz, "bazs") | |
} | |
// do this in a goroutine so that slow servers don't take us down | |
func work() { | |
for { | |
klass, notClosed := <-messages | |
if notClosed { | |
if klass == "foo" { | |
resp, _ := http.PostForm("http://example.com/form", url.Values{"key": {"foo"}}) | |
resp.Body.Close() | |
} else if klass == "bar" { | |
resp, _ := http.PostForm("http://example.com/form", url.Values{"key": {"bar"}}) | |
resp.Body.Close() | |
} else if klass == "baz" { | |
resp, _ := http.PostForm("http://example.com/form", url.Values{"key": {"baz"}}) | |
resp.Body.Close() | |
} | |
} else { | |
panic("shutting down") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment