void ParseRateLimits(string route, WebHeaderCollection headers) { RequestLimit requestlimit = limiter; string headervalue = headers["X-RateLimit-Limit"]; if (!string.IsNullOrEmpty(headervalue)) { int.TryParse(headervalue, out var limit); requestlimit.Limit = limit; } headervalue = headers["X-RateLimit-Remaining"]; if (!string.IsNullOrEmpty(headervalue)) { int.TryParse(headervalue, out var remaining); requestlimit.Remaining = remaining; } headervalue = headers["X-RateLimit-Reset"]; if (!string.IsNullOrEmpty(headervalue)) { int.TryParse(headervalue, out var seconds); requestlimit.Reset = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc) + TimeSpan.FromSeconds(seconds); } }
void EnterRequest(string route) { RequestLimit limit; lock (limiterlock) { if (!limiters.TryGetValue(route, out limit)) { limiters[route] = limit = new RequestLimit { Limit = int.MaxValue, Remaining = int.MaxValue, Reset = DateTime.Now } } ; Monitor.Enter(limit.Lock); } if (limit.Remaining == 0 && DateTime.Now < limit.Reset) { TimeSpan timetoreset = limit.Reset - DateTime.Now; if (timetoreset.TotalSeconds > 0.0) { Logger.Warning(this, $"Well, rate limits have been hit ... watiting for {timetoreset} now."); Thread.Sleep(timetoreset); } } } void ExitRequest(string route) { lock (limiterlock) Monitor.Exit(limiters[route].Lock); } T Request <T>(string route, string endpoint, params Parameter[] parameters)