コード例 #1
0
ファイル: SockIOPool.cs プロジェクト: yanh19930226/yunExpress
        public void CheckIn(SockIO socket, bool addToAvail)
        {
            if (socket == null)
            {
                return;
            }

            string host = socket.Host;

            //if(Log.IsDebugEnabled)
            //{
            //    Log.Debug("Calling check-in on socket: " + socket.ToString() + " for host: " + host);
            //}

            // remove from the busy pool
            //if(Log.IsDebugEnabled)
            //{
            //    Log.Debug("Removing socket (" + socket.ToString() + ") from busy pool for host: " + host);
            //}
            RemoveSocketFromPool(_busyPool, host, socket);

            // add to avail pool
            if (addToAvail && socket.IsConnected)
            {
                //if(Log.IsDebugEnabled)
                //{
                //    Log.Debug("Returning socket (" + socket.ToString() + " to avail pool for host: " + host);
                //}
                AddSocketToPool(_availPool, host, socket);
            }
        }
コード例 #2
0
        /// <summary>
        /// 根据缓存Key获取其所存储的服务器信息
        /// </summary>
        /// <param name="key">缓存Key</param>
        /// <returns>Key所存储的服务器信息</returns>
        public static string GetHostInfoByCacheKey(string key)
        {
            string hostInfo = "";

            SockIO sock = null;

            try
            {
                sock = SockIOPool.GetInstance(SocketPoolName).GetSock(key);

                if (sock != null)
                {
                    hostInfo = sock.Host;
                }
            }
            finally
            {
                if (sock != null)
                {
                    sock.Close();
                }
            }

            return(hostInfo);
        }
コード例 #3
0
ファイル: SockIOPool.cs プロジェクト: yanh19930226/yunExpress
        protected static void RemoveSocketFromPool(Hashtable pool, string host, SockIO socket)
        {
            if (host != null && host.Length == 0 || pool == null)
            {
                return;
            }

            if (pool.ContainsKey(host))
            {
                Hashtable sockets = (Hashtable)pool[host];
                if (sockets != null)
                {
                    sockets.Remove(socket);
                }
            }
        }
コード例 #4
0
ファイル: SockIOPool.cs プロジェクト: yanh19930226/yunExpress
        /// <summary>
        /// Adds a socket to a given pool for the given host.
        ///
        /// Internal utility method.
        /// </summary>
        /// <param name="pool">pool to add to</param>
        /// <param name="host">host this socket is connected to</param>
        /// <param name="socket">socket to add</param>
        //[MethodImpl(MethodImplOptions.Synchronized)]
        protected static void AddSocketToPool(Hashtable pool, string host, SockIO socket)
        {
            if (pool == null)
            {
                return;
            }

            Hashtable sockets;

            if (host != null && host.Length != 0 && pool.ContainsKey(host))
            {
                sockets = (Hashtable)pool[host];
                if (sockets != null)
                {
                    sockets[socket] = DateTime.Now; //MaxM 1.16.05: Use current DateTime to indicate socker creation time rather than milliseconds since 1970

                    return;
                }
            }

            sockets         = new Hashtable();
            sockets[socket] = DateTime.Now; //MaxM 1.16.05: Use current DateTime to indicate socker creation time rather than milliseconds since 1970
            pool[host]      = sockets;
        }
コード例 #5
0
ファイル: SockIOPool.cs プロジェクト: yanh19930226/yunExpress
        public SockIO GetConnection(string host)
        {
            if (!_initialized)
            {
                //if(Log.IsErrorEnabled)
                //{
                //    Log.Error(GetLocalizedString("get socket from uninitialized pool"));
                //}
                return(null);
            }

            if (host == null)
            {
                return(null);
            }

            // if we have items in the pool
            // then we can return it
            if (_availPool != null && !(_availPool.Count == 0))
            {
                // take first connected socket
                Hashtable aSockets = (Hashtable)_availPool[host];

                if (aSockets != null && !(aSockets.Count == 0))
                {
                    foreach (SockIO socket in new IteratorIsolateCollection(aSockets.Keys))
                    {
                        if (socket.IsConnected)
                        {
                            //if(Log.IsDebugEnabled)
                            //{
                            //    Log.Debug(GetLocalizedString("move socket").Replace("$$Host$$", host).Replace("$$Socket$$", socket.ToString()));
                            //}

                            // remove from avail pool
                            aSockets.Remove(socket);

                            // add to busy pool
                            AddSocketToPool(_busyPool, host, socket);

                            // return socket
                            return(socket);
                        }
                        else
                        {
                            // not connected, so we need to remove it
                            //if(Log.IsErrorEnabled)
                            //{
                            //    Log.Error(GetLocalizedString("socket not connected").Replace("$$Host$$", host).Replace("$$Socket$$", socket.ToString()));
                            //}

                            // remove from avail pool
                            aSockets.Remove(socket);
                        }
                    }
                }
            }

            // if here, then we found no sockets in the pool
            // try to create on a sliding scale up to maxCreate
            object cShift = _createShift[host];
            int    shift  = (cShift != null) ? (int)cShift : 0;

            int create = 1 << shift;

            if (create >= _maxCreate)
            {
                create = _maxCreate;
            }
            else
            {
                shift++;
            }

            // store the shift value for this host
            _createShift[host] = shift;

            //if(Log.IsDebugEnabled)
            //{
            //    Log.Debug(GetLocalizedString("creating sockets").Replace("$$Create$$", create.ToString(new NumberFormatInfo())));
            //}

            for (int i = create; i > 0; i--)
            {
                SockIO socket = CreateSocket(host);
                if (socket == null)
                {
                    break;
                }

                if (i == 1)
                {
                    // last iteration, add to busy pool and return sockio
                    AddSocketToPool(_busyPool, host, socket);
                    return(socket);
                }
                else
                {
                    // add to avail pool
                    AddSocketToPool(_availPool, host, socket);
                }
            }

            // should never get here
            return(null);
        }
コード例 #6
0
ファイル: SockIOPool.cs プロジェクト: yanh19930226/yunExpress
        /// <summary>
        /// Returns appropriate SockIO object given
        /// string cache key and optional hashcode.
        ///
        /// Trys to get SockIO from pool.  Fails over
        /// to additional pools in event of server failure.
        /// </summary>
        /// <param name="key">hashcode for cache key</param>
        /// <param name="hashCode">if not null, then the int hashcode to use</param>
        /// <returns>SockIO obj connected to server</returns>
        public SockIO GetSock(string key, object hashCode)
        {
            string hashCodeString = "<null>";

            if (hashCode != null)
            {
                hashCodeString = hashCode.ToString();
            }

            //if(Log.IsDebugEnabled)
            //{
            //    Log.Debug(GetLocalizedString("cache socket pick").Replace("$$Key$$", key).Replace("$$HashCode$$", hashCodeString));
            //}

            if (key == null || key.Length == 0)
            {
                //if(Log.IsDebugEnabled)
                //{
                //    Log.Debug(GetLocalizedString("null key"));
                //}
                return(null);
            }

            if (!_initialized)
            {
                //if(Log.IsErrorEnabled)
                //{
                //    Log.Error(GetLocalizedString("get socket from uninitialized pool"));
                //}
                return(null);
            }

            // if no servers return null
            if (_buckets.Count == 0)
            {
                return(null);
            }

            // if only one server, return it
            if (_buckets.Count == 1)
            {
                return(GetConnection((string)_buckets[0]));
            }

            int tries = 0;

            // generate hashcode
            int hv;

            if (hashCode != null)
            {
                hv = (int)hashCode;
            }
            else
            {
                // NATIVE_HASH = 0
                // OLD_COMPAT_HASH = 1
                // NEW_COMPAT_HASH = 2
                switch (_hashingAlgorithm)
                {
                case HashingAlgorithm.Native:
                    hv = key.GetHashCode();
                    break;

                case HashingAlgorithm.OldCompatibleHash:
                    hv = OriginalHashingAlgorithm(key);
                    break;

                case HashingAlgorithm.NewCompatibleHash:
                    hv = NewHashingAlgorithm(key);
                    break;

                default:
                    // use the native hash as a default
                    hv = key.GetHashCode();
                    _hashingAlgorithm = HashingAlgorithm.Native;
                    break;
                }
            }

            // keep trying different servers until we find one
            while (tries++ <= _buckets.Count)
            {
                // get bucket using hashcode
                // get one from factory
                int bucket = hv % _buckets.Count;
                if (bucket < 0)
                {
                    bucket += _buckets.Count;
                }

                SockIO sock = GetConnection((string)_buckets[bucket]);

                //if(Log.IsDebugEnabled)
                //{
                //    Log.Debug(GetLocalizedString("cache choose").Replace("$$Bucket$$", _buckets[bucket].ToString()).Replace("$$Key$$", key));
                //}

                if (sock != null)
                {
                    return(sock);
                }

                // if we do not want to failover, then bail here
                if (!_failover)
                {
                    return(null);
                }

                // if we failed to get a socket from this server
                // then we try again by adding an incrementer to the
                // current key and then rehashing
                switch (_hashingAlgorithm)
                {
                case HashingAlgorithm.Native:
                    hv += ((string)("" + tries + key)).GetHashCode();
                    break;

                case HashingAlgorithm.OldCompatibleHash:
                    hv += OriginalHashingAlgorithm("" + tries + key);
                    break;

                case HashingAlgorithm.NewCompatibleHash:
                    hv += NewHashingAlgorithm("" + tries + key);
                    break;

                default:
                    // use the native hash as a default
                    hv += ((string)("" + tries + key)).GetHashCode();
                    _hashingAlgorithm = HashingAlgorithm.Native;
                    break;
                }
            }

            return(null);
        }
コード例 #7
0
ファイル: SockIOPool.cs プロジェクト: yanh19930226/yunExpress
        /// <summary>
        /// Creates a new SockIO obj for the given server.
        ///
        ///If server fails to connect, then return null and do not try
        ///again until a duration has passed.  This duration will grow
        ///by doubling after each failed attempt to connect.
        /// </summary>
        /// <param name="host">host:port to connect to</param>
        /// <returns>SockIO obj or null if failed to create</returns>
        protected SockIO CreateSocket(string host)
        {
            SockIO socket = null;

            // if host is dead, then we don't need to try again
            // until the dead status has expired
            // we do not try to put back in if failover is off
            if (_failover && _hostDead.ContainsKey(host) && _hostDeadDuration.ContainsKey(host))
            {
                DateTime store  = (DateTime)_hostDead[host];
                long     expire = ((long)_hostDeadDuration[host]);

                if ((store.AddMilliseconds(expire)) > DateTime.Now)
                {
                    return(null);
                }
            }

            try
            {
                socket = new SockIO(this, host, _socketTimeout, _socketConnectTimeout, _nagle);

                if (!socket.IsConnected)
                {
                    //if(Log.IsErrorEnabled)
                    //{
                    //    Log.Error(GetLocalizedString("failed to get socket").Replace("$$Host$$", host));
                    //}
                    try
                    {
                        socket.TrueClose();
                    }
                    catch                    //(SocketException ex)
                    {
                        //if(Log.IsErrorEnabled)
                        //{
                        //    Log.Error(GetLocalizedString("failed to close socket on host").Replace("$$Host$$", host), ex);
                        //}
                        socket = null;
                    }
                }
            }
            catch            //(SocketException ex)
            {
                //if(Log.IsErrorEnabled)
                //{
                //    Log.Error(GetLocalizedString("failed to get socket").Replace("$$Host$$", host), ex);
                //}
                socket = null;
            }
            //catch(ArgumentException ex)
            //{
            //    if(Log.IsErrorEnabled)
            //    {
            //        Log.Error(GetLocalizedString("failed to get socket").Replace("$$Host$$", host), ex);
            //    }
            //    socket = null;
            //}
            //catch(IOException ex)
            //{
            //    if(Log.IsErrorEnabled)
            //    {
            //        Log.Error(GetLocalizedString("failed to get socket").Replace("$$Host$$", host), ex);
            //    }
            //    socket = null;
            //}

            // if we failed to get socket, then mark
            // host dead for a duration which falls off
            if (socket == null)
            {
                DateTime now = DateTime.Now;
                _hostDead[host] = now;
                long expire = (_hostDeadDuration.ContainsKey(host)) ? (((long)_hostDeadDuration[host]) * 2) : 100;
                _hostDeadDuration[host] = expire;
                //if(Log.IsDebugEnabled)
                //{
                //    Log.Debug(GetLocalizedString("ignoring dead host").Replace("$$Host$$", host).Replace("$$Expire$$", expire.ToString(new NumberFormatInfo())));
                //}

                // also clear all entries for this host from availPool
                ClearHostFromPool(_availPool, host);
            }
            else
            {
                //if(Log.IsDebugEnabled)
                //{
                //    Log.Debug(GetLocalizedString("created socket").Replace("$$ToString$$", socket.ToString()).Replace("$$Host$$", host));
                //}
                _hostDead.Remove(host);
                _hostDeadDuration.Remove(host);
                if (_buckets.BinarySearch(host) < 0)
                {
                    _buckets.Add(host);
                }
            }

            return(socket);
        }
コード例 #8
0
ファイル: SockIOPool.cs プロジェクト: yanh19930226/yunExpress
        public void Initialize()
        {
            // check to see if already initialized
            if (_initialized &&
                _buckets != null &&
                _availPool != null &&
                _busyPool != null)
            {
                //if(Log.IsErrorEnabled)
                //{
                //    Log.Error(GetLocalizedString("initializing initialized pool"));
                //}
                return;
            }

            // initialize empty maps
            _buckets          = new ArrayList();
            _availPool        = new Hashtable(_servers.Count * _initConns);
            _busyPool         = new Hashtable(_servers.Count * _initConns);
            _hostDeadDuration = new Hashtable();
            _hostDead         = new Hashtable();
            _createShift      = new Hashtable();
            _maxCreate        = (_poolMultiplier > _minConns) ? _minConns : _minConns / _poolMultiplier;                // only create up to maxCreate connections at once

            //if(Log.IsDebugEnabled)
            //{
            //    Log.Debug(GetLocalizedString("initializing pool")
            //        .Replace("$$InitConnections$$", InitConnections.ToString(new NumberFormatInfo()))
            //        .Replace("$$MinConnections$$", MinConnections.ToString(new NumberFormatInfo()))
            //        .Replace("$$MaxConnections$$", MaxConnections.ToString(new NumberFormatInfo())));
            //}

            // if servers is not set, or it empty, then
            // throw a runtime exception
            if (_servers == null || _servers.Count <= 0)
            {
                //if(Log.IsErrorEnabled)
                //{
                //    Log.Error(GetLocalizedString("initialize with no servers"));
                //}
                throw new ArgumentException(GetLocalizedString("initialize with no servers"));
            }

            for (int i = 0; i < _servers.Count; i++)
            {
                // add to bucket
                // with weights if we have them
                if (_weights != null && _weights.Count > i)
                {
                    for (int k = 0; k < ((int)_weights[i]); k++)
                    {
                        _buckets.Add(_servers[i]);
                        //if(Log.IsDebugEnabled)
                        //{
                        //    Log.Debug("Added " + _servers[i] + " to server bucket");
                        //}
                    }
                }
                else
                {
                    _buckets.Add(_servers[i]);
                    //if(Log.IsDebugEnabled)
                    //{
                    //    Log.Debug("Added " + _servers[i] + " to server bucket");
                    //}
                }

                // create initial connections
                //if(Log.IsDebugEnabled)
                //{
                //    Log.Debug(GetLocalizedString("create initial connections").Replace("$$InitConns$$", InitConnections.ToString(new NumberFormatInfo())).Replace("$$Servers[i]$$", Servers[i].ToString()));
                //}

                for (int j = 0; j < _initConns; j++)
                {
                    SockIO socket = CreateSocket((string)_servers[i]);
                    if (socket == null)
                    {
                        //if(Log.IsErrorEnabled)
                        //{
                        //    Log.Error(GetLocalizedString("failed to connect").Replace("$$Servers[i]$$", Servers[i].ToString()).Replace("$$j$$", j.ToString(new NumberFormatInfo())));
                        //}
                        break;
                    }

                    AddSocketToPool(_availPool, (string)_servers[i], socket);
                    //if(Log.IsDebugEnabled)
                    //{
                    //    Log.Debug(GetLocalizedString("created and added socket").Replace("$$ToString$$", socket.ToString()).Replace("$$Servers[i]$$", Servers[i].ToString()));
                    //}
                }
            }

            // mark pool as initialized
            _initialized = true;

            // start maint thread TODO: re-enable
            if (_maintThreadSleep > 0)
            {
                this.StartMaintenanceThread();
            }
        }
コード例 #9
0
ファイル: SockIOPool.cs プロジェクト: yanh19930226/yunExpress
        protected void SelfMaintain()
        {
            //if(Log.IsDebugEnabled)
            //{
            //    Log.Debug(GetLocalizedString("start self maintenance"));
            //}

            // go through avail sockets and create/destroy sockets
            // as needed to maintain pool settings
            foreach (string host in new IteratorIsolateCollection(_availPool.Keys))
            {
                Hashtable sockets = (Hashtable)_availPool[host];
                //if(Log.IsDebugEnabled)
                //{
                //    Log.Debug(GetLocalizedString("size of available pool").Replace("$$Host$$", host).Replace("$$Sockets$$", sockets.Count.ToString(new NumberFormatInfo())));
                //}

                // if pool is too small (n < minSpare)
                if (sockets.Count < _minConns)
                {
                    // need to create new sockets
                    int need = _minConns - sockets.Count;
                    //if(Log.IsDebugEnabled)
                    //{
                    //    Log.Debug(GetLocalizedString("need to create new sockets").Replace("$$Need$$", need.ToString(new NumberFormatInfo())).Replace("$$Host$$", host));
                    //}

                    for (int j = 0; j < need; j++)
                    {
                        SockIO socket = CreateSocket(host);

                        if (socket == null)
                        {
                            break;
                        }

                        AddSocketToPool(_availPool, host, socket);
                    }
                }
                else if (sockets.Count > _maxConns)
                {
                    // need to close down some sockets
                    int diff        = sockets.Count - _maxConns;
                    int needToClose = (diff <= _poolMultiplier)
                        ? diff
                        : (diff) / _poolMultiplier;

                    //if(Log.IsDebugEnabled)
                    //{
                    //    Log.Debug(GetLocalizedString("need to remove spare sockets").Replace("$$NeedToClose$$", needToClose.ToString(new NumberFormatInfo()).Replace("$$Host$$", host)));
                    //}
                    foreach (SockIO socket in new IteratorIsolateCollection(sockets.Keys))
                    {
                        if (needToClose <= 0)
                        {
                            break;
                        }

                        // remove stale entries
                        DateTime expire = (DateTime)sockets[socket];

                        // if past idle time
                        // then close socket
                        // and remove from pool
                        if ((expire.AddMilliseconds(_maxIdle)) < DateTime.Now)
                        {
                            //if(Log.IsDebugEnabled)
                            //{
                            //    Log.Debug(GetLocalizedString("removing stale entry"));
                            //}
                            try
                            {
                                socket.TrueClose();
                            }
                            catch//(IOException ioe)
                            {
                                //if(Log.IsErrorEnabled)
                                //{
                                //    Log.Error(GetLocalizedString("failed to close socket"), ioe);
                                //}
                            }

                            sockets.Remove(socket);
                            needToClose--;
                        }
                    }
                }

                // reset the shift value for creating new SockIO objects
                _createShift[host] = 0;
            }

            // go through busy sockets and destroy sockets
            // as needed to maintain pool settings
            foreach (string host in _busyPool.Keys)
            {
                Hashtable sockets = (Hashtable)_busyPool[host];
                //if(Log.IsDebugEnabled)
                //{
                //    Log.Debug(GetLocalizedString("size of busy pool").Replace("$$Host$$", host).Replace("$$Sockets$$", sockets.Count.ToString(new NumberFormatInfo())));
                //}

                // loop through all connections and check to see if we have any hung connections
                foreach (SockIO socket in new IteratorIsolateCollection(sockets.Keys))
                {
                    // remove stale entries
                    DateTime hungTime = (DateTime)sockets[socket];

                    // if past max busy time
                    // then close socket
                    // and remove from pool
                    if ((hungTime.AddMilliseconds(_maxBusyTime)) < DateTime.Now)
                    {
                        //if(Log.IsErrorEnabled)
                        //{
                        //    Log.Error(GetLocalizedString("removing hung connection").Replace("$$Time$$", (new TimeSpan(DateTime.Now.Ticks - hungTime.Ticks).TotalMilliseconds).ToString(new NumberFormatInfo())));
                        //}
                        try
                        {
                            socket.TrueClose();
                        }
                        catch//(IOException ioe)
                        {
                            //if(Log.IsErrorEnabled)
                            //{
                            //    Log.Error(GetLocalizedString("failed to close socket"), ioe);
                            //}
                        }

                        sockets.Remove(socket);
                    }
                }
            }

            //if(Log.IsDebugEnabled)
            //{
            //    Log.Debug(GetLocalizedString("end self maintenance"));
            //}
        }
コード例 #10
0
ファイル: SockIOPool.cs プロジェクト: yanh19930226/yunExpress
 public void CheckIn(SockIO socket)
 {
     CheckIn(socket, true);
 }