public bool PreHandleInterceptors(List <IHandlerInterceptor> interceptors, SocketRequest request, Socket socket) { foreach (var interceptor in interceptors) { Stopwatch sw = new Stopwatch(); sw.Start(); var handleResult = interceptor.PreHandle(request); sw.Stop(); if (telemetry != null) { telemetry.Collect(new InterceptorExecutionTime(interceptor.ControllerName, interceptor.ActionName, sw.ElapsedMilliseconds)); } if (handleResult.CancelActionInvoke) { var response = (handleResult.ResponseSuccess ? ResponseStatus.SUCCESS : ResponseStatus.ERROR); request.ProcessResponse(ActionResult.Json( new OperationResult("", response, handleResult.Message), response, handleResult.Message), socket, null); return(false); } } return(true); }
public override void RunTask() { double cpu = GetCPU(); double memory = GetMemory(); if (telemetry != null) { telemetry.Collect(new HardwareUsage(cpu, memory, coreServer.CurrentThreadsCount())); } }
public IController InstantiateController(string name, RequestBody requestBody) { try { ControllerRegister register = GetControllerRegister(name); if (register == null) { throw new Exception($"Controller '{name}' not registered or not found"); } IDependencyInjectorMaker injector = null; object[] injectArgs = null; if (!name.Equals("ServerInfoController")) { IDependencyInjectionService diService = serviceManager.GetService <IDependencyInjectionService>(); injector = diService.GetInjectorMaker(name); if (injector != null) { Stopwatch sw = new Stopwatch(); sw.Start(); injectArgs = injector.BuildInjectValues(requestBody); sw.Stop(); if (telemetry != null) { telemetry.Collect(new DependencyInjectorExecutionTime(injector.ControllerName, sw.ElapsedMilliseconds)); } } } IController controller = (IController)Activator.CreateInstance(register.Type, injectArgs); return(controller); } catch (Exception ex) { ILoggingService logger = serviceManager.GetService <ILoggingService>(); string msg = ex.Message; if (ex.InnerException != null) { msg += ex.InnerException.Message; } logger.WriteLog($@"Instantiate controller '{name}' threw an exception. {msg}", name, "", ServerLogType.ERROR); throw new Exception($@"Instantiate controller '{name}' threw an exception. {msg}"); } }
public override object DoInBackGround(int p) { SocketRequest request = null; try { request = GetRequestSocket(); if (request == null) { return(null); } if (HasRequestErros(ref request)) { return(null); } controller = request.Controller; method = controller.GetType().GetMethod(request.Action); ValidateActionMethod(); controllerName = controller.GetType().Name; actionName = method.Name; WaitActionLockPendingCompletations(ref request, ref controller, ref actionName); if (method.GetCustomAttribute <SingleThreaded>() != null) { ActionLocker.AddLock(controller, actionName); } ResolveActionParameters(ref request); if (!ResolveInterceptors(ref request)) { return(null); } ParameterInfo[] methodParameters = method.GetParameters(); object[] parameterValues = new object[methodParameters.Length]; int methodParameterCount = methodParameters .Where(pr => request.Parameters.Any(rp => rp.Name.Equals(pr.Name))) .Count(); int parameterIndex = 0; int valueIndex = 0; foreach (RequestParameter rp in request.Parameters) { ParameterInfo parameterInfo = methodParameters.FirstOrDefault(mp => mp.Name.Equals(rp.Name)); if (parameterInfo == null) { parameterIndex += 1; continue; } parameterValues[valueIndex] = request.Parameters[parameterIndex].Value; parameterIndex += 1; valueIndex += 1; } ActionResult result = null; Stopwatch w = new Stopwatch(); w.Start(); if (method.ReturnType == null) { //void action method.Invoke(controller, parameterValues); result = ActionResult.Json(new OperationResult(null, 600, "")); } else if (method.ReturnType == typeof(ActionResult)) //ActionResult action { result = (ActionResult)method.Invoke(controller, parameterValues); } else { //user-defined object Action object returnObj = method.Invoke(controller, parameterValues); result = ActionResult.Json(new OperationResult(returnObj, 600, "")); } w.Stop(); if (telemetry != null) { telemetry.Collect(new ActionExecutionTime(controllerName, actionName, w.ElapsedMilliseconds)); } logger.WriteLog($"Request completed: {controllerName}/{actionName}; ~{w.ElapsedMilliseconds}ms"); if (w.ElapsedMilliseconds > 10000) { ServerAlertManager.CreateAlert(new ServerAlert(request.Controller.GetType().Name, request.Action, $"The action it is taking considerable time to execute ({w.ElapsedMilliseconds} ms). Review your code to improve performance.'")); } ActionLocker.ReleaseLock(controller, method.Name); request.ProcessResponse(result, clientSocket, requestBody.InTo); return(result); } catch (LockedActionException lockedEx) { ProcessErrorResponse(lockedEx, ref request); return(null); } catch (Exception ex) { if (method != null && controller != null) { ActionLocker.ReleaseLock(controller, actionName); } ProcessErrorResponse(ex, ref request); return(null); } }