示例#1
0
        /// <summary>
        /// Record a network issue has occurred trying to connect to this ip address
        /// </summary>
        /// <param name="ipAddress"></param>
        /// <returns></returns>
        public async Task RecordNetworkIssue(IPAddress ipAddress)
        {
            HostCacheManagerRecord record = await GetRecord(ipAddress.ToString());

            if (record == null)
            {
                return;
            }
            record.NetworkIssues++;
        }
示例#2
0
        public async Task UpdateHostSettings(HostInfo info)
        {
            HostCacheManagerRecord record = await GetRecord(info.Host);

            if (record == null)
            {
                return;
            }
            record.SetFromHostInfo(info);
        }
示例#3
0
        public async Task <HostInfo> GetHostInfo(string host)
        {
            HostCacheManagerRecord record = await GetRecord(host);

            if (record == null)
            {
                return(HostInfo.None);
            }

            return(record.GetHostInfo());
        }
示例#4
0
        /// <summary>
        /// Delay until we can fetch
        /// </summary>
        /// <param name="hostName"></param>
        /// <returns>True if we can fetch, false if timeout or cancelled</returns>
        public async Task <bool> WaitToFetch(string hostName, int timeoutMilliseconds)
        {
            DateTime startTime   = DateTime.UtcNow;
            bool     keepWaiting = true;
            bool     success     = false;
            HostCacheManagerRecord host_record = await GetRecord(hostName, true);

            while (keepWaiting)
            {
                try
                {
                    while (!await host_record.FetchWaitHandle.WaitAsync(360000))
                    {
                        Utility.LogInfo("Been waiting a long time to update a record: {0}", host_record.Host);
                    }

                    if (host_record.IsFetchable)
                    {
                        host_record.LastCall = DateTime.UtcNow;
                        host_record.TouchCount++;
                        keepWaiting = false;
                        success     = true;
                    }
                }
                catch (Exception ex)
                {
                    Utility.LogException(ex);
                }
                finally
                {
                    host_record.FetchWaitHandle.Release();
                }

                if (keepWaiting)
                {
                    if (timeoutMilliseconds != Timeout.Infinite && (DateTime.UtcNow - startTime).TotalMilliseconds > timeoutMilliseconds)
                    {
                        keepWaiting = false;
                    }
                    else
                    {
                        await Task.Delay(host_record.MaxFetchSpeedInMilliseconds);
                    }
                }
            }

            return(success);
        }
示例#5
0
        /// <summary>
        /// Retrives a record from the cache or creates a new one
        /// </summary>
        /// <param name="fromHost"></param>
        /// <returns></returns>
        async Task <HostCacheManagerRecord> GetRecord(string fromHost, bool createIfNotExists = false)
        {
            HostCacheManagerRecord record = null;

            try
            {
                while (!await hosts_lock.WaitAsync(360000))
                {
                    Utility.LogInfo("GetRecord waiting {0}", fromHost);
                }

                bool exists = hosts.ContainsKey(fromHost);

                // make it if its not there
                if (createIfNotExists)
                {
                    if (!exists)
                    {
                        hosts.Add(fromHost,
                                  new HostCacheManagerRecord()
                        {
                            Host          = fromHost,
                            LastCall      = DateTime.MinValue,
                            RobotsChecked = false,
                        }
                                  );
                    }
                    else
                    {
                        // bump the host
                        BumpHost(fromHost);
                    }

                    exists = true;
                }

                if (exists)
                {
                    // if there's too many domains in memory we drop one to avoid filling up memory with cached robots objects
                    if (exists && recencyQueue.Count > FetchoConfiguration.Current.HostCacheManagerMaxInMemoryDomainRecords)
                    {
                        string domainToDrop = recencyQueue.Dequeue();

                        record = hosts[domainToDrop];
                        record.Dispose();

                        hosts.Remove(domainToDrop);
                    }

                    record = hosts[fromHost];
                }
            }
            catch (Exception ex)
            {
                Utility.LogException(ex);
            }
            finally
            {
                hosts_lock.Release();
            }

            return(record);
        }
示例#6
0
 /// <summary>
 /// Returns how much time we'd have to wait before fetching
 /// </summary>
 /// <param name="fromHost"></param>
 /// <returns></returns>
 int GetTimeRemainingToWait(HostCacheManagerRecord host_record)
 => host_record.MaxFetchSpeedInMilliseconds - (int)((DateTime.UtcNow - host_record.LastCall).TotalMilliseconds);
示例#7
0
        public async Task <bool> HasHostExceedNetworkIssuesThreshold(IPAddress ipAddress)
        {
            HostCacheManagerRecord record = await GetRecord(ipAddress.ToString());

            return(record != null && record.NetworkIssues > FetchoConfiguration.Current.MaxNetworkIssuesThreshold);
        }