コード例 #1
0
        internal static IDriver CreateDriver(Uri uri, Config config, IPooledConnectionFactory connectionFactory)
        {
            var logger = config.Logger;

            var parsedUri       = uri.ParseBoltUri(DefaultBoltPort);
            var routingContext  = uri.ParseRoutingContext();
            var routingSettings = new RoutingSettings(parsedUri, routingContext, config);

            var metrics = config.MetricsFactory?.CreateMetrics(config);
            var connectionPoolSettings = new ConnectionPoolSettings(config, metrics);

            var retryLogic = new ExponentialBackoffRetryLogic(config.MaxTransactionRetryTime, logger);

            IConnectionProvider connectionProvider = null;

            switch (parsedUri.Scheme.ToLower())
            {
            case "bolt":
                EnsureNoRoutingContext(uri, routingContext);
                connectionProvider = new ConnectionPool(parsedUri, connectionFactory, connectionPoolSettings, logger);
                break;

            case "bolt+routing":
                connectionProvider = new LoadBalancer(connectionFactory, routingSettings, connectionPoolSettings, logger);
                break;

            default:
                throw new NotSupportedException($"Unsupported URI scheme: {parsedUri.Scheme}");
            }

            return(new Internal.Driver(parsedUri, connectionProvider, retryLogic, logger, metrics));
        }
コード例 #2
0
        public void ShouldRetry(int index)
        {
            var mockLogger = new Mock <ILogger>();

            mockLogger.SetupGet(l => l.Level).Returns(LogLevel.Info);
            var retryLogic = new ExponentialBackoffRetryLogic(TimeSpan.FromSeconds(5), mockLogger.Object);

            Parallel.For(0, index, i => Retry(i, retryLogic));

            mockLogger.Verify(l => l.Info(It.IsAny <string>(), It.IsAny <Exception>()), Times.AtLeast(2 * index));
        }
コード例 #3
0
        public void ShouldRetry(int index)
        {
            var mockLogger = new Mock <IDriverLogger>();

            var retryLogic = new ExponentialBackoffRetryLogic(TimeSpan.FromSeconds(5), mockLogger.Object);

            Parallel.For(0, index, i => Retry(i, retryLogic));

            mockLogger.Verify(l => l.Warn(It.IsAny <Exception>(), It.IsAny <string>()),
                              Times.Exactly((int)Interlocked.Read(ref _globalCounter)));
        }
コード例 #4
0
        public void ShouldNotRetryOnError(string errorCode)
        {
            var mockLogger = new Mock <IDriverLogger>();
            var retryLogic = new ExponentialBackoffRetryLogic(TimeSpan.FromSeconds(30), mockLogger.Object);

            int count = 0;
            var e     = Record.Exception(() => retryLogic.Retry <int>(() =>
            {
                count++;
                throw ParseServerException(errorCode, "an error");
            }));

            e.Should().BeOfType <TransientException>();
            (e as TransientException).Code.Should().Be(errorCode);
            count.Should().Be(1);
            mockLogger.Verify(l => l.Warn(It.IsAny <Exception>(), It.IsAny <string>()), Times.Never);
        }
コード例 #5
0
        public void ShouldNotRetryOnError(string errorCode)
        {
            var mockLogger = new Mock <ILogger>();

            mockLogger.SetupGet(l => l.Level).Returns(LogLevel.Info);
            var retryLogic = new ExponentialBackoffRetryLogic(TimeSpan.FromSeconds(30), mockLogger.Object);
            var timer      = new Stopwatch();

            timer.Start();
            var e = Record.Exception(() => retryLogic.Retry <int>(() =>
            {
                throw ParseServerException(errorCode, "an error");
            }));

            timer.Stop();
            e.Should().BeOfType <TransientException>();
            (e as TransientException).Code.Should().Be(errorCode);
            timer.Elapsed.TotalMilliseconds.Should().BeLessThan(10);
            mockLogger.Verify(l => l.Info(It.IsAny <string>(), It.IsAny <Exception>()), Times.Never);
        }
コード例 #6
0
        /// <summary>
        ///     Returns a driver for a Neo4j instance with custom configuration.
        /// </summary>
        /// <param name="uri">
        ///     The URI to the Neo4j instance. Should be in the form
        ///     <c>bolt://&lt;server location&gt;:&lt;port&gt;</c>. If <c>port</c> is not supplied the default of <c>7687</c> will
        ///     be used.</param>
        /// <param name="authToken">Authentication to use, <see cref="AuthTokens" />.</param>
        /// <param name="config">
        ///     Configuration for the driver instance to use, if <c>null</c> <see cref="Config.DefaultConfig" />
        ///     is used.
        /// </param>
        /// <returns>A new driver to the database instance specified by the <paramref name="uri"/>.</returns>
        public static IDriver Driver(Uri uri, IAuthToken authToken, Config config = null)
        {
            config = config ?? Config.DefaultConfig;

            var parsedUri      = uri.ParseUri(DefaultBoltPort);
            var routingContext = uri.ParseRoutingContext();

            var routingSettings        = new RoutingSettings(routingContext);
            var connectionSettings     = new ConnectionSettings(parsedUri, authToken, config);
            var connectionPoolSettings = new ConnectionPoolSettings(config);

            var logger     = config.Logger;
            var retryLogic = new ExponentialBackoffRetryLogic(config.MaxTransactionRetryTime, logger);

            IConnectionProvider connectionProvider = null;

            switch (parsedUri.Scheme.ToLower())
            {
            case "bolt":
                if (routingContext.Count != 0)
                {
                    throw new ArgumentException($"Routing context are not supported with scheme 'bolt'. Given URI: '{uri}'");
                }
                connectionProvider = new ConnectionPool(parsedUri, connectionSettings, connectionPoolSettings, logger);
                break;

            case "bolt+routing":
                connectionProvider = new LoadBalancer(routingSettings, connectionSettings, connectionPoolSettings, logger);
                break;

            default:
                throw new NotSupportedException($"Unsupported URI scheme: {parsedUri.Scheme}");
            }

            return(new Internal.Driver(parsedUri, connectionProvider, retryLogic, logger));
        }