Exemple #1
0
        static async Task Main(string[] args)
        {
            clientState = ClientState.Failure;

            var taskBatches =
                Enumerable
                .Range(0, 1000)
                .Select(async c =>
            {
                var tasks =
                    Enumerable
                    .Range(0, 100)
                    .Select(MakeCall);

                var res = await Task.WhenAll(tasks);
                return(new List <int>(res));
            });
            var batches = await Task.WhenAll(taskBatches);

            var totalItems =
                batches
                .SelectMany(t => t)
                .Count();

            Console.WriteLine($"clientStats: totalClientCalls:{TotalClientCalls}, totalClientErrors: {TotalClientErrors}");
            Console.WriteLine($"callerStats: totalCallsMade:{TotalCallsMade}, totalErrorsReceived: {TotalErrorsReceived}");

            var tokenBucker = TokenBucket.Create(
                tokens: 50,
                timeWindow: TimeSpan.FromSeconds(1),
                returnTokens: 50
                );
        }
Exemple #2
0
        public bool EnqueueOutgoing(OutgoingPacket packet)
        {
            int category = (int)packet.Category;

            if (category >= 0 && category < m_packetOutboxes.Length)
            {
                LocklessQueue <OutgoingPacket> queue = m_packetOutboxes[category];
                TokenBucket bucket = m_throttleCategories[category];

                if (bucket.RemoveTokens(packet.Buffer.DataLength))
                {
                    // Enough tokens were removed from the bucket, the packet will not be queued
                    return(false);
                }
                else
                {
                    // Not enough tokens in the bucket, queue this packet
                    queue.Enqueue(packet);
                    return(true);
                }
            }
            else
            {
                // We don't have a token bucket for this category, so it will not be queued
                return(false);
            }
        }
Exemple #3
0
        public LLUDPServer(LLUDP udp, IScene scene, IPAddress bindAddress, int port, IConfigSource configSource, IScheduler scheduler)
            : base(bindAddress, port)
        {
            m_udp     = udp;
            Scene     = scene;
            Scheduler = scheduler;

            IConfig throttleConfig = configSource.Configs["LLUDP"];

            m_throttleRates = new ThrottleRates(LLUDPServer.MTU, throttleConfig);

            m_resendTimer = new TimedEvent(100);
            m_ackTimer    = new TimedEvent(500);
            m_pingTimer   = new TimedEvent(5000);

            m_httpServer = Scene.Simian.GetAppModule <IHttpServer>();

            IConfig config = configSource.Configs["LLUDP"];

            if (config != null)
            {
                m_asyncPacketHandling = config.GetBoolean("AsyncPacketHandling", true);
                m_recvBufferSize      = config.GetInt("SocketReceiveBufferSize", 0);
            }

            m_throttle = new TokenBucket(null, m_throttleRates.SceneTotalLimit, m_throttleRates.SceneTotal);

            PacketEvents = new PacketEventDictionary(Scheduler);

            Scene.OnPresenceRemove += PresenceRemoveHandler;
        }
 private void InitializeDefault()
 {
     for (TableIndex index = 0; index < TableIndex.Max; index++)
     {
         _buckets[(int)index] = new TokenBucket(new MetadataToken(index, 1));
     }
 }
Exemple #5
0
        private void InitCache()
        {
            var bucket = new TokenBucket(300, 5);

            _sharedWebCache    = new TokenCompliantCacheWrapper(new MemoryCacheMethod(), bucket);
            _sharedRenderCache = new MemoryCacheMethod();
        }
Exemple #6
0
        /// <summary>
        /// Default constructor
        /// </summary>
        public WSAgent(WebSockets server, TokenBucket parentThrottle, ThrottleRates rates,
                       UUID agentID, UUID sessionID, Socket socket, bool isChildAgent)
        {
            m_id           = agentID;
            m_server       = server;
            m_interestList = new InterestList(this, 200);

            IsChildPresence = isChildAgent;

            m_localID = m_server.Scene.CreateLocalID();

            //TextureEntry = new Primitive.TextureEntry(DEFAULT_AVATAR_TEXTURE);

            SessionID = sessionID;
            Socket    = socket;

            // Create a token bucket throttle for this client that has the scene token bucket as a parent
            m_throttle = new TokenBucket(parentThrottle, rates.ClientTotalLimit, rates.ClientTotal);
            // Create an array of token buckets for this clients different throttle categories
            m_throttleCategories = new TokenBucket[THROTTLE_CATEGORY_COUNT];

            for (int i = 0; i < THROTTLE_CATEGORY_COUNT; i++)
            {
                ThrottleCategory type = (ThrottleCategory)i;

                // Initialize the message outboxes, where messages sit while they are waiting for tokens
                m_messageOutboxes[i] = new LocklessQueue <OutgoingMessage>();
                // Initialize the token buckets that control the throttling for each category
                m_throttleCategories[i] = new TokenBucket(m_throttle, rates.GetLimit(type), rates.GetRate(type));
            }

            // Initialize this to a sane value to prevent early disconnects
            TickLastMessageReceived = Util.TickCount();
        }
Exemple #7
0
        public void TestSetRequestDripRateWithChildren()
        {
            TestHelpers.InMethod();

            TokenBucket tbParent = new TokenBucket("tbParent", null, 0, 0);
            TokenBucket tbChild1 = new TokenBucket("tbChild1", tbParent, 3000, 0);
            TokenBucket tbChild2 = new TokenBucket("tbChild2", tbParent, 5000, 0);

            AssertRates(tbParent, 8000, 8000, 8000, 0);
            AssertRates(tbChild1, 3000, 0, 3000, 0);
            AssertRates(tbChild2, 5000, 0, 5000, 0);

            // Test: Setting a parent request greater than total children requests.
            tbParent.RequestedDripRate = 10000;

            AssertRates(tbParent, 10000, 8000, 8000, 0);
            AssertRates(tbChild1, 3000, 0, 3000, 0);
            AssertRates(tbChild2, 5000, 0, 5000, 0);

            // Test: Setting a parent request lower than total children requests.
            tbParent.RequestedDripRate = 6000;

            AssertRates(tbParent, 6000, 8000, 6000, 0);
            AssertRates(tbChild1, 3000, 0, 6000 / 8 * 3, 0);
            AssertRates(tbChild2, 5000, 0, 6000 / 8 * 5, 0);
        }
Exemple #8
0
        public bool EnqueueOutgoing(OutgoingMessage message)
        {
            int category = (int)message.Category;

            if (category >= 0 && category < m_messageOutboxes.Length)
            {
                LocklessQueue <OutgoingMessage> queue = m_messageOutboxes[category];
                TokenBucket bucket = m_throttleCategories[category];

                if (bucket.RemoveTokens(message.Data.Length))
                {
                    // Enough tokens were removed from the bucket, the message will not be queued
                    return(false);
                }
                else
                {
                    // Not enough tokens in the bucket, queue this message
                    queue.Enqueue(message);
                    return(true);
                }
            }
            else
            {
                // We don't have a token bucket for this category, so it will not be queued
                return(false);
            }
        }
Exemple #9
0
        public virtual ValidationResult Validate()
        {
            int orGenerateAppToken = SingletonComponent <ServerMgr> .Instance.persistance.GetOrGenerateAppToken(Request.playerId);

            if (Request.playerId == 0L || Request.playerToken != orGenerateAppToken)
            {
                return(ValidationResult.NotFound);
            }
            if ((ServerUsers.Get(Request.playerId)?.group ?? ServerUsers.UserGroup.None) == ServerUsers.UserGroup.Banned)
            {
                return(ValidationResult.Banned);
            }
            TokenBucket tokenBucket = _playerBuckets?.Get(Request.playerId);

            if (tokenBucket == null || !tokenBucket.TryTake(TokenCost))
            {
                if (tokenBucket == null || !tokenBucket.IsNaughty)
                {
                    return(ValidationResult.RateLimit);
                }
                return(ValidationResult.Rejected);
            }
            UserId = Request.playerId;
            Player = BasePlayer.FindByID(UserId) ?? BasePlayer.FindSleeping(UserId);
            Client.Subscribe(new PlayerTarget(UserId));
            return(ValidationResult.Success);
        }
Exemple #10
0
 private bool Equals(TokenBucket other)
 {
     return(_capacity == other._capacity &&
            _tokensPerSecond.Equals(other._tokensPerSecond) &&
            _nanoTimeOfLastSend == other._nanoTimeOfLastSend &&
            _availableTokens == other._availableTokens);
 }
        private void InitCache()
        {
            var bucket = new TokenBucket(300, 5);

            _sharedTokenBucketMiddleware = new TokenComplianceMiddleware(bucket);
            _sharedWebCache    = new MemoryCacheMethod();
            _sharedRenderCache = new MemoryCacheMethod();
        }
Exemple #12
0
 private void AssertRates(
     TokenBucket tb, double requestedDripRate, double totalDripRequest, double dripRate, double maxDripRate)
 {
     Assert.AreEqual((int)requestedDripRate, tb.RequestedDripRate, "Requested drip rate");
     Assert.AreEqual((int)totalDripRequest, tb.TotalDripRequest, "Total drip request");
     Assert.AreEqual((int)dripRate, tb.DripRate, "Drip rate");
     Assert.AreEqual((int)maxDripRate, tb.MaxDripRate, "Max drip rate");
 }
Exemple #13
0
        public void PolicyExecutesThePassedDelegate()
        {
            bool executed = false;
            var  policy   = TokenBucket.Create(50, TimeSpan.FromMinutes(1), 2);

            policy.Execute(() => executed = true);

            executed.Should().BeTrue();
        }
 public RequestManager(ILogger logger, TokenBucket bucketRps, TokenBucket bucketRpm, ClientSettings settings, AccessToken token, Func <AccessToken, Task <AccessToken> > refresh)
 {
     _logger    = logger;
     _bucketRps = bucketRps;
     _bucketRpm = bucketRpm;
     _settings  = settings;
     _refresh   = refresh;
     _token     = token;
 }
Exemple #15
0
 public void Sets_Properties(int count, int interval)
 {
     using (var t = new TokenBucket(count, interval))
     {
         Assert.Equal(count, t.Capacity);
         Assert.Equal(interval, t.GetProperty <System.Timers.Timer>("Clock").Interval);
         Assert.Equal(count, t.GetProperty <long>("CurrentCount"));
     }
 }
Exemple #16
0
        public async Task GetAsync_Decrements_Count_By_Requested_Count()
        {
            using (var t = new TokenBucket(10, 10000))
            {
                await t.GetAsync(5);

                Assert.Equal(5, t.GetProperty <long>("CurrentCount"));
            }
        }
Exemple #17
0
        public void SetCapacity_Sets_Capacity(int count)
        {
            using (var t = new TokenBucket(10, 1000))
            {
                t.SetCapacity(count);

                Assert.Equal(count, t.Capacity);
            }
        }
        private void InitializeTable(IDotNetDirectory netDirectory)
        {
            var tableStream = netDirectory.Metadata.GetStream <TablesStream>();

            for (TableIndex index = 0; index < TableIndex.Max; index++)
            {
                var table = tableStream.GetTable(index);
                _buckets[(int)index] = new TokenBucket(new MetadataToken(index, (uint)table.Count + 1));
            }
        }
Exemple #19
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="maxThrdNum">最大工作线程数</param>
        /// <param name="maxUploadNum">每次最大上传个数</param>
        /// <param name="tokenBucket">令牌池</param>
        public LogMonitor(int maxThrdNum, int maxUploadNum, TokenBucket <TLog> tokenBucket)
        {
            _maxThrdNum   = maxThrdNum;
            _maxUploadNum = maxUploadNum;
            _tokenBucket  = tokenBucket;

            _start      = false;
            _dataQueue  = new ConcurrentQueue <TLog>();
            _curThrdNum = 0;
        }
Exemple #20
0
        public async Task GetAsync_Returns_Capacity_If_Request_Exceeds_Capacity()
        {
            using (var t = new TokenBucket(10, 10000))
            {
                int tokens = 0;
                var ex     = await Record.ExceptionAsync(async() => tokens = await t.GetAsync(11));

                Assert.Null(ex);
                Assert.Equal(10, tokens);
            }
        }
        public void TestTokenBucketWithRateAdjustmentFactor()
        {
            TokenBucket tb     = new TokenBucket(1000, 2000);
            long        delay1 = tb.GetMillisecondsDelay(1000, 0.5);

            Assert.Equal(0, delay1);

            long delay2 = tb.GetMillisecondsDelay(1000, 0.5);

            Assert.InRange(delay2, 800, 1000);
        }
Exemple #22
0
        public void SetCount_Throws_ArgumentOutOfRangeException_Given_Negative_Count()
        {
            using (var t = new TokenBucket(10, 1000))
            {
                var ex = Record.Exception(() => t.SetCapacity(-1));

                Assert.NotNull(ex);
                Assert.IsType <ArgumentOutOfRangeException>(ex);
                Assert.Equal("capacity", ((ArgumentOutOfRangeException)ex).ParamName);
            }
        }
Exemple #23
0
        public async Task GetAsync_Returns_Available_Tokens_If_Request_Exceeds_Available_Count()
        {
            using (var t = new TokenBucket(10, 10000))
            {
                await t.GetAsync(6);

                var count = await t.GetAsync(6);

                Assert.Equal(4, count);
            }
        }
Exemple #24
0
        public WebSocketServer(WebSockets server)
        {
            m_server = server;

            m_receiveBufferSize = MTU;
            m_readPool          = new ObjectPool <SocketAsyncEventArgs>(0, CreateSocketArgs);

            // TODO: Support throttle config
            m_throttleRates = new ThrottleRates(MTU, null);
            m_throttle      = new TokenBucket(null, m_throttleRates.SceneTotalLimit, m_throttleRates.SceneTotal);
        }
Exemple #25
0
        public void ReplaceMeWithRealTests()
        {
            /*
             * This test is for illustrative purposes, to show the interfaces a typical synchronous non-generic policy fulfills.
             * Real tests should check policy behaviour.
             */
            var policy = TokenBucket.Create(50, TimeSpan.FromMinutes(1), 2);

            policy.Should().BeAssignableTo <ISyncPolicy>();
            policy.Should().BeAssignableTo <ITokenBucket>();
        }
Exemple #26
0
        /// <summary>
        /// 初始化日志
        /// </summary>
        static Logger()
        {
            ReleaseRefDLL("zlib32.dll", Properties.Resources.zlib32);
            ReleaseRefDLL("zlib64.dll", Properties.Resources.zlib64);

            var logger = LoggerFactory.CreateLogger(Config.Environment);

            _tokenBucket = new TokenBucket <LogBase>(Config.MaxHandleNum);

            _monitor = new LogMonitor <LogBase>(Config.MaxThrdNum, Config.MaxUploadNum, _tokenBucket);
            _monitor.Start(logger);
        }
Exemple #27
0
        public void TestSetRequestDripRate()
        {
            TestHelpers.InMethod();

            TokenBucket tb = new TokenBucket("tb", null, 5000, 0);
            AssertRates(tb, 5000, 0, 5000, 0);

            tb.RequestedDripRate = 4000;
            AssertRates(tb, 4000, 0, 4000, 0);

            tb.RequestedDripRate = 6000;
            AssertRates(tb, 6000, 0, 6000, 0);
        }
Exemple #28
0
        public async Task Return_Adds_Given_Value()
        {
            using (var t = new TokenBucket(10, 1000000))
            {
                await t.GetAsync(5);

                Assert.Equal(5, t.GetProperty <long>("CurrentCount"));

                t.Return(5);

                Assert.Equal(10, t.GetProperty <long>("CurrentCount"));
            }
        }
Exemple #29
0
        public async Task Return_Does_Not_Change_Count_Given_Negative()
        {
            using (var t = new TokenBucket(10, 1000000))
            {
                await t.GetAsync(5);

                Assert.Equal(5, t.GetProperty <long>("CurrentCount"));

                t.Return(-5);

                Assert.Equal(5, t.GetProperty <long>("CurrentCount"));
            }
        }
Exemple #30
0
        public async Task GetAsync_Waits_For_Reset_If_Bucket_Is_Depleted()
        {
            using (var t = new TokenBucket(1, 10))
            {
                await t.GetAsync(1);

                await t.GetAsync(1);

                await t.GetAsync(1);

                Assert.True(true);
            }
        }
Exemple #31
0
        public void TestSetRequestDripRate()
        {
            TestHelpers.InMethod();

            TokenBucket tb = new TokenBucket("tb", null, 5000, 0);

            AssertRates(tb, 5000, 0, 5000, 0);

            tb.RequestedDripRate = 4000;
            AssertRates(tb, 4000, 0, 4000, 0);

            tb.RequestedDripRate = 6000;
            AssertRates(tb, 6000, 0, 6000, 0);
        }
Exemple #32
0
        public void ThrottleMode_must_accurately_interleave_replenish_and_consume()
        {
            var bucket = new TokenBucket(100, 100, 0L, 20);
            var bucket1 = bucket.TryConsumeTokens(0L, 10);
            bucket1.Item1.ShouldBe(new TokenBucket(100, 100, 0L, 10));
            bucket1.Item2.ShouldBeTrue();

            var bucket2 = bucket1.Item1.TryConsumeTokens(HalfSecond, 60);
            bucket2.Item1.ShouldBe(new TokenBucket(100, 100, HalfSecond, 0));
            bucket2.Item2.ShouldBeTrue();

            var bucket3 = bucket2.Item1.TryConsumeTokens(HalfSecond * 2, 40);
            bucket3.Item1.ShouldBe(new TokenBucket(100, 100, HalfSecond * 2, 10));
            bucket3.Item2.ShouldBeTrue();

            var bucket4 = bucket3.Item1.TryConsumeTokens(HalfSecond * 3, 70);
            bucket4.Item1.ShouldBe(new TokenBucket(100, 100, HalfSecond * 2, 10));
            bucket4.Item2.ShouldBeFalse();
        }
Exemple #33
0
        public void ThrottleMode_must_in_tokenbucket_mode_allow_consuming_tokens_up_to_capacity()
        {
            var bucket = new TokenBucket(100, 100, 0L, 100);
            var bucket1 = bucket.TryConsumeTokens(0L, 10);
            bucket1.Item1.ShouldBe(new TokenBucket(100, 100, 0L, 90));
            bucket1.Item2.ShouldBeTrue();

            var bucket2 = bucket1.Item1.TryConsumeTokens(0L, 40);
            bucket2.Item1.ShouldBe(new TokenBucket(100, 100, 0L, 50));
            bucket2.Item2.ShouldBeTrue();

            var bucket3 = bucket2.Item1.TryConsumeTokens(0L, 50);
            bucket3.Item1.ShouldBe(new TokenBucket(100, 100, 0L, 0));
            bucket3.Item2.ShouldBeTrue();

            var bucket4 = bucket3.Item1.TryConsumeTokens(0L, 1);
            bucket4.Item1.ShouldBe(new TokenBucket(100, 100, 0L, 0));
            bucket4.Item2.ShouldBeFalse();
        }
Exemple #34
0
        public void ThrottleMode_must_accurately_replenish_tokens()
        {
            var bucket = new TokenBucket(100, 100, 0L, 0);
            var bucket1 = bucket.TryConsumeTokens(0L, 0);
            bucket1.Item1.ShouldBe(new TokenBucket(100, 100, 0L, 0));
            bucket1.Item2.ShouldBeTrue();

            var bucket2 = bucket1.Item1.TryConsumeTokens(HalfSecond, 0);
            bucket2.Item1.ShouldBe(new TokenBucket(100, 100, HalfSecond, 50));
            bucket2.Item2.ShouldBeTrue();

            var bucket3 = bucket2.Item1.TryConsumeTokens(HalfSecond * 2, 0);
            bucket3.Item1.ShouldBe(new TokenBucket(100, 100, HalfSecond * 2, 100));
            bucket3.Item2.ShouldBeTrue();

            var bucket4 = bucket3.Item1.TryConsumeTokens(HalfSecond * 3, 0);
            bucket4.Item1.ShouldBe(new TokenBucket(100, 100, HalfSecond * 3, 100));
            bucket4.Item2.ShouldBeTrue();
        }
Exemple #35
0
		public AniDB(int localPort, string clientName = "libanidbdotnet", int clientVer = 1, Encoding encoding = null,
		             string remoteHostName = "api.anidb.net", int remotePort = 9000)
		{
			Timeout = 20000;

			ClientName = clientName;
			ClientVer = clientVer;

			_encoding = encoding ?? Encoding.ASCII;

			_sentRequests = new ConcurrentDictionary<string, AniDBRequest>();

			_udpClient = new UdpClient(new IPEndPoint(IPAddress.Any, localPort));
			_udpClient.Connect(remoteHostName, remotePort);

			SessionKey = "";

			RecievePackets();

			//Magical maths that turns the burst length into a number of tokens (in this case, 30)
			_sendBucket = new TokenBucket<AniDBRequest>(MinSendDelay, AvgSendDelay,
			                                            BurstLength / (AvgSendDelay - MinSendDelay), true,
														SendPacket);
		}
        /// <summary>
        /// Default constructor
        /// </summary>
        /// <param name="server">Reference to the UDP server this client is connected to</param>
        /// <param name="rates">Default throttling rates and maximum throttle limits</param>
        /// <param name="parentThrottle">Parent HTB (hierarchical token bucket)
        /// that the child throttles will be governed by</param>
        /// <param name="circuitCode">Circuit code for this connection</param>
        /// <param name="agentID">AgentID for the connected agent</param>
        /// <param name="remoteEndPoint">Remote endpoint for this connection</param>
        /// <param name="defaultRTO">
        /// Default retransmission timeout for unacked packets.  The RTO will never drop
        /// beyond this number.
        /// </param>
        /// <param name="maxRTO">
        /// The maximum retransmission timeout for unacked packets.  The RTO will never exceed this number.
        /// </param>
        public LLUDPClient(
            LLUDPServer server, ThrottleRates rates, TokenBucket parentThrottle, uint circuitCode, UUID agentID,
            IPEndPoint remoteEndPoint, int defaultRTO, int maxRTO)
        {
            AgentID = agentID;
            RemoteEndPoint = remoteEndPoint;
            CircuitCode = circuitCode;
            m_udpServer = server;
            if (defaultRTO != 0)
                m_defaultRTO = defaultRTO;
            if (maxRTO != 0)
                m_maxRTO = maxRTO;

            // Create a token bucket throttle for this client that has the scene token bucket as a parent
            m_throttleClient = new AdaptiveTokenBucket(parentThrottle, rates.Total, rates.AdaptiveThrottlesEnabled);
            // Create a token bucket throttle for the total categary with the client bucket as a throttle
            m_throttleCategory = new TokenBucket(m_throttleClient, 0);
            // Create an array of token buckets for this clients different throttle categories
            m_throttleCategories = new TokenBucket[THROTTLE_CATEGORY_COUNT];

            m_cannibalrate = rates.CannibalizeTextureRate;
            
            for (int i = 0; i < THROTTLE_CATEGORY_COUNT; i++)
            {
                ThrottleOutPacketType type = (ThrottleOutPacketType)i;

                // Initialize the packet outboxes, where packets sit while they are waiting for tokens
                m_packetOutboxes[i] = new ThreadedClasses.NonblockingQueue<OutgoingPacket>();
                // Initialize the token buckets that control the throttling for each category
                m_throttleCategories[i] = new TokenBucket(m_throttleCategory, rates.GetRate(type));
            }

            // Default the retransmission timeout to one second
            RTO = m_defaultRTO;

            // Initialize this to a sane value to prevent early disconnects
            TickLastPacketReceived = Environment.TickCount;
        }
        /// <summary>
        ///     Default constructor
        /// </summary>
        /// <param name="server">Reference to the UDP server this client is connected to</param>
        /// <param name="rates">Default throttling rates and maximum throttle limits</param>
        /// <param name="parentThrottle">
        ///     Parent HTB (hierarchical token bucket)
        ///     that the child throttles will be governed by
        /// </param>
        /// <param name="circuitCode">Circuit code for this connection</param>
        /// <param name="agentID">AgentID for the connected agent</param>
        /// <param name="remoteEndPoint">Remote endpoint for this connection</param>
        /// <param name="defaultRTO"></param>
        /// <param name="maxRTO"></param>
        public LLUDPClient(LLUDPServer server, ThrottleRates rates, TokenBucket parentThrottle, uint circuitCode,
            UUID agentID, IPEndPoint remoteEndPoint, int defaultRTO, int maxRTO)
        {
            AgentID = agentID;
            RemoteEndPoint = remoteEndPoint;
            CircuitCode = circuitCode;
            m_udpServer = server;
            if (defaultRTO != 0)
                m_defaultRTO = defaultRTO;
            if (maxRTO != 0)
                m_maxRTO = maxRTO;

            // Create a token bucket throttle for this client that has the scene token bucket as a parent
            m_throttle = new TokenBucket(parentThrottle, rates.TotalLimit, 0);

            // remember the rates the client requested
            Rates = new int[(int) ThrottleOutPacketType.Count];

            for (int i = 0; i < (int) ThrottleOutPacketType.Count; i++)
            {
                PacketsCounts[i] = 0;
            }

            //Set the priorities for the different packet types
            //Higher is more important
            MapCatsToPriority[(int) ThrottleOutPacketType.Resend] = 7;
            MapCatsToPriority[(int) ThrottleOutPacketType.Land] = 1;
            MapCatsToPriority[(int) ThrottleOutPacketType.Wind] = 0;
            MapCatsToPriority[(int) ThrottleOutPacketType.Cloud] = 0;
            MapCatsToPriority[(int) ThrottleOutPacketType.Task] = 4;
            MapCatsToPriority[(int) ThrottleOutPacketType.Texture] = 2;
            MapCatsToPriority[(int) ThrottleOutPacketType.Asset] = 3;
            MapCatsToPriority[(int) ThrottleOutPacketType.Transfer] = 5;
            MapCatsToPriority[(int) ThrottleOutPacketType.State] = 5;
            MapCatsToPriority[(int) ThrottleOutPacketType.AvatarInfo] = 6;
            MapCatsToPriority[(int) ThrottleOutPacketType.OutBand] = 7;

            // Default the retransmission timeout to one second
            RTO = m_defaultRTO;

            // Initialize this to a sane value to prevent early disconnects
            TickLastPacketReceived = Environment.TickCount & Int32.MaxValue;
        }
        public LLUDPServer(
            IPAddress listenIP, ref uint port, int proxyPortOffsetParm, bool allow_alternate_port,
            IConfigSource configSource, AgentCircuitManager circuitManager)
            : base(listenIP, (int)port)
        {
            #region Environment.TickCount Measurement

            // Measure the resolution of Environment.TickCount
            TickCountResolution = 0f;
            for (int i = 0; i < 5; i++)
            {
                int start = Environment.TickCount;
                int now = start;
                while (now == start)
                    now = Environment.TickCount;
                TickCountResolution += (float)(now - start) * 0.2f;
            }
            m_log.Info("[LLUDPSERVER]: Average Environment.TickCount resolution: " + TickCountResolution + "ms");
            TickCountResolution = (float)Math.Ceiling(TickCountResolution);

            #endregion Environment.TickCount Measurement

            m_circuitManager = circuitManager;
            int sceneThrottleBps = 0;
            bool usePools = false;

            IConfig config = configSource.Configs["ClientStack.LindenUDP"];
            if (config != null)
            {
                m_asyncPacketHandling = config.GetBoolean("async_packet_handling", true);
                m_recvBufferSize = config.GetInt("client_socket_rcvbuf_size", 0);
                sceneThrottleBps = config.GetInt("scene_throttle_max_bps", 0);

                PrimUpdatesPerCallback = config.GetInt("PrimUpdatesPerCallback", 100);
                TextureSendLimit = config.GetInt("TextureSendLimit", 20);

                m_defaultRTO = config.GetInt("DefaultRTO", 0);
                m_maxRTO = config.GetInt("MaxRTO", 0);
                m_disableFacelights = config.GetBoolean("DisableFacelights", false);
                m_ackTimeout = 1000 * config.GetInt("AckTimeout", 60);
                m_pausedAckTimeout = 1000 * config.GetInt("PausedAckTimeout", 300);
            }
            else
            {
                PrimUpdatesPerCallback = 100;
                TextureSendLimit = 20;
                m_ackTimeout = 1000 * 60; // 1 minute
                m_pausedAckTimeout = 1000 * 300; // 5 minutes
            }

            // FIXME: This actually only needs to be done once since the PacketPool is shared across all servers.
            // However, there is no harm in temporarily doing it multiple times.
            IConfig packetConfig = configSource.Configs["PacketPool"];
            if (packetConfig != null)
            {
                PacketPool.Instance.RecyclePackets = packetConfig.GetBoolean("RecyclePackets", true);
                PacketPool.Instance.RecycleDataBlocks = packetConfig.GetBoolean("RecycleDataBlocks", true);
                usePools = packetConfig.GetBoolean("RecycleBaseUDPPackets", usePools);
            }

            #region BinaryStats
            config = configSource.Configs["Statistics.Binary"];
            m_shouldCollectStats = false;
            if (config != null)
            {
                m_shouldCollectStats = config.GetBoolean("Enabled", false);
                binStatsMaxFilesize = TimeSpan.FromSeconds(config.GetInt("packet_headers_period_seconds", 300));
                binStatsDir = config.GetString("stats_dir", ".");
                m_aggregatedBWStats = config.GetBoolean("aggregatedBWStats", false);
            }
            #endregion BinaryStats

            m_throttle = new TokenBucket(null, sceneThrottleBps);
            ThrottleRates = new ThrottleRates(configSource);

            if (usePools)
                EnablePools();
        }
Exemple #39
0
        public void TestSetRequestDripRateWithChildren()
        {
            TestHelpers.InMethod();

            TokenBucket tbParent = new TokenBucket("tbParent", null, 0, 0);
            TokenBucket tbChild1 = new TokenBucket("tbChild1", tbParent, 3000, 0);
            TokenBucket tbChild2 = new TokenBucket("tbChild2", tbParent, 5000, 0);

            AssertRates(tbParent, 8000, 8000, 8000, 0);
            AssertRates(tbChild1, 3000, 0, 3000, 0);
            AssertRates(tbChild2, 5000, 0, 5000, 0);

            // Test: Setting a parent request greater than total children requests.
            tbParent.RequestedDripRate = 10000;

            AssertRates(tbParent, 10000, 8000, 8000, 0);
            AssertRates(tbChild1, 3000, 0, 3000, 0);
            AssertRates(tbChild2, 5000, 0, 5000, 0);

            // Test: Setting a parent request lower than total children requests.
            tbParent.RequestedDripRate = 6000;

            AssertRates(tbParent, 6000, 8000, 6000, 0);
            AssertRates(tbChild1, 3000, 0, 6000 / 8 * 3, 0);
            AssertRates(tbChild2, 5000, 0, 6000 / 8 * 5, 0);
        }
Exemple #40
0
        public void ThrottleMode_must_allow_oversized_packets_through_by_loaning()
        {
            var bucket = new TokenBucket(100, 100, 0L, 20);
            var bucket1 = bucket.TryConsumeTokens(0L, 30);
            bucket1.Item1.ShouldBe(new TokenBucket(100, 100, 0L, 20));
            bucket1.Item2.ShouldBeFalse();

            var bucket2 = bucket1.Item1.TryConsumeTokens(HalfSecond, 110);
            bucket2.Item1.ShouldBe(new TokenBucket(100, 100, HalfSecond, -40));
            bucket2.Item2.ShouldBeTrue();

            var bucket3 = bucket2.Item1.TryConsumeTokens(HalfSecond * 2, 20);
            bucket3.Item1.ShouldBe(new TokenBucket(100, 100, HalfSecond, -40));
            bucket3.Item2.ShouldBeFalse();

            var bucket4 = bucket3.Item1.TryConsumeTokens(HalfSecond * 3,20);
            bucket4.Item1.ShouldBe(new TokenBucket(100, 100, HalfSecond * 3, 40));
            bucket4.Item2.ShouldBeTrue();
        }
        public void Initialise(uint port, IConfigSource configSource, AgentCircuitManager circuitManager)
        {
            IConfig networkConfig = configSource.Configs["Network"];
            IPAddress internalIP = IPAddress.Any;
            if (networkConfig != null)
                IPAddress.TryParse(networkConfig.GetString("internal_ip", "0.0.0.0"), out internalIP);

            InitThreadPool(15);

            base.Initialise(internalIP, (int)port);

            #region Environment.TickCount Measurement

            // Measure the resolution of Environment.TickCount
            TickCountResolution = 0f;
            for (int i = 0; i < 5; i++)
            {
                int start = Environment.TickCount;
                int now = start;
                while (now == start)
                    now = Environment.TickCount;
                TickCountResolution += (now - start)*0.2f;
            }
            //MainConsole.Instance.Info("[LLUDPSERVER]: Average Environment.TickCount resolution: " + TickCountResolution + "ms");
            TickCountResolution = (float) Math.Ceiling(TickCountResolution);

            #endregion Environment.TickCount Measurement

            m_circuitManager = circuitManager;
            int sceneThrottleBps = 0;

            IConfig config = configSource.Configs["ClientStack.LindenUDP"];
            if (config != null)
            {
                m_asyncPacketHandling = config.GetBoolean("async_packet_handling", false);
                m_recvBufferSize = config.GetInt("client_socket_rcvbuf_size", 0);
                sceneThrottleBps = config.GetInt("scene_throttle_max_bps", 0);

                PrimUpdatesPerCallback = config.GetInt("PrimUpdatesPerCallback", 60);
                AvatarUpdatesPerCallBack = config.GetInt("AvatarUpdatesPerCallback", 80);
                TextureSendLimit = config.GetInt("TextureSendLimit", 25);

                m_defaultRTO = config.GetInt("DefaultRTO", 1000);
                m_maxRTO = config.GetInt("MaxRTO", 20000);
                ClientTimeOut = config.GetInt("ClientTimeOut", 120);
            }
            else
            {
                PrimUpdatesPerCallback = 60;
                AvatarUpdatesPerCallBack = 80;
                TextureSendLimit = 25;
                ClientTimeOut = 120;
            }

            #region BinaryStats

            config = configSource.Configs["Statistics.Binary"];
            m_shouldCollectStats = false;
            if (config != null)
            {
                if (config.Contains("enabled") && config.GetBoolean("enabled"))
                {
                    if (config.Contains("collect_packet_headers"))
                        m_shouldCollectStats = config.GetBoolean("collect_packet_headers");
                    if (config.Contains("packet_headers_period_seconds"))
                    {
                        binStatsMaxFilesize = TimeSpan.FromSeconds(config.GetInt("region_stats_period_seconds"));
                    }
                    if (config.Contains("stats_dir"))
                    {
                        binStatsDir = config.GetString("stats_dir");
                    }
                }
                else
                {
                    m_shouldCollectStats = false;
                }
            }

            #endregion BinaryStats

            if (sceneThrottleBps != 0)
                m_throttle = new TokenBucket(null, sceneThrottleBps, 0);
            m_throttleRates = new ThrottleRates(configSource);
        }
Exemple #42
0
 private void AssertRates(
     TokenBucket tb, double requestedDripRate, double totalDripRequest, double dripRate, double maxDripRate)
 {
     Assert.AreEqual((int)requestedDripRate, tb.RequestedDripRate, "Requested drip rate");
     Assert.AreEqual((int)totalDripRequest, tb.TotalDripRequest, "Total drip request");
     Assert.AreEqual((int)dripRate, tb.DripRate, "Drip rate");
     Assert.AreEqual((int)maxDripRate, tb.MaxDripRate, "Max drip rate");
 }