Skip to content

Instantly share code, notes, and snippets.

@PratikDeoghare
Created February 11, 2022 08:44
Show Gist options
  • Save PratikDeoghare/1f3f2f021e8f840af9ee4a73527efe1c to your computer and use it in GitHub Desktop.
Save PratikDeoghare/1f3f2f021e8f840af9ee4a73527efe1c to your computer and use it in GitHub Desktop.

Revisions

  1. PratikDeoghare created this gist Feb 11, 2022.
    45 changes: 45 additions & 0 deletions ports
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    package main

    import (
    "fmt"
    "sync"
    "time"
    )

    func main() {
    newPort := func() func() int {
    x := 4000
    var mu sync.Mutex
    return func() int {
    mu.Lock()
    x++
    y := x
    mu.Unlock()
    return y
    }
    }()
    _ = newPort

    localPort := make(chan uint16)
    go func() {
    for i := 5000; i < 60000; i++ {
    localPort <- uint16(i)
    }
    }()

    var m sync.Map
    for i := 0; i < 10000; i++ {
    go func() {
    //p := newPort()
    p := <-localPort
    _, ok := m.Load(p)
    if ok {
    panic(p)
    } else {
    m.Store(p, 1)
    }
    fmt.Println(p)
    }()
    }
    time.Sleep(time.Second * 10)
    }