github.com/xmidt-org/webpa-common@v1.11.9/service/zk/options.go (about)

     1  package zk
     2  
     3  import (
     4  	"strings"
     5  	"time"
     6  )
     7  
     8  const (
     9  	DefaultServer      = "localhost:2181"
    10  	DefaultServiceName = "test"
    11  	DefaultPath        = "/xmidt/test"
    12  	DefaultAddress     = "localhost"
    13  	DefaultPort        = 8080
    14  	DefaultScheme      = "http"
    15  
    16  	DefaultConnectTimeout time.Duration = 5 * time.Second
    17  	DefaultSessionTimeout time.Duration = 10 * time.Second
    18  )
    19  
    20  type Registration struct {
    21  	// Name is the service name under which to register.  If not supplied, DefaultServiceName is used.
    22  	Name string `json:"name,omitempty"`
    23  
    24  	// Path is the znode path under which to register.  If not supplied, DefaultPath is used.
    25  	Path string `json:"path,omitempty"`
    26  
    27  	// Address is the FQDN or hostname of the server which hosts the service.  If not supplied, DefaultAddress is used.
    28  	Address string `json:"address,omitempty"`
    29  
    30  	// Port is the TCP port on which the service listens.  If not supplied, DefaultPort is used.
    31  	Port int `json:"port,omitempty"`
    32  
    33  	// Scheme specific the protocl used for the service.  If not supplied, DefaultScheme is used.
    34  	Scheme string `json:"scheme,omitempty"`
    35  }
    36  
    37  func (r Registration) name() string {
    38  	if len(r.Name) > 0 {
    39  		return r.Name
    40  	}
    41  
    42  	return DefaultServiceName
    43  }
    44  
    45  func (r Registration) path() string {
    46  	if len(r.Path) > 0 {
    47  		return r.Path
    48  	}
    49  
    50  	return DefaultPath
    51  }
    52  
    53  func (r Registration) address() string {
    54  	if len(r.Address) > 0 {
    55  		return r.Address
    56  	}
    57  
    58  	return DefaultAddress
    59  }
    60  
    61  func (r Registration) port() int {
    62  	if r.Port > 0 {
    63  		return r.Port
    64  	}
    65  
    66  	return DefaultPort
    67  }
    68  
    69  func (r Registration) scheme() string {
    70  	if len(r.Scheme) > 0 {
    71  		return r.Scheme
    72  	}
    73  
    74  	return DefaultScheme
    75  }
    76  
    77  // Client is the client portion of the options struct
    78  type Client struct {
    79  	// Connection is the comma-delimited Zookeeper connection string.  Both this and
    80  	// Servers may be set, and they will be merged together when connecting to Zookeeper.
    81  	Connection string `json:"connection,omitempty"`
    82  
    83  	// Servers is the array of Zookeeper servers.  Both this and Connection may be set,
    84  	// and they will be merged together when connecting to Zookeeper.
    85  	Servers []string `json:"servers,omitempty"`
    86  
    87  	// ConnectTimeout is the Zookeeper connection timeout.
    88  	ConnectTimeout time.Duration `json:"connectTimeout"`
    89  
    90  	// SessionTimeout is the Zookeeper session timeout.
    91  	SessionTimeout time.Duration `json:"sessionTimeout"`
    92  }
    93  
    94  func (c *Client) servers() []string {
    95  	servers := make([]string, 0, 10)
    96  
    97  	if c != nil {
    98  		if len(c.Connection) > 0 {
    99  			for _, server := range strings.Split(c.Connection, ",") {
   100  				servers = append(servers, strings.TrimSpace(server))
   101  			}
   102  		}
   103  
   104  		if len(c.Servers) > 0 {
   105  			servers = append(servers, c.Servers...)
   106  		}
   107  	}
   108  
   109  	if len(servers) == 0 {
   110  		servers = append(servers, DefaultServer)
   111  	}
   112  
   113  	return servers
   114  }
   115  
   116  func (c *Client) connectTimeout() time.Duration {
   117  	if c != nil && c.ConnectTimeout > 0 {
   118  		return c.ConnectTimeout
   119  	}
   120  
   121  	return DefaultConnectTimeout
   122  }
   123  
   124  func (c *Client) sessionTimeout() time.Duration {
   125  	if c != nil && c.SessionTimeout > 0 {
   126  		return c.SessionTimeout
   127  	}
   128  
   129  	return DefaultSessionTimeout
   130  }
   131  
   132  // Options represents the set of configurable attributes for Zookeeper
   133  type Options struct {
   134  	// Client holds the zookeeper client options
   135  	Client Client `json:"client"`
   136  
   137  	// Registrations are the ways in which the host process should be registered with zookeeper.
   138  	// There is no default for this field.
   139  	Registrations []Registration `json:"registrations,omitempty"`
   140  
   141  	// Watches are the zookeeper paths to watch for updates.  There is no default for this field.
   142  	Watches []string `json:"watches,omitempty"`
   143  }
   144  
   145  func (o *Options) client() *Client {
   146  	if o != nil {
   147  		return &o.Client
   148  	}
   149  
   150  	return nil
   151  }
   152  
   153  func (o *Options) registrations() []Registration {
   154  	if o != nil && len(o.Registrations) > 0 {
   155  		return o.Registrations
   156  	}
   157  
   158  	return nil
   159  }
   160  
   161  func (o *Options) watches() []string {
   162  	if o != nil && len(o.Watches) > 0 {
   163  		return o.Watches
   164  	}
   165  
   166  	return nil
   167  }