README

go-srvclient

A simple package for resolving SRV records being served by skydns, according to the algorithm set forth in that project's README. It simply takes in a hostname and does a SRV request against it. It wil then look at all the returned entries and make a weighted random choice of one of them, returning a string which is the "host:port" of the picked entry.

Does not work on Windows.

Install

go get github.com/levenlabs/go-srvclient

Example

package main

import "github.com/levenlabs/go-srvclient"

func main() {
	addr, err := srvclient.SRV("foo.skydns.local")
	if err != nil {
		fmt.Fatal(err)
	}

	log.Printf("%s was chosen!", addr)
}

Binary client

This project also has an installable binary client which can be easily used. It can be installed with:

go get github.com/levenlabs/go-srvclient/srvclient

And used like so:

# srvclient some.host.name
8.9.10.11:1213
Expand ▾ Collapse ▴

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultSRVClient = new(SRVClient)

    DefaultSRVClient is an instance of SRVClient with all zero'd values, used as the default client for all global methods. It can be overwritten prior to any of the methods being used in order to modify their behavior

    Functions

    func AllSRV

    func AllSRV(hostname string) ([]string, error)

      AllSRV calls the AllSRV method on the DefaultSRVClient

      func AllSRVTranslate

      func AllSRVTranslate(hostname string) ([]string, error)

        AllSRVTranslate calls the AllSRVTranslate method on the DefaultSRVClient

        func MaybeSRV

        func MaybeSRV(host string) string

          MaybeSRV calls the MaybeSRV method on the DefaultSRVClient

          func MaybeSRVURL

          func MaybeSRVURL(host string) string

            MaybeSRVURL calls the MaybeSRVURL method on the DefaultSRVClient

            func SRV

            func SRV(hostname string) (string, error)

              SRV calls the SRV method on the DefaultSRVClient

              func SRVNoPort

              func SRVNoPort(hostname string) (string, error)

                SRVNoPort calls the SRVNoPort method on the DefaultSRVClient

                func SRVNoTranslate

                func SRVNoTranslate(hostname string) (string, error)

                  SRVNoTranslate calls the SRVNoTranslate method on the DefaultSRVClient

                  Types

                  type ErrNotFound

                  type ErrNotFound struct {
                  	// contains filtered or unexported fields
                  }

                    ErrNotFound is returned when there were no SRV records for the given hostname

                    func (*ErrNotFound) Error

                    func (err *ErrNotFound) Error() string

                      Error implements the error interface

                      type SRVClient

                      type SRVClient struct {
                      	UDPSize uint16
                      
                      	// If IgnoreTruncated is true, then lookups will NOT fallback to TCP when
                      	// they were truncated over UDP.
                      	IgnoreTruncated bool
                      
                      	// A list of addresses ("ip:port") which should be used as the resolver
                      	// list. If none are set then the resolver settings in /etc/resolv.conf are
                      	// used. This can only be updated before the SRVClient is used for the first
                      	// time.
                      	ResolverAddrs []string
                      
                      	// If non-nill, will be called on messages returned from dns servers prior
                      	// to them being processed (i.e. before they are cached, sorted,
                      	// ip-replaced, etc...)
                      	Preprocess func(*dns.Msg)
                      	// contains filtered or unexported fields
                      }

                        SRVClient is a holder for methods related to SRV lookups. Use new(SRVClient) to initialize one.

                        func (*SRVClient) AllSRV

                        func (sc *SRVClient) AllSRV(hostname string) ([]string, error)

                          AllSRV returns the list of all hostnames and ports for the SRV lookup The results are sorted by priority and then weight. Like SRV, if hostname contained a port then the port on all results will be replaced with the originally-passed port AllSRV will NOT replace hostnames with their respective IPs

                          func (*SRVClient) AllSRVTranslate

                          func (sc *SRVClient) AllSRVTranslate(hostname string) ([]string, error)

                            AllSRVTranslate returns the list of all IPs and ports for the SRV lookup The results are sorted by priority and then weight. Like SRV, if hostname contained a port then the port on all results will be replaced with the originally-passed port

                            func (*SRVClient) EnableCacheLast

                            func (sc *SRVClient) EnableCacheLast()

                              EnableCacheLast is used to make SRVClient cache the last successful SRV response for each domain requested, and if the next request results in some kind of error it will use that last response instead.

                              func (*SRVClient) MaybeSRV

                              func (sc *SRVClient) MaybeSRV(host string) string

                                MaybeSRV attempts a SRV lookup if the host doesn't contain a port and if the SRV lookup succeeds it'll rewrite the host and return it with the lookup result. If it fails it'll just return the host originally sent

                                func (*SRVClient) MaybeSRVURL

                                func (sc *SRVClient) MaybeSRVURL(host string) string

                                  MaybeSRVURL calls MaybeSRV and also prepends http:// if no scheme was sent

                                  func (*SRVClient) SRV

                                  func (sc *SRVClient) SRV(hostname string) (string, error)

                                    SRV will perform a SRV request on the given hostname, and then choose one of the returned entries randomly based on the priority and weight fields it sees. It will return the address ("host:port") of the winning entry, or an error if the query couldn't be made or it returned no entries. If the DNS server provided the A records for the hosts, then the result will have the target replaced with its respective IP.

                                    If the given hostname already has a ":port" appended to it, only the ip will be looked up from the SRV request, but the port given will be returned

                                    If the given hostname is "ip:port", it'll just immediately return what you sent.

                                    func (*SRVClient) SRVNoPort

                                    func (sc *SRVClient) SRVNoPort(hostname string) (string, error)

                                      SRVNoPort behaves the same as SRV, but the returned address string will not contain the port

                                      func (*SRVClient) SRVNoTranslate

                                      func (sc *SRVClient) SRVNoTranslate(hostname string) (string, error)

                                        SRVNoTranslate is exactly like SRV except it won't translate names to their respective IPs

                                        func (*SRVClient) Stats

                                        func (sc *SRVClient) Stats() SRVStats

                                          Stats returns the latest SRVStats struct for the given client

                                          type SRVStats

                                          type SRVStats struct {
                                          	UDPQueries         int64
                                          	TCPQueries         int64
                                          	TruncatedResponses int64
                                          	ExchangeErrors     int64
                                          	CacheLastHits      int64
                                          	CacheLastMisses    int64
                                          }

                                            SRVStats contains lifetime counts for various statistics

                                            Directories

                                            Path Synopsis