Skip to content

Implement cross-process coordination to reduce HTTP polling and stay within Azure rate limits#9

Draft
Copilot wants to merge 4 commits intomasterfrom
copilot/fix-8
Draft

Implement cross-process coordination to reduce HTTP polling and stay within Azure rate limits#9
Copilot wants to merge 4 commits intomasterfrom
copilot/fix-8

Conversation

Copy link

Copilot AI commented Jun 16, 2025

This PR implements a cross-process coordination system to address the rate limiting issue where multiple processes polling the Azure Scheduled Events endpoint can exceed the 5 requests/second per VM limit.

Problem

Currently, every process that uses this library queries the HTTP endpoint individually:

  • Rate limit: 5 requests/second per VM
  • Multiple processes = potential rate limit violations
  • Inefficient resource usage with duplicate HTTP calls

Solution

Implemented a file-based caching system with Named Mutex coordination:

Architecture

  • Global Lock: Uses Named Mutex (Azure.ScheduledEvents.GlobalLock) for cross-process coordination
  • Coordinator Process: First process to acquire lock becomes HTTP poller (every 10 seconds)
  • Client Processes: Read from shared cache file with 30-second expiry
  • Automatic Failover: Periodic lock checking (every 30 seconds) ensures takeover if coordinator dies

Key Components

  • ScheduledEventsCoordinator: Manages lock acquisition, HTTP polling, and file caching
  • File Cache: JSON data stored in %TEMP%\azure-scheduled-events-cache.json
  • Fallback Logic: Direct HTTP calls when coordination fails
  • Modified ScheduledEventsClient: Now uses coordinator instead of direct HTTP

Benefits

Reduces HTTP requests from N processes to 1 per VM
Stays within rate limits (5 requests/second per VM)
Zero breaking changes - existing API unchanged
Fault tolerant - automatic failover when coordinator process dies
Production ready - comprehensive error handling and logging

Usage

No code changes required - the existing API works identically:

var services = new ServiceCollection()
    .AddScheduledEventsClient()  // Now includes coordination
    .BuildServiceProvider();

var client = services.GetRequiredService<ScheduledEventsClient>();
var events = await client.GetScheduledEvents(); // Uses coordination automatically

Testing

Added comprehensive test suite:

  • Coordinator instantiation and cleanup
  • Multiple coordinator instance coordination
  • Cache file operations and expiry
  • HTTP timeout handling
  • Failover scenarios
  • Backward compatibility validation

All tests pass successfully, demonstrating robust coordination behavior.

Fixes #8.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copilot AI and others added 3 commits June 16, 2025 02:33
…-process coordination

Co-authored-by: MatthewSteeples <255971+MatthewSteeples@users.noreply.github.com>
…ror handling

Co-authored-by: MatthewSteeples <255971+MatthewSteeples@users.noreply.github.com>
…dination

Co-authored-by: MatthewSteeples <255971+MatthewSteeples@users.noreply.github.com>
Copilot AI changed the title [WIP] Reduce the amount of HTTP polling required Implement cross-process coordination to reduce HTTP polling and stay within Azure rate limits Jun 16, 2025
Copilot AI requested a review from MatthewSteeples June 16, 2025 02:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Reduce the amount of HTTP polling required

2 participants