Skip to content

Instantly share code, notes, and snippets.

@heppu
Last active December 9, 2016 08:21
Show Gist options
  • Select an option

  • Save heppu/adf35c4906cc6c4530fadbf5f3e35784 to your computer and use it in GitHub Desktop.

Select an option

Save heppu/adf35c4906cc6c4530fadbf5f3e35784 to your computer and use it in GitHub Desktop.

Revisions

  1. heppu revised this gist Dec 9, 2016. 1 changed file with 2 additions and 4 deletions.
    6 changes: 2 additions & 4 deletions limitter.go
    Original file line number Diff line number Diff line change
    @@ -13,7 +13,7 @@ func main() {

    var wg sync.WaitGroup
    workers := make(chan struct{}, Limit)

    doWork := func(i int, j string) {
    defer wg.Done()
    time.Sleep(2 * time.Second)
    @@ -23,13 +23,11 @@ func main() {

    for j := 0; j < 15; j++ {
    work := string(rune(97 + j))

    log.Printf("Work %s enqueued\n", work)

    workers <- struct{}{}
    wg.Add(1)

    go doWork(j, work)
    }

    wg.Wait()
    }
  2. heppu revised this gist May 23, 2016. 1 changed file with 4 additions and 7 deletions.
    11 changes: 4 additions & 7 deletions limitter.go
    Original file line number Diff line number Diff line change
    @@ -11,28 +11,25 @@ const Limit = 5
    func main() {
    log.SetFlags(log.Ltime) // format log output hh:mm:ss

    wg := sync.WaitGroup{}
    queue := make(chan struct{}, Limit)
    var wg sync.WaitGroup
    workers := make(chan struct{}, Limit)

    doWork := func(i int, j string) {
    defer wg.Done()
    time.Sleep(2 * time.Second)
    log.Printf("Worker %d working on %s\n", i, j)
    <-queue
    <-workers
    }

    for j := 0; j < 15; j++ {
    work := string(rune(97 + j))

    log.Printf("Work %s enqueued\n", work)

    workers <- struct{}{}
    wg.Add(1)
    queue <- struct{}{}

    go doWork(j, work)
    }

    close(queue)

    wg.Wait()
    }
  3. heppu revised this gist May 23, 2016. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions limitter.go
    Original file line number Diff line number Diff line change
    @@ -12,7 +12,7 @@ func main() {
    log.SetFlags(log.Ltime) // format log output hh:mm:ss

    wg := sync.WaitGroup{}
    queue := make(chan bool, Limit)
    queue := make(chan struct{}, Limit)

    doWork := func(i int, j string) {
    defer wg.Done()
    @@ -27,7 +27,7 @@ func main() {
    log.Printf("Work %s enqueued\n", work)

    wg.Add(1)
    queue <- false
    queue <- struct{}{}

    go doWork(j, work)
    }
  4. heppu revised this gist May 23, 2016. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions limitter.go
    Original file line number Diff line number Diff line change
    @@ -12,7 +12,7 @@ func main() {
    log.SetFlags(log.Ltime) // format log output hh:mm:ss

    wg := sync.WaitGroup{}
    queue := make(chan interface{}, Limit)
    queue := make(chan bool, Limit)

    doWork := func(i int, j string) {
    defer wg.Done()
    @@ -27,12 +27,12 @@ func main() {
    log.Printf("Work %s enqueued\n", work)

    wg.Add(1)
    queue <- nil
    queue <- false

    go doWork(j, work)
    }

    close(queue)

    wg.Wait()
    }
    }
  5. heppu revised this gist May 23, 2016. No changes.
  6. heppu created this gist May 23, 2016.
    38 changes: 38 additions & 0 deletions limitter.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    package main

    import (
    "log"
    "sync"
    "time"
    )

    const Limit = 5

    func main() {
    log.SetFlags(log.Ltime) // format log output hh:mm:ss

    wg := sync.WaitGroup{}
    queue := make(chan interface{}, Limit)

    doWork := func(i int, j string) {
    defer wg.Done()
    time.Sleep(2 * time.Second)
    log.Printf("Worker %d working on %s\n", i, j)
    <-queue
    }

    for j := 0; j < 15; j++ {
    work := string(rune(97 + j))

    log.Printf("Work %s enqueued\n", work)

    wg.Add(1)
    queue <- nil

    go doWork(j, work)
    }

    close(queue)

    wg.Wait()
    }