2019-01-23 01:39:42 +03:00
|
|
|
package stan
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-02-10 13:16:41 +03:00
|
|
|
"time"
|
2019-01-23 01:39:42 +03:00
|
|
|
|
|
|
|
"github.com/micro/go-micro/broker"
|
2019-05-24 11:39:27 +03:00
|
|
|
"github.com/micro/go-micro/server"
|
2019-01-23 01:39:42 +03:00
|
|
|
stan "github.com/nats-io/go-nats-streaming"
|
|
|
|
)
|
|
|
|
|
|
|
|
type optionsKey struct{}
|
|
|
|
|
|
|
|
// Options accepts stan.Options
|
|
|
|
func Options(opts stan.Options) broker.Option {
|
|
|
|
return setBrokerOption(optionsKey{}, opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
type clusterIDKey struct{}
|
|
|
|
|
|
|
|
// ClusterID specify cluster id to connect
|
|
|
|
func ClusterID(clusterID string) broker.Option {
|
|
|
|
return setBrokerOption(clusterIDKey{}, clusterID)
|
|
|
|
}
|
|
|
|
|
2019-04-26 11:34:46 +03:00
|
|
|
type clientIDKey struct{}
|
|
|
|
|
|
|
|
// ClientID specify client id to connect
|
|
|
|
func ClientID(clientID string) broker.Option {
|
|
|
|
return setBrokerOption(clientIDKey{}, clientID)
|
|
|
|
}
|
|
|
|
|
2019-01-23 01:39:42 +03:00
|
|
|
type subscribeOptionKey struct{}
|
|
|
|
|
|
|
|
func SubscribeOption(opts ...stan.SubscriptionOption) broker.SubscribeOption {
|
|
|
|
return setSubscribeOption(subscribeOptionKey{}, opts)
|
|
|
|
}
|
|
|
|
|
2019-05-24 11:39:27 +03:00
|
|
|
func ServerSubscriberOption(opts ...stan.SubscriptionOption) server.SubscriberOption {
|
|
|
|
return setServerSubscriberOption(subscribeOptionKey{}, opts)
|
|
|
|
}
|
|
|
|
|
2019-01-23 01:39:42 +03:00
|
|
|
type subscribeContextKey struct{}
|
|
|
|
|
|
|
|
// SubscribeContext set the context for broker.SubscribeOption
|
|
|
|
func SubscribeContext(ctx context.Context) broker.SubscribeOption {
|
|
|
|
return setSubscribeOption(subscribeContextKey{}, ctx)
|
|
|
|
}
|
|
|
|
|
2019-01-28 11:18:02 +03:00
|
|
|
type ackSuccessKey struct{}
|
2019-01-23 01:39:42 +03:00
|
|
|
|
2019-01-28 11:20:25 +03:00
|
|
|
// AckOnSuccess will automatically acknowledge messages when no error is returned
|
2019-01-28 11:18:02 +03:00
|
|
|
func AckOnSuccess() broker.SubscribeOption {
|
|
|
|
return setSubscribeOption(ackSuccessKey{}, true)
|
2019-01-23 01:39:42 +03:00
|
|
|
}
|
2019-02-10 13:16:41 +03:00
|
|
|
|
2019-02-12 17:27:14 +03:00
|
|
|
type connectTimeoutKey struct{}
|
2019-02-10 13:16:41 +03:00
|
|
|
|
2019-02-12 17:27:14 +03:00
|
|
|
// ConnectTimeout timeout for connecting to broker -1 infinitive or time.Duration value
|
|
|
|
func ConnectTimeout(td time.Duration) broker.Option {
|
|
|
|
return setBrokerOption(connectTimeoutKey{}, td)
|
2019-02-10 13:16:41 +03:00
|
|
|
}
|
|
|
|
|
2019-02-12 17:27:14 +03:00
|
|
|
type connectRetryKey struct{}
|
2019-02-10 13:16:41 +03:00
|
|
|
|
2019-02-12 17:27:14 +03:00
|
|
|
// ConnectRetry reconnect to broker in case of errors
|
|
|
|
func ConnectRetry(v bool) broker.Option {
|
|
|
|
return setBrokerOption(connectRetryKey{}, v)
|
2019-02-10 13:16:41 +03:00
|
|
|
}
|
2019-05-29 16:57:48 +03:00
|
|
|
|
|
|
|
type durableKey struct{}
|
|
|
|
|
|
|
|
// DurableName sets the DurableName for the subscriber
|
|
|
|
func DurableName(name string) broker.Option {
|
|
|
|
return setBrokerOption(durableKey{}, name)
|
|
|
|
}
|