github.com/emitter-io/go/v2@v2.1.0/options.go (about)

     1  package emitter
     2  
     3  import (
     4  	"crypto/tls"
     5  	"net/url"
     6  	"strconv"
     7  	"time"
     8  )
     9  
    10  // WithMatcher If "mqtt", then topic matching would follow MQTT specification.
    11  func WithMatcher(matcher string) func(*Client) {
    12  	return func(c *Client) {
    13  		if "mqtt" == matcher {
    14  			c.handlers = NewTrieMQTT()
    15  		}
    16  	}
    17  }
    18  
    19  // WithBrokers configures broker URIs to connect to. The format should be scheme://host:port
    20  // Where "scheme" is one of "tcp", "ssl", or "ws", "host" is the ip-address (or hostname)
    21  // and "port" is the port on which the broker is accepting connections.
    22  func WithBrokers(brokers ...string) func(*Client) {
    23  	return func(c *Client) {
    24  		c.opts.Servers = []*url.URL{}
    25  		for _, broker := range brokers {
    26  			brokerURI, err := url.Parse(broker)
    27  			if err != nil {
    28  				panic(err)
    29  			}
    30  
    31  			c.opts.Servers = append(c.opts.Servers, brokerURI)
    32  		}
    33  	}
    34  }
    35  
    36  // WithClientID will set the client id to be used by this client when
    37  // connecting to the MQTT broker. According to the MQTT v3.1 specification,
    38  // a client id mus be no longer than 23 characters.
    39  func WithClientID(id string) func(*Client) {
    40  	return func(c *Client) {
    41  		c.opts.SetClientID(id)
    42  	}
    43  }
    44  
    45  // WithUsername will set the username to be used by this client when connecting
    46  // to the MQTT broker. Note: without the use of SSL/TLS, this information will
    47  // be sent in plaintext accross the wire.
    48  func WithUsername(username string) func(*Client) {
    49  	return func(c *Client) {
    50  		c.opts.SetUsername(username)
    51  	}
    52  }
    53  
    54  // WithPassword will set the password to be used by this client when connecting
    55  // to the MQTT broker. Note: without the use of SSL/TLS, this information will
    56  // be sent in plaintext accross the wire.
    57  func WithPassword(password string) func(*Client) {
    58  	return func(c *Client) {
    59  		c.opts.SetPassword(password)
    60  	}
    61  }
    62  
    63  // WithTLSConfig will set an SSL/TLS configuration to be used when connecting
    64  // to an MQTT broker. Please read the official Go documentation for more
    65  // information.
    66  func WithTLSConfig(t *tls.Config) func(*Client) {
    67  	return func(c *Client) {
    68  		c.opts.SetTLSConfig(t)
    69  	}
    70  }
    71  
    72  // WithKeepAlive will set the amount of time (in seconds) that the client
    73  // should wait before sending a PING request to the broker. This will
    74  // allow the client to know that a connection has not been lost with the
    75  // server.
    76  func WithKeepAlive(k time.Duration) func(*Client) {
    77  	return func(c *Client) {
    78  		c.opts.SetKeepAlive(k)
    79  	}
    80  }
    81  
    82  // WithPingTimeout will set the amount of time (in seconds) that the client
    83  // will wait after sending a PING request to the broker, before deciding
    84  // that the connection has been lost. Default is 10 seconds.
    85  func WithPingTimeout(k time.Duration) func(*Client) {
    86  	return func(c *Client) {
    87  		c.opts.SetPingTimeout(k)
    88  	}
    89  }
    90  
    91  // WithConnectTimeout limits how long the client will wait when trying to open a connection
    92  // to an MQTT server before timeing out and erroring the attempt. A duration of 0 never times out.
    93  // Default 30 seconds. Currently only operational on TCP/TLS connections.
    94  func WithConnectTimeout(t time.Duration) func(*Client) {
    95  	return func(c *Client) {
    96  		c.opts.SetConnectTimeout(t)
    97  	}
    98  }
    99  
   100  // WithMaxReconnectInterval sets the maximum time that will be waited between reconnection attempts
   101  // when connection is lost
   102  func WithMaxReconnectInterval(t time.Duration) func(*Client) {
   103  	return func(c *Client) {
   104  		c.opts.SetMaxReconnectInterval(t)
   105  	}
   106  }
   107  
   108  // WithAutoReconnect sets whether the automatic reconnection logic should be used
   109  // when the connection is lost, even if disabled the ConnectionLostHandler is still
   110  // called
   111  func WithAutoReconnect(a bool) func(*Client) {
   112  	return func(c *Client) {
   113  		c.opts.SetAutoReconnect(a)
   114  	}
   115  }
   116  
   117  // option represents a key/value pair that can be supplied to the publish/subscribe or unsubscribe
   118  // methods and provide ways to configure the operation.
   119  type option string
   120  
   121  const (
   122  	withRetain = option("+r")
   123  	withQos0   = option("+0")
   124  	withQos1   = option("+1")
   125  )
   126  
   127  // String converts the option to a string.
   128  func (o option) String() string {
   129  	return string(o)
   130  }
   131  
   132  // WithoutEcho constructs an option which disables self-receiving messages if subscribed to a channel.
   133  func WithoutEcho() Option {
   134  	return option("me=0")
   135  }
   136  
   137  // WithTTL constructs an option which can be used during publish requests to set a Time-To-Live.
   138  func WithTTL(seconds int) Option {
   139  	return option("ttl=" + strconv.Itoa(seconds))
   140  }
   141  
   142  // WithLast constructs an option which can be used during subscribe requests to retrieve a message history.
   143  func WithLast(messages int) Option {
   144  	return option("last=" + strconv.Itoa(messages))
   145  }
   146  
   147  // WithRetain constructs an option which sets the message 'retain' flag to true.
   148  func WithRetain() Option {
   149  	return withRetain
   150  }
   151  
   152  // WithAtMostOnce instructs to publish at most once (MQTT QoS 0).
   153  func WithAtMostOnce() Option {
   154  	return withQos0
   155  }
   156  
   157  // WithAtLeastOnce instructs to publish at least once (MQTT QoS 1).
   158  func WithAtLeastOnce() Option {
   159  	return withQos1
   160  }
   161  
   162  func getUTCTimestamp(input time.Time) int64 {
   163  	t := input
   164  	if zone, _ := t.Zone(); zone != "UTC" {
   165  		loc, _ := time.LoadLocation("UTC")
   166  		t = t.In(loc)
   167  	}
   168  	return t.Unix()
   169  }
   170  
   171  // WithFrom request messages from a point in time.
   172  func WithFrom(from time.Time) Option {
   173  	return option("from=" + strconv.FormatInt(getUTCTimestamp(from), 10))
   174  }
   175  
   176  // WithUntil request messages until a point in time.
   177  func WithUntil(until time.Time) Option {
   178  	return option("until=" + strconv.FormatInt(getUTCTimestamp(until), 10))
   179  }