示例#1
0
        public static void Initialize(string envPrefix,
                                      string configFile,
                                      Server serverInfo,
                                      NativeLogLevel logLevel,
                                      NativeLogKind logKind,
                                      Action <string> logFunction,
                                      MetricsConfiguration metricsConfig,
                                      ServiceDiscoveryListener serviceDiscoveryListener = null)
        {
            _serviceDiscoveryListener    = serviceDiscoveryListener;
            _handleRpcCallback           = new HandleRpcCallbackFunc(HandleRpcCallback);
            _clusterNotificationCallback = new ClusterNotificationCallbackFunc(ClusterNotificationCallback);
            _logFunctionCallback         = new LogFunction(LogFunctionCallback);
            var logCtx        = GCHandle.Alloc(logFunction, GCHandleType.Normal);
            var pitayaMetrics = IntPtr.Zero;

            if (metricsConfig.IsEnabled)
            {
                if (metricsConfig.CustomMetrics == null)
                {
                    metricsConfig.CustomMetrics = new CustomMetrics();
                }
                metricsConfig.CustomMetrics.AddHistogram(
                    RPC_LATENCY_METRIC,
                    new HistogramBuckets(HistogramBucketKind.Exponential, 0.0005, 2.0, 20)
                    );
                _metricsReporter = new MetricsReporter(metricsConfig);
                pitayaMetrics    = _metricsReporter.GetPitayaPtr();
            }

            IntPtr err = pitaya_initialize_with_nats(
                IntPtr.Zero,
                envPrefix,
                configFile,
                Marshal.GetFunctionPointerForDelegate(_handleRpcCallback),
                Marshal.GetFunctionPointerForDelegate(_clusterNotificationCallback),
                logLevel,
                logKind,
                Marshal.GetFunctionPointerForDelegate(_logFunctionCallback),
                GCHandle.ToIntPtr(logCtx),
                pitayaMetrics,
                serverInfo.Handle,
                out pitaya
                );

            if (err != IntPtr.Zero)
            {
                var pitayaError = new PitayaError(pitaya_error_code(err), pitaya_error_message(err));
                pitaya_error_drop(err);
                throw new PitayaException($"Initialization failed: code={pitayaError.Code} msg={pitayaError.Message}");
            }

            _rpcClient = new RpcClient(pitaya, _serializer);
            _metricsReporter?.Start();
        }
示例#2
0
 private static extern IntPtr pitaya_initialize_with_nats(
     IntPtr context,
     string envPrefix,
     string configFile,
     IntPtr handleRpcCallback,
     IntPtr clusterNotificationCallback,
     NativeLogLevel logLevel,
     NativeLogKind logKind,
     IntPtr logFunction,
     IntPtr logCtx,
     IntPtr rawMetricsReporter,
     IntPtr serverInfo,
     out IntPtr pitaya);
        public static void Initialize(GrpcConfig grpcCfg,
                                      SDConfig sdCfg,
                                      Server server,
                                      NativeLogLevel logLevel,
                                      ServiceDiscoveryListener serviceDiscoveryListener = null,
                                      string logFile = "")
        {
            IntPtr grpcCfgPtr = new StructWrapper(grpcCfg);
            IntPtr sdCfgPtr   = new StructWrapper(sdCfg);
            IntPtr serverPtr  = new StructWrapper(server);

            bool ok = InitializeWithGrpcInternal(grpcCfgPtr, sdCfgPtr, serverPtr, logLevel,
                                                 logFile);

            if (!ok)
            {
                throw new PitayaException("Initialization failed");
            }

            AddServiceDiscoveryListener(serviceDiscoveryListener);
            ListenToIncomingRPCs();
        }
 private static extern bool InitializeWithGrpcInternal(IntPtr grpcCfg, IntPtr sdCfg, IntPtr server, NativeLogLevel logLevel, string logFile);