This repository has been archived by the owner on Apr 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
535 additions
and
86 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package liteconfig | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
) | ||
|
||
// Modified from https://github.com/phayes/freeport/blob/95f893ade6f232a5f1511d61735d89b1ae2df543/freeport.go | ||
|
||
type portProvider struct { | ||
listeners []*net.TCPListener | ||
} | ||
|
||
// getFreePort asks the kernel for a free open port that is ready to use. | ||
func (p *portProvider) getFreePort() (int, error) { | ||
addr, err := net.ResolveTCPAddr("tcp", "127.0.0.1:0") | ||
if err != nil { | ||
if addr, err = net.ResolveTCPAddr("tcp6", "[::1]:0"); err != nil { | ||
panic(fmt.Sprintf("temporalite: failed to get free port: %v", err)) | ||
} | ||
} | ||
|
||
l, err := net.ListenTCP("tcp", addr) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
p.listeners = append(p.listeners, l) | ||
|
||
return l.Addr().(*net.TCPAddr).Port, nil | ||
} | ||
|
||
func (p *portProvider) mustGetFreePort() int { | ||
port, err := p.getFreePort() | ||
if err != nil { | ||
panic(err) | ||
} | ||
return port | ||
} | ||
|
||
func (p *portProvider) close() error { | ||
for _, l := range p.listeners { | ||
if err := l.Close(); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} |
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.