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.
ways of generating ports
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)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment