public bool CanAccess(string pattern, string clientAddress, int maxRequests, LimitUnit unit) { CleanUpExpiredData(); var requestKey = new RequestKey(pattern, clientAddress); if (!_requests.ContainsKey(requestKey)) { _requests[requestKey] = new List <DateTime>(); } var fromTime = unit switch { LimitUnit.Sec => _dateTime.Now.AddSeconds(-1), LimitUnit.Min => _dateTime.Now.AddMinutes(-1), LimitUnit.Hr => _dateTime.Now.AddHours(-1), _ => throw new ArgumentOutOfRangeException(nameof(unit), unit, null) }; var totalRequestFromTime = _requests[requestKey].Count(x => x >= fromTime); // if an api must be throttled not need to add request and refresh timer if (totalRequestFromTime >= maxRequests) { return(false); } _requests[requestKey].Add(_dateTime.Now); return(true); }
protected override void Execute(CodeActivityContext context) { // Open the config file and get the connection string Configuration config = WebConfigurationManager.OpenWebConfiguration("/RequestWeb"); ConnectionStringsSection css = (ConnectionStringsSection)config.GetSection("connectionStrings"); string connectionString = css.ConnectionStrings["Request"].ConnectionString; // Lookup the Request RequestDataContext dc = new RequestDataContext(connectionString); Request r = dc.Requests.SingleOrDefault(x => x.RequestKey == RequestKey.Get(context)); if (r == null) { throw new InvalidProgramException("The specified request (" + RequestKey.Get(context) + ") was not found"); } r.QC = true; r.AssignedDate = DateTime.UtcNow; r.AssignedOperator = null; // Update the Request record PersistRequest persist = context.GetExtension <PersistRequest>(); persist.AddRequest(r); }
public bool AsyncFetchContents(ChorusAccount chorusAccount, ChorusUrl chorusUrl, out ChorusServerException chorusException) { Uri requestUri = GetContentsUri(chorusAccount, chorusUrl); if (null == requestUri) { chorusException = null; return(true); } RequestKey requestKey = new RequestKey(chorusAccount, requestUri); lock (_lock) { ChorusContentsResponse chorusContentsResponse; if (_chorusContentsByServerUrl.TryGetValue(requestKey, out chorusContentsResponse)) { chorusException = chorusContentsResponse.ChorusException; return(chorusContentsResponse.IsComplete); } chorusException = null; if (!_fetchRequests.Add(requestKey)) { return(false); } } ActionUtil.RunAsync(() => FetchAndStoreContents(chorusAccount, requestUri), "Fetch from Chorus"); // Not L10N return(false); }
public async Task <IActionResult> Download([ModelBinder(typeof(RequestKeyBinder))] RequestKey Key) { if (GetValidateResult() is IActionResult ErrorResult) { return(ErrorResult); } var Token = HttpContext.RequestAborted; try { var Path = HttpContext.Request.Path + HttpContext.Request.QueryString; var Result = new PipelineStreamResult(); var Receive = await Store.GetReceiveAsync(Key, Token); await Receive.ConnectionAsync(Result, Token); return(Result); } catch (OperationCanceledException e) { if (Token.IsCancellationRequested) { return(BadRequest(string.Format(CancelMessage, e.Message))); } return(BadRequest(string.Format(TimeoutMessage, ConnectionTimeout))); } catch (InvalidOperationException e) { Logger.LogError(e, DownloadFail); return(BadRequest(string.Format(ErrorMessage, e.Message))); } }
protected IEnumerable <IRequestHandler <I, O, S, C, A> > InitHandler <I, O>(RequestKey key, bool reset) { try { return((from t in GetHandlerTypes(reset) let interfaces = t.GetTypeInfo().GetInterfaces() let attributes = t.GetTypeInfo().GetCustomAttributes(typeof(RequestHandlerAttribute)) where attributes.Any(attr => ((RequestHandlerAttribute)attr).Namespace == key.Namespace) && interfaces.Any(x => x.IsConstructedGenericType && x.FullName == typeof(IRequestHandler <I, O, S, C, A>).FullName && x.GetGenericArguments()[0] == key.InputType && x.GetGenericArguments()[1] == key.OutputType && x.GetGenericArguments()[2] == typeof(S) && x.GetGenericArguments()[3] == typeof(C) && x.GetGenericArguments()[4] == typeof(A)) select(IRequestHandler <I, O, S, C, A>) Activator.CreateInstance(t)).ToList()); } catch (Exception e) { if (!reset) { return(InitHandler <I, O>(key, true)); } else { var e2 = new ClientException($"no handler found for request {key.Namespace} {key.InputType.Name} {key.OutputType.Name}", e); e2.Data.Add("Key", key); throw e2; } } }
public static void InsertHeadersFromContextInto(HttpListenerContext _Context, Action <string, string> _CollectionAddFunction, string[] _ExcludeHeaderKeys = null) { if (_ExcludeHeaderKeys != null) { for (int i = 0; i < _ExcludeHeaderKeys.Length; i++) { _ExcludeHeaderKeys[i] = _ExcludeHeaderKeys[i].ToLower(); } } foreach (var RequestKey in _Context.Request.Headers.AllKeys) { var LoweredKey = RequestKey.ToLower(); if (!IllegalHttpRequestHeaders.Contains(LoweredKey)) { if (_ExcludeHeaderKeys != null && _ExcludeHeaderKeys.Contains(LoweredKey)) { continue; } var Values = _Context.Request.Headers.GetValues(RequestKey); foreach (var Value in Values) { _CollectionAddFunction?.Invoke(RequestKey, Value); } } } }
private void FetchAndStore <T>(Uri requestUri, Func <Uri, T> fetcher) { var key = new RequestKey(typeof(T), requestUri); try { var data = fetcher(requestUri); lock (_lock) { _responses[key] = new RemoteResponse(data, null); } } catch (Exception exception) { RemoteServerException remoteException = exception as RemoteServerException; // ReSharper disable once ConvertIfStatementToNullCoalescingExpression if (null == remoteException) { remoteException = new RemoteServerException( Resources.RemoteSession_FetchContents_There_was_an_error_communicating_with_the_server__ + exception.Message, exception); } lock (_lock) { _responses[key] = new RemoteResponse(null, remoteException); } } FireContentsAvailable(); }
protected bool AsyncFetch <T>(Uri requestUri, Func <Uri, T> fetcher, out RemoteServerException remoteException) { if (null == requestUri) { remoteException = null; return(true); } lock (_lock) { RemoteResponse response; var key = new RequestKey(typeof(T), requestUri); if (_responses.TryGetValue(key, out response)) { remoteException = response.Exception; return(true); } remoteException = null; if (!_fetchRequests.Add(key)) { return(false); } } ActionUtil.RunAsync(() => FetchAndStore(requestUri, fetcher)); return(false); }
protected override void Execute(CodeActivityContext context) { // Get the connection string DBConnection ext = context.GetExtension <DBConnection>(); if (ext == null) { throw new InvalidProgramException("No connection string available"); } // Lookup the Request RequestDataContext dc = new RequestDataContext(ext.ConnectionString); Request r = dc.Requests.SingleOrDefault(x => x.RequestKey == RequestKey.Get(context)); if (r == null) { throw new InvalidProgramException("The specified request (" + RequestKey.Get(context) + ") was not found"); } // Update the Request record r.ActionTaken = ActionTaken.Get(context); r.RouteNext = RouteNext.Get(context); PersistRequest persist = context.GetExtension <PersistRequest>(); persist.AddRequest(r); context.SetValue(Request, r); }
private void FetchAndStoreContents(ChorusAccount chorusAccount, Uri requestUri) { ChorusContents contents = new ChorusContents(); var key = new RequestKey(chorusAccount, requestUri); try { StoreContentsResponse(key, new ChorusContentsResponse(FetchContents(chorusAccount, requestUri), true)); } catch (Exception exception) { ChorusServerException chorusException = exception as ChorusServerException; // ReSharper disable once ConvertIfStatementToNullCoalescingExpression if (null == chorusException) { chorusException = new ChorusServerException( Resources.ChorusSession_FetchContents_There_was_an_error_communicating_with_the_server__ + exception.Message, exception); } StoreContentsResponse(key, new ChorusContentsResponse(contents, true) { ChorusException = chorusException, }); } finally { lock (_lock) { _fetchRequests.Remove(key); } } }
/** * When we get a reply ack, we can remove the item from our cache, * we know the other guy got our reply */ protected void HandleReplyAck(ReqrepType rt, int idnum, MemBlock err_data, ISender ret_path) { RequestKey rk = new RequestKey(idnum, ret_path); lock ( _sync ) { /** * This is not completely safe, but probably fine. Consider the * case where: * A -(req)-> B * A timeout but B does get the req * A <-(rep)- B * A -(req)-> B (these cross in flight) * A -(repack)-> B * * but then the repack passes the req retransmission (out of order * delivery) * * This is unlikely, but we could improve it. * @todo improve the reply caching algorithm */ ReplyState rs = (ReplyState)_reply_cache[rk]; if (rs != null) { ReleaseReplyState(rs); } } }
public void Release(RequestKey requestKey) { if (_waitHandles.ContainsKey(requestKey)) { _waitHandles.Remove(requestKey); } }
// Methods ///// /** Create a ReplyState for a new Request * Note, this is not synchronized, you must hold the lock when calling! */ protected ReplyState GenerateReplyState(PType prefix, RequestKey rk) { var rs = new ReplyState(_prefix, rk); _reply_cache[rk] = rs; rs.LocalID = _reply_id_table.GenerateID(rs); return(rs); }
public void RequestKeyTest1(PathString path, IQueryCollection query, PathString ExpectedLocalPath, int ExpectedReceivers, string ExpectToString) { var Key = new RequestKey(path, query); Assert.AreEqual((string)ExpectedLocalPath, Key.Path); Assert.AreEqual(ExpectedReceivers, Key.Receivers); Assert.AreEqual(ExpectToString, Key.ToString()); }
public HomeController( IHttpContextAccessor httpContextAccessor, IHostingEnvironment hostingEnvironment, RequestKey requestKey) { this.HttpContextAccessor = httpContextAccessor; this.HostingEnvironment = hostingEnvironment; this.RequestKey = requestKey; }
public void Release <TResult>(RequestKey key, TResult result) { lock (this) { var waitHandle = (RequestWaitHandle <TResult>)_waitHandles[key]; _waitHandles.Remove(key); waitHandle.Release(result); } }
public void ReleaseWithException <TResult>(RequestKey key, Exception thrownException) { lock (this) { var waitHandle = (RequestWaitHandle <TResult>)_waitHandles[key]; _waitHandles.Remove(key); waitHandle.ReleaseWithException(thrownException); } }
public ReplyState(PType prefix, RequestKey rk) { _prefix = prefix; RequestKey = rk; RequestDate = DateTime.UtcNow; _reply_timeouts = 0; _uri = new WriteOnce <string>(); _lid = 0; }
public HomeController( IHttpContextAccessor httpContextAccessor, GracefullShutdownState gracefullShutdownState, RequestKey requestKey) { HttpContextAccessor = httpContextAccessor; GracefullShutdownState = gracefullShutdownState; RequestKey = requestKey; }
protected void HandleRequest(ReqrepType rt, int idnum, MemBlock rest, ISender retpath) { /** * Lets see if we have been asked this question before */ ReplyState rs = null; bool resend = false; #if REQREP_DEBUG Console.Error.WriteLine("[ReqrepManager: {0}] Receiving request id: {1}, from: {2}", _info, idnum, retpath); #endif RequestKey rk = new RequestKey(idnum, retpath); lock ( _sync ) { rs = (ReplyState)_reply_cache[rk]; if (rs == null) { rs = GenerateReplyState(_prefix, rk); } else { resend = true; } } if (resend) { //This is an old request: rs.Resend(); } else { //This is a new request: try { _sub.Handle(rest, rs); } catch { lock ( _sync ) { ReleaseReplyState(rs); } //This didn't work out: try { MemBlock err_data = MemBlock.Reference( new byte[] { (byte)ReqrepError.HandlerFailure }); ICopyable reply = MakeRequest(ReqrepType.Error, idnum, err_data); retpath.Send(reply); } catch { //If this fails, we may think about logging. //The return path could fail, that's the only obvious exception ///@todo log exception } } } }
private void StoreContentsResponse(RequestKey key, ChorusContentsResponse chorusContentsResponse) { lock (_lock) { _chorusContentsByServerUrl[key] = chorusContentsResponse; } var contentsAvailableEvent = ContentsAvailable; if (null != contentsAvailableEvent) { contentsAvailableEvent(); } }
public IRequestHandler <I, O, S, C, A> GetHandler <I, O>(IRequest <I, O> request) { // get or initialise handler. Safe to cast because init is generic. return(((IEnumerable <IRequestHandler <I, O, S, C, A> >)handlers .GetOrAdd(RequestKey.ForRequest(request), key => { return InitHandler <I, O>(key); })).OrderBy(handler => { RequestHandlerPriorityAttribute attr = handler.GetType().GetTypeInfo().GetCustomAttribute <RequestHandlerPriorityAttribute>(); return attr == null ? 0 : attr.Priority; }).Where(handler => (handler is IConditionalHandler) ? ((IConditionalHandler)handler).CanHandle(request.Content) : true) .First()); }
protected override void Execute(CodeActivityContext context) { // Open the config file and get the connection string Configuration config = WebConfigurationManager.OpenWebConfiguration("/RequestWeb"); ConnectionStringsSection css = (ConnectionStringsSection)config.GetSection("connectionStrings"); string connectionString = css.ConnectionStrings["Request"].ConnectionString; // Lookup the Queue RequestDataContext dc = new RequestDataContext(connectionString); Queue q = null; string queue = QueueName.Get(context); if (queue != null && queue.Length > 0 && queue != "None") { q = dc.Queues.SingleOrDefault(x => x.QueueName == QueueName.Get(context)); if (q == null) { throw new InvalidProgramException("The specified queue (" + QueueName.Get(context) + ") was not found"); } } // Lookup the Request Request r = dc.Requests.SingleOrDefault(x => x.RequestKey == RequestKey.Get(context)); if (r == null) { throw new InvalidProgramException("The specified request (" + RequestKey.Get(context) + ") was not found"); } if (q != null) { r.CurrentQueueID = q.QueueID; r.AssignedDate = DateTime.UtcNow; r.AssignedOperator = null; } else { r.CurrentQueueID = null; r.AssignedDate = null; r.AssignedOperator = null; } // Update the Request record PersistRequest persist = context.GetExtension <PersistRequest>(); persist.AddRequest(r); }
public bool TryGetAwaitable <TResult>(RequestKey key, Func <Task <TResult> > loaderTask, out Task <TResult> awaitable) { object awaitableTask; if (_waitHandles.TryGetValue(key, out awaitableTask)) { awaitable = (Task <TResult>)awaitableTask; return(true); } else { awaitable = loaderTask(); _waitHandles[key] = awaitable; return(false); } }
public override bool Equals(object o) { if (o == this) { return(true); } RequestKey rk = o as RequestKey; if (rk != null) { return((rk.RequestID == this.RequestID) && rk.Sender.Equals(this.Sender)); } else { return(false); } }
public async ValueTask <IActionResult> Options([ModelBinder(typeof(RequestKeyBinder))] RequestKey Key) { var Token = HttpContext.RequestAborted; if (Option.EnableContentOfHeadMethod) { return(Options()); } var AccessControlAllowMethods = await Store.GetOptionMethodsAsync(Key, Token); var Headers = HttpContext.Response.Headers; Headers.Add("Access-Control-Allow-Origin", "*"); Headers.Add("Access-Control-Allow-Methods", string.Join(", ", AccessControlAllowMethods)); Headers.Add("Access-Control-Max-Age", "-1"); throw new NotImplementedException(); }
public bool ShouldWaitForHandle <TResult>(RequestKey key, out RequestWaitHandle <TResult> waitHandle) { lock (this) { object waitHandleObject; if (_waitHandles.TryGetValue(key, out waitHandleObject)) { waitHandle = (RequestWaitHandle <TResult>)waitHandleObject; return(true); } waitHandle = new RequestWaitHandle <TResult>(); _waitHandles[key] = waitHandle; return(false); } }
protected void RetryFetch <T>(Uri requestUri, Func <Uri, T> fetcher) { if (requestUri == null) { return; } var key = new RequestKey(typeof(T), requestUri); lock (_lock) { if (_fetchRequests.Contains(key)) { return; } _responses.Remove(key); RemoteServerException exceptionIgnore; AsyncFetch(requestUri, fetcher, out exceptionIgnore); } }
public void RetryFetchContents(ChorusAccount chorusAccount, ChorusUrl chorusUrl) { Uri requestUri = GetContentsUri(chorusAccount, chorusUrl); if (null == requestUri) { return; } RequestKey requestKey = new RequestKey(chorusAccount, requestUri); lock (_lock) { if (_fetchRequests.Contains(requestKey)) { return; } _chorusContentsByServerUrl.Remove(requestKey); ChorusServerException exceptionIgnore; AsyncFetchContents(chorusAccount, chorusUrl, out exceptionIgnore); } }
public IRequestHandler <I, O, S, C, A> GetHandler <I, O>(IRequest <I, O> request) { // get or initialise handler. Safe to cast because init is generic. try { return(((IEnumerable <IRequestHandler <I, O, S, C, A> >)handlers .GetOrAdd(RequestKey.ForRequest(request), key => { return InitHandler <I, O>(key, false); })).OrderBy(handler => { RequestHandlerPriorityAttribute attr = handler.GetType().GetTypeInfo().GetCustomAttribute <RequestHandlerPriorityAttribute>(); return attr == null ? 0 : attr.Priority; }).Where(handler => (handler is IConditionalHandler) ? ((IConditionalHandler)handler).CanHandle(request.Content) : true) .First()); } catch (InvalidOperationException e) { throw new ClientException($"No handler found for request {request.Namespace} {typeof(I).Name} {typeof(O).Name}", e); } }
public ReplyState(RequestKey rk) { RequestKey = rk; RequestDate = DateTime.UtcNow; _reply_timeouts = 0; _uri = new WriteOnce<string>(); _lid = 0; }
public ReplyState(RequestKey rk) { RequestKey = rk; RequestDate = DateTime.UtcNow; _reply_timeouts = 0; }
public ReplyState(PType prefix, RequestKey rk) { _prefix = prefix; RequestKey = rk; RequestDate = DateTime.UtcNow; _reply_timeouts = 0; _uri = new WriteOnce<string>(); _lid = 0; }
// Methods ///// /** Create a ReplyState for a new Request * Note, this is not synchronized, you must hold the lock when calling! */ protected ReplyState GenerateReplyState(PType prefix, RequestKey rk) { var rs = new ReplyState(_prefix, rk); _reply_cache[rk] = rs; rs.LocalID = _reply_id_table.GenerateID(rs); return rs; }
/** * When we get a reply ack, we can remove the item from our cache, * we know the other guy got our reply */ protected void HandleReplyAck(ReqrepType rt, int idnum, MemBlock err_data, ISender ret_path) { RequestKey rk = new RequestKey(idnum, ret_path); lock( _sync ) { /** * This is not completely safe, but probably fine. Consider the * case where: * A -(req)-> B * A timeout but B does get the req * A <-(rep)- B * A -(req)-> B (these cross in flight) * A -(repack)-> B * * but then the repack passes the req retransmission (out of order * delivery) * * This is unlikely, but we could improve it. * @todo improve the reply caching algorithm */ ReplyState rs = (ReplyState)_reply_cache[rk]; if( rs != null ) { ReleaseReplyState(rs); } } }
protected void HandleRequest(ReqrepType rt, int idnum, MemBlock rest, ISender retpath) { /** * Lets see if we have been asked this question before */ ReplyState rs = null; bool resend = false; #if REQREP_DEBUG Console.Error.WriteLine("[ReqrepManager: {0}] Receiving request id: {1}, from: {2}", _info, idnum, retpath); #endif RequestKey rk = new RequestKey(idnum, retpath); lock( _sync ) { rs = (ReplyState)_reply_cache[rk]; if( rs == null ) { rs = GenerateReplyState(_prefix, rk); } else { resend = true; } } if( resend ) { //This is an old request: rs.Resend(); } else { //This is a new request: try { _sub.Handle(rest, rs); } catch { lock( _sync ) { ReleaseReplyState(rs); } //This didn't work out: try { MemBlock err_data = MemBlock.Reference( new byte[]{ (byte) ReqrepError.HandlerFailure } ); ICopyable reply = MakeRequest(ReqrepType.Error, idnum, err_data); retpath.Send(reply); } catch { //If this fails, we may think about logging. //The return path could fail, that's the only obvious exception ///@todo log exception } } } }