Exemplo n.º 1
0
        private void InitializeConnectionPools()
        {
            var config = ConfigurationManager.Get(m_ConfigKey);

            if (string.IsNullOrWhiteSpace(config))
            {
                throw new ConfigurationException($"Config error: cannot load config '{m_ConfigKey}'.");
            }

            var poolsConfig = new Dictionary <string, ConnectionPoolConfig>();

            foreach (var item in JsonUtil.Deserialize <JsonFormatConfigItem[]>(config))
            {
                var poolConfig = ConnectionPoolConfig.Parse(item.ConnectionString);
                poolConfig.ShardNo = (item.ShardNo ?? string.Empty).Trim();

                poolsConfig[poolConfig.ShardNo] = poolConfig;
            }

            if (poolsConfig.Count <= 0)
            {
                throw new ConfigurationException("Config error: at least one connection string is required.");
            }

            ReloadConnectionPools(poolsConfig);
        }
Exemplo n.º 2
0
        public ConnectionPool(string host, int port, ConnectionPoolConfig config, bool isframed = false)
        {
            if (config == null)
            {
                throw new ArgumentNullException("config");
            }

            _target         = new Server(host, port, isframed);
            _config         = config;
            _socketSettings = config.SocketSettings ?? TSocketSettings.DefaultSettings;

            if (config.PoolSize > 0)
            {
                _useLimiter        = true;
                _connectionLimiter = new Semaphore(config.PoolSize, config.PoolSize);
            }
        }
Exemplo n.º 3
0
            public static ConnectionPoolConfig Parse(string config)
            {
                AssertUtil.ArgumentNotEmpty(config, nameof(config));

                var result = new ConnectionPoolConfig();

                foreach (string keyValuePair in config.Split(';', StringSplitOptions.RemoveEmptyEntries))
                {
                    var items = keyValuePair.Split('=', 2);
                    if (items.Length != 2)
                    {
                        throw new ConfigurationException($"Config error: cannot parse '{keyValuePair}'.");
                    }

                    var key           = items[0].Trim().ToLower();
                    var value         = items[1].Trim();
                    int resolvedValue = -1;

                    if (string.IsNullOrWhiteSpace(key) || string.IsNullOrWhiteSpace(value))
                    {
                        throw new ConfigurationException($"Config error: key and value cannot be empty string.");
                    }

                    switch (key)
                    {
                    case "server":
                        var hostPort = value.Split(',', 2);
                        result.Host = hostPort[0].Trim();
                        if (result.Host == ".")
                        {
                            result.Host = "localhost";
                        }

                        if (hostPort.Length == 2)
                        {
                            if (!int.TryParse(hostPort[1].Trim(), out resolvedValue))
                            {
                                throw new ConfigurationException($"Config error: cannot parse 'Port' to integer value.");
                            }

                            if (resolvedValue < 0 || resolvedValue > 65535)
                            {
                                throw new ConfigurationException($"Config error: 'Port' must be > 0 and < 65535.");
                            }

                            result.Port = resolvedValue;
                        }
                        break;

                    case "ssl":
                        if (string.Compare("True", value, true) == 0)
                        {
                            result.Ssl = true;
                        }
                        else if (string.Compare("False", value, true) == 0)
                        {
                            result.Ssl = false;
                        }
                        else
                        {
                            throw new ConfigurationException($"Config error: cannot parse 'Ssl' to bool value.");
                        }
                        break;

                    case "minpoolsize":
                        if (!int.TryParse(value, out resolvedValue))
                        {
                            throw new ConfigurationException($"Config error: cannot parse 'MinPoolSize' to integer value.");
                        }

                        if (resolvedValue < 0)
                        {
                            throw new ConfigurationException($"Config error: 'MinPoolSize' must be >= 0.");
                        }

                        result.MinPoolSize = resolvedValue;
                        break;

                    case "maxpoolsize":
                        if (!int.TryParse(value, out resolvedValue))
                        {
                            throw new ConfigurationException($"Config error: cannot parse 'MaxPoolSize' to integer value.");
                        }

                        if (resolvedValue <= 0)
                        {
                            throw new ConfigurationException($"Config error: 'MaxPoolSize' must be > 0.");
                        }

                        result.MaxPoolSize = resolvedValue;
                        break;

                    case "connecttimeout":
                        if (!int.TryParse(value, out resolvedValue))
                        {
                            throw new ConfigurationException($"Config error: cannot parse 'ConnectTimeout' to integer value.");
                        }

                        if (resolvedValue <= 0)
                        {
                            throw new ConfigurationException($"Config error: 'ConnectTimeout' must be > 0.");
                        }

                        result.ConnectTimeout = resolvedValue;
                        break;

                    case "readwritetimeout":
                        if (!int.TryParse(value, out resolvedValue))
                        {
                            throw new ConfigurationException($"Config error: cannot parse 'ReadWriteTimeout' to integer value.");
                        }

                        if (resolvedValue <= 0)
                        {
                            throw new ConfigurationException($"Config error: 'ReadWriteTimeout' must be > 0.");
                        }

                        result.ReadWriteTimeout = resolvedValue;
                        break;

                    case "connectionidletimeout":
                        if (!int.TryParse(value, out resolvedValue))
                        {
                            throw new ConfigurationException($"Config error: cannot parse 'ConnectionIdleTimeout' to integer value.");
                        }

                        if (resolvedValue <= 0)
                        {
                            throw new ConfigurationException($"Config error: 'ConnectionIdleTimeout' must be > 0.");
                        }

                        result.ConnectionIdleTimeout = resolvedValue;
                        break;

                    case "connectionbusytimeout":
                        if (!int.TryParse(value, out resolvedValue))
                        {
                            throw new ConfigurationException($"Config error: cannot parse 'ConnectionBusyTimeout' to integer value.");
                        }

                        if (resolvedValue <= 0)
                        {
                            throw new ConfigurationException($"Config error: 'ConnectionBusyTimeout' must be > 0.");
                        }

                        result.ConnectionBusyTimeout = resolvedValue;
                        break;

                    default:
                        throw new ConfigurationException($"Config error: cannot support key '{items[0].Trim()}'.");
                    }
                }

                if (string.IsNullOrWhiteSpace(result.Host))
                {
                    throw new ConfigurationException("Config error: 'Server' is required.");
                }

                return(result);
            }