Skip to content

Instantly share code, notes, and snippets.

@PotHix
Last active December 28, 2015 08:59
Show Gist options
  • Select an option

  • Save PotHix/7475316 to your computer and use it in GitHub Desktop.

Select an option

Save PotHix/7475316 to your computer and use it in GitHub Desktop.

Revisions

  1. PotHix revised this gist Nov 14, 2013. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions namesp
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,7 @@
    type Result struct {
    Status int
    }

    type Metric struct {
    Json []byte
    Name string
  2. PotHix revised this gist Nov 14, 2013. 1 changed file with 9 additions and 0 deletions.
    9 changes: 9 additions & 0 deletions namesp
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,12 @@
    type Metric struct {
    Json []byte
    Name string
    }

    var (
    rc = redis.New("localhost:6379")
    )

    func retrieveJson(url string) []byte {
    resp, err := http.Get(url)
    if err != nil {
  3. PotHix created this gist Nov 14, 2013.
    31 changes: 31 additions & 0 deletions main
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    const NumberOfGoroutines = 15

    func main() {
    var wg sync.WaitGroup

    channel := make(chan string, NumberOfGoroutines)
    redisch := make(chan *namesp.Metric, 3)

    for i := 0; i < NumberOfGoroutines; i++ {
    wg.Add(1)
    go namesp.CacheMetric(channel, redisch, &wg, i)
    }

    // Get the value back and store on redis
    for i := 0; i < NumberOfGoroutines/5; i++ {
    go namesp.StoreValue(redisch)
    }

    runtime.GOMAXPROCS(2)

    for _, server := range audit.CacheMachines() { // The name of some servers
    metrics := namesp.GetMetrics() // just a list of metrics to use as part of the url
    for _, metric := range metrics {
    channel <- fmt.Sprintf(metric, server)
    }
    }

    close(channel)

    wg.Wait()
    }
    54 changes: 54 additions & 0 deletions namesp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,54 @@
    func retrieveJson(url string) []byte {
    resp, err := http.Get(url)
    if err != nil {
    panic("Could not get the URL: " + url)
    }

    body, err := ioutil.ReadAll(resp.Body)
    defer resp.Body.Close()

    if err != nil {
    panic("Could not read the response body")
    }

    return body
    }


    func cacheResource(full_metric string, out chan *Metric) {
    before, after := formattedTime()

    url := fmt.Sprintf(NAMESP_URL, full_metric, before, after)

    fmt.Println("processing: " + url)

    var metric *Metric
    metric = new(Metric)
    metric.Json = retrieveJson(url)
    metric.Name = full_metric

    out <- metric
    }

    func CacheMetric(server chan string, out chan *Metric, wg *sync.WaitGroup, i int) {
    for metric := range server {
    starting := int(time.Now().Unix())
    cacheResource(metric, out)
    ending := int(time.Now().Unix())
    fmt.Println(strconv.Itoa(ending-starting) + ": [" + strconv.Itoa(i) + "] done processing " + metric)
    }
    wg.Done()
    }

    func StoreValue(out chan *Metric) {
    for metric := range out {
    result := &Result{}
    json.Unmarshal(metric.Json, &result)

    if result.Status == 200 {
    rc.Set(metric.Name, string(metric.Json))
    } else {
    fmt.Println("Got the status '" + strconv.Itoa(result.Status) + "' when looking for " + metric.Name)
    }
    }
    }