Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/thekondor/anyport
Golang library to bind any available or random TCP port for listening
https://github.com/thekondor/anyport
go microservices tcp-port tcp-server
Last synced: 3 months ago
JSON representation
Golang library to bind any available or random TCP port for listening
- Host: GitHub
- URL: https://github.com/thekondor/anyport
- Owner: thekondor
- License: mit
- Created: 2019-01-22T11:41:38.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2019-10-06T16:26:21.000Z (over 5 years ago)
- Last Synced: 2023-07-27T22:43:07.637Z (over 1 year ago)
- Topics: go, microservices, tcp-port, tcp-server
- Language: Go
- Size: 5.86 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# AnyPort [![GoDoc](https://godoc.org/github.com/thekondor/anyport?status.svg)](http://godoc.org/github.com/thekondor/anyport/)
`anyport` is a `Go` package to provide with a tool to bind any available or random port to listen for incoming TCP connections. The use case does make sense when a started (micro)service should not be pinned to a fixed TCP port but could be found through Service Discovery (like Consul) endpoints.
# Usage
## Random TCP Port (any range)
Plain TCP:
```go
anyPort, err := anyport.ListenInsecure("localhost")
if nil != err {
panic(err)
}
defer anyPort.Listener.Close()
log.Printf("Incoming port: %d", anyPort.PortNumber)
```Over TLS:
```go
tlsConfig := tls.Config{...}
anyPort, err := anyport.ListenSecure("localhost", &tlsConfig)
if nil != err {
panic(err)
}
defer anyPort.Listener.Close()
log.Printf("Incoming TLS port: %d", anyPort.PortNumber)
```## Random TCP port (in a fixed range)
Plain TCP:
```go
anyPort, err := anyport.ListenInsecure("localhost:3000-5000")
if nil != err {
panic(err)
}
defer anyPort.Listener.Close()
log.Printf("Incoming port: %d", anyPort.PortNumber)
```Over TLS:
```go
tlsConfig := tls.Config{...}
anyPort, err := anyport.ListenSecure("localhost:3000-5000", &tlsConfig)
if nil != err {
panic(err)
}
defer anyPort.Listener.Close()
log.Printf("Incoming TLS port: %d", anyPort.PortNumber)
```
# LicenseThe library is released under the MIT license. See LICENSE file.