Created
February 11, 2022 08:44
-
-
Save PratikDeoghare/1f3f2f021e8f840af9ee4a73527efe1c to your computer and use it in GitHub Desktop.
ways of generating ports
This file contains hidden or 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 ( | |
| "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