dnssrv

package
v2.0.0-...-9629169 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 29, 2023 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package dnssrv defines some implementations for a dns based service discovery

Index

Examples

Constants

View Source
const Namespace = "dns"

Namespace is the key for the dns sd module

Variables

View Source
var DefaultLookup = net.LookupSRV

DefaultLookup is the function used for the DNS resolution

View Source
var TTL = 30 * time.Second

TTL is the duration of the cached data

Functions

func New

func New(name string) sd.Subscriber

New creates a DNS subscriber with the default values

func NewDetailed

func NewDetailed(name string, lookup lookup, ttl time.Duration) sd.Subscriber

NewDetailed creates a DNS subscriber with the received values

Example
srvSet := []*net.SRV{
	{
		Port:   90,
		Target: "foobar",
		Weight: 2,
	},
	{
		Port:   90,
		Target: "127.0.0.1",
		Weight: 2,
	},
	{
		Port:   80,
		Target: "127.0.0.1",
		Weight: 2,
	},
	{
		Port:   81,
		Target: "127.0.0.1",
		Weight: 4,
	},
	{
		Port:     82,
		Target:   "127.0.0.1",
		Weight:   10,
		Priority: 2,
	},
	{
		Port:   83,
		Target: "127.0.0.1",
	},
}
lookupFunc := func(service, proto, name string) (cname string, addrs []*net.SRV, err error) {
	return "cname", srvSet, nil
}

s := NewDetailed("some.example.tld", lookupFunc, 10*time.Second)
hosts, err := s.Hosts()
if err != nil {
	fmt.Println("Getting the hosts:", err.Error())
	return
}
for _, h := range hosts {
	fmt.Println(h)
}
Output:

http://127.0.0.1:81
http://127.0.0.1:81
http://127.0.0.1:80
http://127.0.0.1:90
http://foobar:90

func NewDetailedWithScheme

func NewDetailedWithScheme(name string, lookup lookup, ttl time.Duration, scheme string) sd.Subscriber

NewDetailedWithScheme creates a DNS subscriber with the received values and the scheme to use for the fetched server entries.

Example
srvSet := []*net.SRV{
	{
		Port:   90,
		Target: "foobar",
		Weight: 2,
	},
	{
		Port:   90,
		Target: "127.0.0.1",
		Weight: 2,
	},
	{
		Port:   80,
		Target: "127.0.0.1",
		Weight: 2,
	},
	{
		Port:   81,
		Target: "127.0.0.1",
		Weight: 4,
	},
	{
		Port:     82,
		Target:   "127.0.0.1",
		Weight:   10,
		Priority: 2,
	},
	{
		Port:   83,
		Target: "127.0.0.1",
	},
}
lookupFunc := func(service, proto, name string) (cname string, addrs []*net.SRV, err error) {
	return "cname", srvSet, nil
}

s := NewDetailedWithScheme("some.example.tld", lookupFunc, 10*time.Second, "https")
hosts, err := s.Hosts()
if err != nil {
	fmt.Println("Getting the hosts:", err.Error())
	return
}
for _, h := range hosts {
	fmt.Println(h)
}
Output:

https://127.0.0.1:81
https://127.0.0.1:81
https://127.0.0.1:80
https://127.0.0.1:90
https://foobar:90

func Register

func Register() error

Register registers the dns sd subscriber factory under the name defined by Namespace

Example
if err := Register(); err != nil {
	fmt.Println("registering the dns module:", err.Error())
	return
}
srvSet := []*net.SRV{
	{
		Port:   90,
		Target: "foobar",
		Weight: 2,
	},
	{
		Port:   90,
		Target: "127.0.0.1",
		Weight: 2,
	},
	{
		Port:   80,
		Target: "127.0.0.1",
		Weight: 2,
	},
	{
		Port:   81,
		Target: "127.0.0.1",
		Weight: 4,
	},
	{
		Port:     82,
		Target:   "127.0.0.1",
		Weight:   10,
		Priority: 2,
	},
	{
		Port:   83,
		Target: "127.0.0.1",
	},
}
DefaultLookup = func(service, proto, name string) (cname string, addrs []*net.SRV, err error) {
	return "cname", srvSet, nil
}

s := sd.GetRegister().Get(Namespace)(&config.Backend{Host: []string{"some.example.tld"}, SD: Namespace})
hosts, err := s.Hosts()
if err != nil {
	fmt.Println("Getting the hosts:", err.Error())
	return
}
for _, h := range hosts {
	fmt.Println(h)
}
Output:

http://127.0.0.1:81
http://127.0.0.1:81
http://127.0.0.1:80
http://127.0.0.1:90
http://foobar:90

func SubscriberFactory

func SubscriberFactory(cfg *config.Backend) sd.Subscriber

SubscriberFactory builds a DNS_SRV Subscriber with the received config

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL