Skip to content
This repository has been archived by the owner on Nov 9, 2023. It is now read-only.
/ anyport Public archive

Golang library to bind any available or random TCP port for listening

License

Notifications You must be signed in to change notification settings

thekondor/anyport

Repository files navigation

AnyPort GoDoc

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:

  anyPort, err := anyport.ListenInsecure("localhost")
  if nil != err {
      panic(err)
  }
  defer anyPort.Listener.Close()
  log.Printf("Incoming port: %d", anyPort.PortNumber)

Over TLS:

  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:

  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:

  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)

License

The library is released under the MIT license. See LICENSE file.

About

Golang library to bind any available or random TCP port for listening

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages