示例#1
0
        public override TResponse Request <TResponse>(RequestData requestData)
        {
            if (!_calls.ContainsKey(requestData.Uri.Port))
            {
                throw new Exception($"Expected a call to happen on port {requestData.Uri.Port} but received none");
            }

            try
            {
                var state = _calls[requestData.Uri.Port];
                if (IsSniffRequest(requestData))
                {
                    _ = Interlocked.Increment(ref state.Sniffed);
                    return(HandleRules <TResponse, ISniffRule>(
                               requestData,
                               nameof(VirtualCluster.Sniff),
                               _cluster.SniffingRules,
                               requestData.RequestTimeout,
                               (r) => UpdateCluster(r.NewClusterState),
                               (r) => SniffResponseBytes.Create(_cluster.Nodes, _cluster.ElasticsearchVersion, _cluster.PublishAddressOverride, _cluster.SniffShouldReturnFqnd)
                               ));
                }
                if (IsPingRequest(requestData))
                {
                    _ = Interlocked.Increment(ref state.Pinged);
                    return(HandleRules <TResponse, IRule>(
                               requestData,
                               nameof(VirtualCluster.Ping),
                               _cluster.PingingRules,
                               requestData.PingTimeout,
                               (r) => { },
                               (r) => null                  //HEAD request
                               ));
                }
                _ = Interlocked.Increment(ref state.Called);
                return(HandleRules <TResponse, IClientCallRule>(
                           requestData,
                           nameof(VirtualCluster.ClientCalls),
                           _cluster.ClientCallRules,
                           requestData.RequestTimeout,
                           (r) => { },
                           CallResponse
                           ));
            }
            catch (TheException e)
            {
                return(ResponseBuilder.ToResponse <TResponse>(requestData, e, null, null, Stream.Null));
            }
        }
示例#2
0
        public override TResponse Request <TResponse>(RequestData requestData)
        {
            this.Calls.Should().ContainKey(requestData.Uri.Port);
            try
            {
                var state = this.Calls[requestData.Uri.Port];
                if (IsSniffRequest(requestData))
                {
                    var sniffed = Interlocked.Increment(ref state.Sniffed);
                    return(HandleRules <TResponse, ISniffRule>(
                               requestData,
                               this._cluster.SniffingRules,
                               requestData.RequestTimeout,
                               (r) => this.UpdateCluster(r.NewClusterState),
                               (r) => SniffResponseBytes.Create(this._cluster.Nodes, this._cluster.PublishAddressOverride, this._cluster.SniffShouldReturnFqnd)
                               ));
                }
                if (IsPingRequest(requestData))
                {
                    var pinged = Interlocked.Increment(ref state.Pinged);
                    return(HandleRules <TResponse, IRule>(
                               requestData,
                               this._cluster.PingingRules,
                               requestData.PingTimeout,
                               (r) => { },
                               (r) => null                  //HEAD request
                               ));
                }
                var called = Interlocked.Increment(ref state.Called);
                return(HandleRules <TResponse, IClientCallRule>(
                           requestData,
                           this._cluster.ClientCallRules,
                           requestData.RequestTimeout,
                           (r) => { },
                           CallResponse
                           ));
            }
#if DOTNETCORE
            catch (System.Net.Http.HttpRequestException e)
#else
            catch (WebException e)
#endif
            {
                return(ResponseBuilder.ToResponse <TResponse>(requestData, e, null, null, Stream.Null));
            }
        }