Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable TCP connection sharing to improve raven performance #251

Closed
andreasohlund opened this issue Mar 27, 2012 · 6 comments
Closed

Enable TCP connection sharing to improve raven performance #251

andreasohlund opened this issue Mar 27, 2012 · 6 comments
Assignees
Milestone

Comments

@andreasohlund
Copy link
Member

And avoid:

From time to time "Raven" gives up throwing System.Net.Sockets.SocketException (0x80004005): An operation on a socket could not be performed because the system lacked sufficient buffer space or because a queue was full 127.0.0.1:8080

http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.unsafeauthenticatedconnectionsharing.aspx

To set it:
documentStore.JsonRequestFactory => req => ((HttpWebRequest)req).UnsafeAuthenticatedConnectionSharing = true;

@ghost ghost assigned andreasohlund Sep 14, 2012
@johnsimons
Copy link
Member

But according to the doco, this is only for when NTLM is used?

@ryanwatson
Copy link

I keep unfortunately coming back to this problem and it seems to occur when the server is just extremely busy. However it now seems to be a daily reoccurring theme that takes me ages to have to start stop and sometimes having to restart the server for things to slowly get back and running.

Is this due to a new port being for each request?

@johnsimons
Copy link
Member

A different way to configure this is;

store.JsonRequestFactory.ConfigureRequest += (sender, e) =>
                       {
                           var httpWebRequest = ((HttpWebRequest) e.Request);
                           httpWebRequest.UnsafeAuthenticatedConnectionSharing = true;
                           httpWebRequest.PreAuthenticate = true;
                       };

@ghost ghost assigned johannesg and johnsimons Jan 11, 2013
@johnsimons
Copy link
Member

Added way to enable it in 6383897

PeterLehmann pushed a commit to PeterLehmann/NServiceBus that referenced this issue Jan 11, 2013
@ryanwatson
Copy link

With some modification and finishing off of some code johnsimons sent me, here's a work around in the mean time:

public class ModifyRaven : IWantToRunAtStartup
{
    public void Run() {
        var iDocumentStoreType =
            Type.GetType("Raven.Client.IDocumentStore, NServiceBus.Core, Version=3.3.0.0, Culture=neutral, PublicKeyToken=9fc386479f8a226c");
        var webRequestEventArgsType =
            Type.GetType(
                "Raven.Client.Connection.WebRequestEventArgs, NServiceBus.Core, Version=3.3.0.0, Culture=neutral, PublicKeyToken=9fc386479f8a226c");
        dynamic documentStore = Configure.Instance.Builder.Build(iDocumentStoreType);

        var eventHandlerType = typeof(EventHandler<>);
        var jsonRequestFactory = iDocumentStoreType.GetProperty("JsonRequestFactory")
            .GetValue(documentStore, null);
        var configureRequestEventInfo = iDocumentStoreType.GetProperty("JsonRequestFactory")
            .PropertyType.GetEvent("ConfigureRequest");

        var callbackMethodInfo = typeof(ModifyRaven).GetMethod("JsonRequestFactoryOnConfigureRequest", BindingFlags.Public | BindingFlags.Instance);
        var delegateType = eventHandlerType.MakeGenericType(webRequestEventArgsType);
        var handlera = Delegate.CreateDelegate(delegateType, null, callbackMethodInfo);
        configureRequestEventInfo.AddEventHandler(jsonRequestFactory, handlera);
    }

    public void JsonRequestFactoryOnConfigureRequest(object sender, dynamic webRequestEventArgs) {
        var httpWebRequest = (HttpWebRequest)GetProperty(webRequestEventArgs, "Request");
        httpWebRequest.UnsafeAuthenticatedConnectionSharing = true;
        httpWebRequest.PreAuthenticate = true;
    }

    public static object GetProperty(object target, string name) {
        var site = System.Runtime.CompilerServices.CallSite<Func<System.Runtime.CompilerServices.CallSite, object, object>>.Create(Microsoft.CSharp.RuntimeBinder.Binder.GetMember(0, name, target.GetType(), new[] { Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo.Create(0, null) }));
        return site.Target(site, target);
    }

    public void Stop() {

    }
}

@johnsimons
Copy link
Member

Nice work @ryanwatson

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

No branches or pull requests

4 participants