go-micro.dev/v5@v5.12.0/events/natsjs/options.go (about)

     1  package natsjs
     2  
     3  import (
     4  	"crypto/tls"
     5  	"time"
     6  
     7  	"go-micro.dev/v5/logger"
     8  )
     9  
    10  // Options which are used to configure the nats stream.
    11  type Options struct {
    12  	ClusterID             string
    13  	ClientID              string
    14  	Address               string
    15  	NkeyConfig            string
    16  	TLSConfig             *tls.Config
    17  	Logger                logger.Logger
    18  	SyncPublish           bool
    19  	Name                  string
    20  	DisableDurableStreams bool
    21  	Username              string
    22  	Password              string
    23  	RetentionPolicy       int
    24  	MaxAge                time.Duration
    25  	MaxMsgSize            int
    26  }
    27  
    28  // Option is a function which configures options.
    29  type Option func(o *Options)
    30  
    31  // ClusterID sets the cluster id for the nats connection.
    32  func ClusterID(id string) Option {
    33  	return func(o *Options) {
    34  		o.ClusterID = id
    35  	}
    36  }
    37  
    38  // ClientID sets the client id for the nats connection.
    39  func ClientID(id string) Option {
    40  	return func(o *Options) {
    41  		o.ClientID = id
    42  	}
    43  }
    44  
    45  // Address of the nats cluster.
    46  func Address(addr string) Option {
    47  	return func(o *Options) {
    48  		o.Address = addr
    49  	}
    50  }
    51  
    52  // TLSConfig to use when connecting to the cluster.
    53  func TLSConfig(t *tls.Config) Option {
    54  	return func(o *Options) {
    55  		o.TLSConfig = t
    56  	}
    57  }
    58  
    59  // NkeyConfig string to use when connecting to the cluster.
    60  func NkeyConfig(nkey string) Option {
    61  	return func(o *Options) {
    62  		o.NkeyConfig = nkey
    63  	}
    64  }
    65  
    66  // Logger sets the underlying logger.
    67  func Logger(log logger.Logger) Option {
    68  	return func(o *Options) {
    69  		o.Logger = log
    70  	}
    71  }
    72  
    73  // SynchronousPublish allows using a synchronous publishing instead of the default asynchronous.
    74  func SynchronousPublish(sync bool) Option {
    75  	return func(o *Options) {
    76  		o.SyncPublish = sync
    77  	}
    78  }
    79  
    80  // Name allows to add a name to the natsjs connection.
    81  func Name(name string) Option {
    82  	return func(o *Options) {
    83  		o.Name = name
    84  	}
    85  }
    86  
    87  // DisableDurableStreams will disable durable streams.
    88  func DisableDurableStreams() Option {
    89  	return func(o *Options) {
    90  		o.DisableDurableStreams = true
    91  	}
    92  }
    93  
    94  // Authenticate authenticates the connection with the given username and password.
    95  func Authenticate(username, password string) Option {
    96  	return func(o *Options) {
    97  		o.Username = username
    98  		o.Password = password
    99  	}
   100  }
   101  func RetentionPolicy(rp int) Option {
   102  	return func(o *Options) {
   103  		o.RetentionPolicy = rp
   104  	}
   105  }
   106  
   107  func MaxMsgSize(size int) Option {
   108  	return func(o *Options) {
   109  		o.MaxMsgSize = size
   110  	}
   111  }
   112  func MaxAge(age time.Duration) Option {
   113  	return func(o *Options) {
   114  		o.MaxAge = age
   115  	}
   116  }