public void AllowRequestOrThrow(string endpoint, ulong major, DateTimeOffset now) { if (IsGloballyRateLimited(now)) { _logger.Warning("Globally rate limited until {GlobalRateLimitExpiry}, cancelling request", _globalRateLimitExpiry); throw new GloballyRatelimitedException(); } var bucket = _buckets.GetBucket(endpoint, major); if (bucket == null) { // No rate limit for this endpoint (yet), allow through _logger.Debug("No rate limit data for endpoint {Endpoint}, allowing through", endpoint); return; } bucket.Tick(now); if (bucket.TryAcquire()) { // We're allowed to send it! :) return; } // We can't send this request right now; retrying... var waitTime = bucket.GetResetDelay(now); // add a small buffer for Timing:tm: waitTime += TimeSpan.FromMilliseconds(50); // (this is caught by a WaitAndRetry Polly handler, if configured) throw new RatelimitBucketExhaustedException(bucket, waitTime); }
public void BucketManagerTest() { var bucketmgr = new BucketManager(TimeSpan.FromSeconds(3)); var b1 = bucketmgr.GetBucket(DateTime.UtcNow); b1.AppendBlock.Push(1); var b2 = bucketmgr.GetBucket(DateTime.UtcNow.AddSeconds(3)); b2.AppendBlock.Push(2); var buckets = bucketmgr.GetBuckets(DateTime.UtcNow.AddSeconds(6)); Assert.AreEqual(2, buckets.Count); }
public override IEnumerator ExecuteDemoInstruction(InstructionBase instruction, bool increment) { // Gather information from instruction BucketSortInstruction bucketInstruction = null; BucketSortElement sortingElement = null; if (instruction is BucketSortInstruction) { bucketInstruction = (BucketSortInstruction)instruction; // Change internal state of sorting element sortingElement = sortMain.ElementManager.GetSortingElement(bucketInstruction.SortingElementID).GetComponent <BucketSortElement>(); bucketIndex = bucketInstruction.BucketID; } if (instruction is InstructionLoop) { InstructionLoop loopInst = (InstructionLoop)instruction; i = loopInst.I; j = loopInst.J; k = loopInst.K; loopType = loopInst.LoopType; } // Remove highlight from previous instruction pseudoCodeViewer.ChangeColorOfText(prevHighlightedLineOfCode, Util.BLACKBOARD_TEXT_COLOR); // Gather part of code to highlight int lineOfCode = Util.NO_VALUE; useHighlightColor = Util.HIGHLIGHT_STANDARD_COLOR; switch (instruction.Instruction) { case Util.FIRST_INSTRUCTION: lineOfCode = FirstInstructionCodeLine(); SetBuckets(increment); break; case UtilSort.CREATE_BUCKETS_INST: lineOfCode = 1; SetBuckets(increment); break; case UtilSort.FIRST_LOOP: lineOfCode = 2; if (increment) { SetLengthOfList(); useHighlightColor = UseConditionColor(i != lengthOfListInteger); } else { if (i == 0) { lengthOfList = "len(list)"; } else { i -= 1; } } break; case UtilSort.BUCKET_INDEX_INST: lineOfCode = 3; PreparePseudocodeValue(sortingElement.Value, 1); bucketIndex = bucketInstruction.BucketID; if (increment) { sortingElement.IsCompare = bucketInstruction.IsCompare; bucketIndexStr = bucketIndex.ToString(); } else { sortingElement.IsCompare = !bucketInstruction.IsCompare; bucketIndexStr = "list[i] * N / MAX_VALUE"; } UtilSort.IndicateElement(sortingElement.gameObject); break; case UtilSort.MOVE_TO_BUCKET_INST: lineOfCode = 4; if (increment) { PreparePseudocodeValue(sortingElement.Value, 1); bucketIndex = bucketInstruction.BucketID; sortingElement.IsCompare = bucketInstruction.IsCompare; } else { element1Value = "list[i]"; //bucketIndex = ""; // TODO ? sortingElement.IsCompare = !bucketInstruction.IsCompare; } UtilSort.IndicateElement(sortingElement.gameObject); break; case UtilSort.END_LOOP_INST: switch (loopType) { case UtilSort.OUTER_LOOP: lineOfCode = 5; break; case UtilSort.INNER_LOOP: lineOfCode = 12; break; default: Debug.LogError(UtilSort.END_LOOP_INST + ": '" + loopType + "' loop not found"); break; } break; case UtilSort.PHASING_INST: lineOfCode = 6; i = 0; // ???? j = (bucketSortManager.NumberOfBuckets - 1); bucketIndex = j; // Sort buckets bucketManager.AutoSortBuckets(); break; case UtilSort.DISPLAY_ELEMENT: // TODO: Fix //sortMain.WaitForSupportToComplete++; // add to list? StartCoroutine(bucketManager.PutElementsForDisplay(bucketInstruction.BucketID)); if (!increment) { bucketManager.GetBucket(bucketIndex).SetEnterTrigger(true); } break; case Util.SET_VAR_K: lineOfCode = 7; break; case UtilSort.UPDATE_LOOP_INST: if (loopType == UtilSort.OUTER_LOOP) // 2nd loop (outher) { //j = 0; lineOfCode = 8; if (increment) { numberOfBuckets = bucketSortManager.NumberOfBuckets.ToString(); useHighlightColor = UseConditionColor(i != j); } else { if (i > 0) { i -= 1; } else { numberOfBuckets = "N"; } } } else // 2nd loop (inner) { lineOfCode = 9; if (increment) { bucketSize = k.ToString(); useHighlightColor = UseConditionColor(j != k); } else { if (j > 0) { j--; } else { bucketSize = k.ToString(); } } } break; case UtilSort.MOVE_BACK_INST: lineOfCode = 10; k = bucketInstruction.NextHolderID; // ??? if (increment) { PreparePseudocodeValue(sortingElement.Value, 2); sortingElement.IsSorted = bucketInstruction.IsSorted; } else { element2Value = "buckets[" + i + "][" + j + "]"; sortingElement.IsSorted = !bucketInstruction.IsSorted; } break; case Util.UPDATE_VAR_K: lineOfCode = 11; if (increment) { kPlus1 = (k + 1).ToString(); } else { //if (k > 0) // kPlus1 = (k - 1).ToString(); //else kPlus1 = "k + 1"; } break; case Util.FINAL_INSTRUCTION: lineOfCode = FinalInstructionCodeLine(); IsTaskCompleted = increment; break; } // Highlight part of code in pseudocode if (instruction.Instruction == UtilSort.DISPLAY_ELEMENT) { yield return(null); } else { yield return(HighlightPseudoCode(CollectLine(lineOfCode), useHighlightColor)); prevHighlightedLineOfCode = lineOfCode; } // Move sorting element if (instruction is BucketSortInstruction) { switch (bucketInstruction.Instruction) { case UtilSort.MOVE_TO_BUCKET_INST: if (increment) { sortingElement.transform.position = bucketManager.GetBucket(bucketInstruction.BucketID).transform.position + UtilSort.ABOVE_BUCKET_VR; } else { sortingElement.transform.position = bucketSortManager.GetCorrectHolder(bucketInstruction.HolderID).transform.position + UtilSort.ABOVE_HOLDER_VR; sortingElement.gameObject.SetActive(true); bucketManager.GetBucket(bucketIndex).RemoveSortingElement(sortingElement); sortingElement.RigidBody.constraints = RigidbodyConstraints.None; } break; case UtilSort.DISPLAY_ELEMENT: if (increment) { sortingElement.transform.position = bucketManager.GetBucket(bucketInstruction.BucketID).transform.position + UtilSort.ABOVE_BUCKET_VR; sortingElement.gameObject.SetActive(true); } else { sortingElement.gameObject.SetActive(false); } break; case UtilSort.MOVE_BACK_INST: if (increment) { sortingElement.transform.position = bucketSortManager.GetCorrectHolder(bucketInstruction.NextHolderID).transform.position + UtilSort.ABOVE_HOLDER_VR; } else { sortingElement.transform.position = bucketManager.GetBucket(bucketInstruction.BucketID).transform.position + UtilSort.ABOVE_BUCKET_VR; } break; } } // Display element reports itself when it's done if (instruction.Instruction != UtilSort.DISPLAY_ELEMENT) { sortMain.WaitForSupportToComplete--; } }
public override int PrepareNextInstruction(InstructionBase instruction) { Debug.Log(instruction.DebugInfo()); bool gotSortingElement = !bucketSort.SkipDict[Util.SKIP_NO_ELEMENT].Contains(instruction.Instruction); bool noDestination = bucketSort.SkipDict[Util.SKIP_NO_DESTINATION].Contains(instruction.Instruction); switch (instruction.Instruction) { case UtilSort.PHASING_INST: // Phase into Insertion Sort? bucketManager.AutoSortBuckets(); break; case UtilSort.DISPLAY_ELEMENT: Debug.Log("Display elements"); // Display elements on top of bucket sortMain.WaitForSupportToComplete++; StartCoroutine(bucketManager.PutElementsForDisplay(((BucketSortInstruction)instruction).BucketID)); return(1); // Nothing to do for the player, nor any pseudocode } if (instruction is BucketSortInstruction) { // Get the next instruction BucketSortInstruction bucketSortInstruction = (BucketSortInstruction)instruction; // Get the Sorting element BucketSortElement sortingElement = sortMain.ElementManager.GetSortingElement(bucketSortInstruction.SortingElementID).GetComponent <BucketSortElement>(); // Hands out the next instruction sortingElement.Instruction = bucketSortInstruction; Bucket bucket = bucketManager.GetBucket(bucketSortInstruction.BucketID); bucket.BucketSortInstruction = bucketSortInstruction; // Give this sorting element permission to give feedback to progress to next intstruction if (instruction.Instruction == UtilSort.MOVE_TO_BUCKET_INST || instruction.Instruction == UtilSort.MOVE_BACK_INST) { sortingElement.NextMove = true; } } // Display help on blackboard if (sortMain.SortSettings.Difficulty <= Util.BEGINNER) { sortMain.WaitForSupportToComplete++; StartCoroutine(sortMain.GetTeachingAlgorithm().UserTestHighlightPseudoCode(instruction, gotSortingElement)); } Debug.Log("Element: " + gotSortingElement + ", no destination: " + noDestination); if (gotSortingElement && !noDestination) { return(0); } Debug.Log(">>>>>>>>>>>>>>>>>>>>>>>>>>>> Nothing for player to do, continuing to next instruction"); return(1); }