2015-06-03 01:25:37 +01:00
|
|
|
package server
|
|
|
|
|
2019-01-24 00:46:26 +03:00
|
|
|
import "context"
|
|
|
|
|
|
|
|
type HandlerOption func(*HandlerOptions)
|
|
|
|
|
2016-01-08 14:02:32 +00:00
|
|
|
type HandlerOptions struct {
|
|
|
|
Internal bool
|
2016-05-26 18:01:02 +01:00
|
|
|
Metadata map[string]map[string]string
|
2020-09-11 11:55:31 +03:00
|
|
|
Context context.Context
|
2016-01-08 14:02:32 +00:00
|
|
|
}
|
|
|
|
|
2020-09-20 16:57:54 +03:00
|
|
|
func NewHandlerOptions(opts ...HandlerOption) HandlerOptions {
|
|
|
|
options := HandlerOptions{
|
|
|
|
Context: context.Background(),
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
|
|
|
return options
|
2020-09-05 02:11:29 +03:00
|
|
|
}
|
|
|
|
|
2019-01-24 00:46:26 +03:00
|
|
|
type SubscriberOption func(*SubscriberOptions)
|
|
|
|
|
2016-01-08 14:02:32 +00:00
|
|
|
type SubscriberOptions struct {
|
2019-05-24 20:06:27 +08:00
|
|
|
// AutoAck defaults to true. When a handler returns
|
|
|
|
// with a nil error the message is acked.
|
|
|
|
AutoAck bool
|
2016-01-22 21:48:43 +00:00
|
|
|
Queue string
|
2016-01-08 14:02:32 +00:00
|
|
|
Internal bool
|
2019-01-24 00:46:26 +03:00
|
|
|
Context context.Context
|
2016-01-08 14:02:32 +00:00
|
|
|
}
|
|
|
|
|
2020-09-05 02:11:29 +03:00
|
|
|
func NewSubscriberOptions(opts ...SubscriberOption) SubscriberOptions {
|
2020-09-20 16:57:54 +03:00
|
|
|
options := SubscriberOptions{
|
2020-09-05 02:11:29 +03:00
|
|
|
AutoAck: true,
|
|
|
|
Context: context.Background(),
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, o := range opts {
|
2020-09-20 16:57:54 +03:00
|
|
|
o(&options)
|
2020-09-05 02:11:29 +03:00
|
|
|
}
|
|
|
|
|
2020-09-20 16:57:54 +03:00
|
|
|
return options
|
2020-09-05 02:11:29 +03:00
|
|
|
}
|
|
|
|
|
2016-05-26 20:43:05 +01:00
|
|
|
// EndpointMetadata is a Handler option that allows metadata to be added to
|
|
|
|
// individual endpoints.
|
|
|
|
func EndpointMetadata(name string, md map[string]string) HandlerOption {
|
|
|
|
return func(o *HandlerOptions) {
|
|
|
|
o.Metadata[name] = md
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-08 14:02:32 +00:00
|
|
|
// Internal Handler options specifies that a handler is not advertised
|
|
|
|
// to the discovery system. In the future this may also limit request
|
|
|
|
// to the internal network or authorised user.
|
|
|
|
func InternalHandler(b bool) HandlerOption {
|
|
|
|
return func(o *HandlerOptions) {
|
|
|
|
o.Internal = b
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Internal Subscriber options specifies that a subscriber is not advertised
|
|
|
|
// to the discovery system.
|
|
|
|
func InternalSubscriber(b bool) SubscriberOption {
|
|
|
|
return func(o *SubscriberOptions) {
|
|
|
|
o.Internal = b
|
|
|
|
}
|
2015-06-12 19:52:27 +01:00
|
|
|
}
|
2016-01-22 21:48:43 +00:00
|
|
|
|
2019-05-24 20:06:27 +08:00
|
|
|
// DisableAutoAck will disable auto acking of messages
|
|
|
|
// after they have been handled.
|
|
|
|
func DisableAutoAck() SubscriberOption {
|
|
|
|
return func(o *SubscriberOptions) {
|
|
|
|
o.AutoAck = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-22 21:48:43 +00:00
|
|
|
// Shared queue name distributed messages across subscribers
|
|
|
|
func SubscriberQueue(n string) SubscriberOption {
|
|
|
|
return func(o *SubscriberOptions) {
|
|
|
|
o.Queue = n
|
|
|
|
}
|
|
|
|
}
|
2019-01-24 00:46:26 +03:00
|
|
|
|
|
|
|
// SubscriberContext set context options to allow broker SubscriberOption passed
|
|
|
|
func SubscriberContext(ctx context.Context) SubscriberOption {
|
|
|
|
return func(o *SubscriberOptions) {
|
|
|
|
o.Context = ctx
|
|
|
|
}
|
|
|
|
}
|