2015-06-03 02:25:37 +02:00
|
|
|
package server
|
|
|
|
|
2019-01-23 23:46:26 +02:00
|
|
|
import "context"
|
|
|
|
|
|
|
|
type HandlerOption func(*HandlerOptions)
|
|
|
|
|
2016-01-08 16:02:32 +02:00
|
|
|
type HandlerOptions struct {
|
2016-05-26 19:01:02 +02:00
|
|
|
Metadata map[string]map[string]string
|
2023-04-26 02:16:34 +02:00
|
|
|
Internal bool
|
2016-01-08 16:02:32 +02:00
|
|
|
}
|
|
|
|
|
2019-01-23 23:46:26 +02:00
|
|
|
type SubscriberOption func(*SubscriberOptions)
|
|
|
|
|
2016-01-08 16:02:32 +02:00
|
|
|
type SubscriberOptions struct {
|
2023-04-26 02:16:34 +02:00
|
|
|
Context context.Context
|
|
|
|
Queue string
|
2019-05-24 14:06:27 +02:00
|
|
|
// AutoAck defaults to true. When a handler returns
|
|
|
|
// with a nil error the message is acked.
|
|
|
|
AutoAck bool
|
2016-01-08 16:02:32 +02:00
|
|
|
Internal bool
|
|
|
|
}
|
|
|
|
|
2016-05-26 21:43:05 +02: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 16:02:32 +02:00
|
|
|
// Internal Handler options specifies that a handler is not advertised
|
|
|
|
// to the discovery system. In the future this may also limit request
|
2022-09-30 16:27:07 +02:00
|
|
|
// to the internal network or authorized user.
|
2016-01-08 16:02:32 +02:00
|
|
|
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 20:52:27 +02:00
|
|
|
}
|
2019-01-23 23:46:26 +02:00
|
|
|
func NewSubscriberOptions(opts ...SubscriberOption) SubscriberOptions {
|
|
|
|
opt := SubscriberOptions{
|
2019-05-24 14:06:27 +02:00
|
|
|
AutoAck: true,
|
2019-01-23 23:46:26 +02:00
|
|
|
Context: context.Background(),
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&opt)
|
|
|
|
}
|
|
|
|
|
|
|
|
return opt
|
|
|
|
}
|
2016-01-22 23:48:43 +02:00
|
|
|
|
2019-05-24 14:06:27 +02:00
|
|
|
// DisableAutoAck will disable auto acking of messages
|
|
|
|
// after they have been handled.
|
|
|
|
func DisableAutoAck() SubscriberOption {
|
|
|
|
return func(o *SubscriberOptions) {
|
|
|
|
o.AutoAck = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-30 16:27:07 +02:00
|
|
|
// Shared queue name distributed messages across subscribers.
|
2016-01-22 23:48:43 +02:00
|
|
|
func SubscriberQueue(n string) SubscriberOption {
|
|
|
|
return func(o *SubscriberOptions) {
|
|
|
|
o.Queue = n
|
|
|
|
}
|
|
|
|
}
|
2019-01-23 23:46:26 +02:00
|
|
|
|
2022-09-30 16:27:07 +02:00
|
|
|
// SubscriberContext set context options to allow broker SubscriberOption passed.
|
2019-01-23 23:46:26 +02:00
|
|
|
func SubscriberContext(ctx context.Context) SubscriberOption {
|
|
|
|
return func(o *SubscriberOptions) {
|
|
|
|
o.Context = ctx
|
|
|
|
}
|
|
|
|
}
|