github.com/clubpay/ronykit/kit@v0.14.4-0.20240515065620-d0dace45cbc7/edge_options.go (about)

     1  package kit
     2  
     3  import "time"
     4  
     5  type edgeConfig struct {
     6  	logger          Logger
     7  	prefork         bool
     8  	shutdownTimeout time.Duration
     9  	gateways        []Gateway
    10  	cluster         Cluster
    11  	services        []Service
    12  	errHandler      ErrHandlerFunc
    13  	globalHandlers  []HandlerFunc
    14  	tracer          Tracer
    15  	connDelegate    ConnDelegate
    16  }
    17  
    18  type Option func(s *edgeConfig)
    19  
    20  func WithLogger(l Logger) Option {
    21  	return func(s *edgeConfig) {
    22  		s.logger = l
    23  	}
    24  }
    25  
    26  func WithPrefork() Option {
    27  	return func(s *edgeConfig) {
    28  		s.prefork = true
    29  	}
    30  }
    31  
    32  // WithGateway lets you register a bundle in constructor of the EdgeServer.
    33  func WithGateway(gw ...Gateway) Option {
    34  	return func(s *edgeConfig) {
    35  		s.gateways = append(s.gateways, gw...)
    36  	}
    37  }
    38  
    39  // WithCluster lets you register a cluster in constructor of the EdgeServer.
    40  func WithCluster(cb Cluster) Option {
    41  	return func(s *edgeConfig) {
    42  		s.cluster = cb
    43  	}
    44  }
    45  
    46  // WithService lets you register a service in constructor of the EdgeServer.
    47  func WithService(service ...Service) Option {
    48  	return func(s *edgeConfig) {
    49  		s.services = append(s.services, service...)
    50  	}
    51  }
    52  
    53  // WithServiceDesc lets you register a ServiceDescriptor in constructor of the EdgeServer.
    54  func WithServiceDesc(desc ...ServiceDescriptor) Option {
    55  	return func(s *edgeConfig) {
    56  		for _, d := range desc {
    57  			s.services = append(s.services, d.Generate())
    58  		}
    59  	}
    60  }
    61  
    62  // WithShutdownTimeout sets the maximum time to wait until all running requests to finish.
    63  // Default is to wait forever.
    64  func WithShutdownTimeout(d time.Duration) Option {
    65  	return func(s *edgeConfig) {
    66  		s.shutdownTimeout = d
    67  	}
    68  }
    69  
    70  // WithErrorHandler registers a global error handler to catch any error that
    71  // happens before EdgeServer can deliver the incoming message to the handler, or delivering
    72  // the outgoing message to the client.
    73  // Internal errors are usually wrapped and could be checked for better error handling.
    74  // You can check with errors.Is function to see if the error is one of the following:
    75  // ErrDispatchFailed, ErrWriteToClosedConn, ErrNoHandler
    76  // ErrDecodeIncomingMessageFailed, ErrEncodeOutgoingMessageFailed
    77  func WithErrorHandler(h ErrHandlerFunc) Option {
    78  	return func(s *edgeConfig) {
    79  		s.errHandler = h
    80  	}
    81  }
    82  
    83  // WithGlobalHandlers sets the handlers that will be executed before any service's contract.
    84  func WithGlobalHandlers(handlers ...HandlerFunc) Option {
    85  	return func(s *edgeConfig) {
    86  		s.globalHandlers = handlers
    87  	}
    88  }
    89  
    90  func WithTrace(tp Tracer) Option {
    91  	return func(s *edgeConfig) {
    92  		s.tracer = tp
    93  	}
    94  }
    95  
    96  // WithConnDelegate registers the delegate to receive callbacks on connection open/close events.
    97  // This delegate could be useful to add metrics based on the connections, or any other advanced
    98  // scenarios. For most use cases, this is not necessary.
    99  func WithConnDelegate(d ConnDelegate) Option {
   100  	return func(s *edgeConfig) {
   101  		s.connDelegate = d
   102  	}
   103  }