gitee.com/liuxuezhan/go-micro-v1.18.0@v1.0.0/server/handler.go (about)

     1  package server
     2  
     3  import "context"
     4  
     5  type HandlerOption func(*HandlerOptions)
     6  
     7  type HandlerOptions struct {
     8  	Internal bool
     9  	Metadata map[string]map[string]string
    10  }
    11  
    12  type SubscriberOption func(*SubscriberOptions)
    13  
    14  type SubscriberOptions struct {
    15  	// AutoAck defaults to true. When a handler returns
    16  	// with a nil error the message is acked.
    17  	AutoAck  bool
    18  	Queue    string
    19  	Internal bool
    20  	Context  context.Context
    21  }
    22  
    23  // EndpointMetadata is a Handler option that allows metadata to be added to
    24  // individual endpoints.
    25  func EndpointMetadata(name string, md map[string]string) HandlerOption {
    26  	return func(o *HandlerOptions) {
    27  		o.Metadata[name] = md
    28  	}
    29  }
    30  
    31  // Internal Handler options specifies that a handler is not advertised
    32  // to the discovery system. In the future this may also limit request
    33  // to the internal network or authorised user.
    34  func InternalHandler(b bool) HandlerOption {
    35  	return func(o *HandlerOptions) {
    36  		o.Internal = b
    37  	}
    38  }
    39  
    40  // Internal Subscriber options specifies that a subscriber is not advertised
    41  // to the discovery system.
    42  func InternalSubscriber(b bool) SubscriberOption {
    43  	return func(o *SubscriberOptions) {
    44  		o.Internal = b
    45  	}
    46  }
    47  func NewSubscriberOptions(opts ...SubscriberOption) SubscriberOptions {
    48  	opt := SubscriberOptions{
    49  		AutoAck: true,
    50  		Context: context.Background(),
    51  	}
    52  
    53  	for _, o := range opts {
    54  		o(&opt)
    55  	}
    56  
    57  	return opt
    58  }
    59  
    60  // DisableAutoAck will disable auto acking of messages
    61  // after they have been handled.
    62  func DisableAutoAck() SubscriberOption {
    63  	return func(o *SubscriberOptions) {
    64  		o.AutoAck = false
    65  	}
    66  }
    67  
    68  // Shared queue name distributed messages across subscribers
    69  func SubscriberQueue(n string) SubscriberOption {
    70  	return func(o *SubscriberOptions) {
    71  		o.Queue = n
    72  	}
    73  }
    74  
    75  // SubscriberContext set context options to allow broker SubscriberOption passed
    76  func SubscriberContext(ctx context.Context) SubscriberOption {
    77  	return func(o *SubscriberOptions) {
    78  		o.Context = ctx
    79  	}
    80  }