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

Comments

Projects
None yet
4 participants
@andreasohlund
Member

andreasohlund commented Mar 27, 2012

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

This comment has been minimized.

Show comment
Hide comment
@johnsimons

johnsimons Dec 28, 2012

Contributor

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

Contributor

johnsimons commented Dec 28, 2012

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

@ryanwatson

This comment has been minimized.

Show comment
Hide comment
@ryanwatson

ryanwatson Jan 9, 2013

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?

ryanwatson commented Jan 9, 2013

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

This comment has been minimized.

Show comment
Hide comment
@johnsimons

johnsimons Jan 10, 2013

Contributor

A different way to configure this is;

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

johnsimons commented Jan 10, 2013

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

This comment has been minimized.

Show comment
Hide comment
@johnsimons

johnsimons Jan 11, 2013

Contributor

Added way to enable it in 6383897

Contributor

johnsimons commented Jan 11, 2013

Added way to enable it in 6383897

@johnsimons johnsimons closed this Jan 11, 2013

PeterLehmann pushed a commit to PeterLehmann/NServiceBus that referenced this issue Jan 11, 2013

@ryanwatson

This comment has been minimized.

Show comment
Hide comment
@ryanwatson

ryanwatson Jan 30, 2013

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() {

    }
}

ryanwatson commented Jan 30, 2013

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

This comment has been minimized.

Show comment
Hide comment
@johnsimons

johnsimons Jan 30, 2013

Contributor

Nice work @ryanwatson

Contributor

johnsimons commented Jan 30, 2013

Nice work @ryanwatson

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment