dsbenghe / Novell.Directory.Ldap.NETStandard

.NET LDAP client library for .NET Standard >= 2.0, .NET Core >=1.0, NET5/NET6/NET7/NET8 - works with any LDAP protocol compatible directory server (including Microsoft Active Directory).
MIT License
555 stars 151 forks source link

LdapConnection reuse #208

Closed safari137 closed 1 year ago

safari137 commented 2 years ago

I have an app that makes many calls to LDAP. I'd like to re-use the connections as much as possible.

I ran across this: https://github.com/dsbenghe/Novell.Directory.Ldap.NETStandard/issues/86

In my case, creating a connection adds ~150ms to each operation. Not the end of the world, but unnecessary. The above issue suggests creating a connection pool. Is there any guidance on this? Or has it already been done?

JoaoVictorVP commented 2 years ago

I have an app that makes many calls to LDAP. I'd like to re-use the connections as much as possible.

I ran across this: #86

In my case, creating a connection adds ~150ms to each operation. Not the end of the world, but unnecessary. The above issue suggests creating a connection pool. Is there any guidance on this? Or has it already been done?

Just make a class to handle that, use factory pattern with queues if it is not thread safe, if it is, just reuse the object, like that:

public interface ILdapConnectionFactory
{
   LdapConnection GetLdapConnection();
}
public class LdapConnectionFactory : ILdapConnectionFactory
{
   static LdapConnection _ldapConnection;
   public LdapConnection GetLdapConnection()
   {
      if(_ldapConnection is null)
         _ldapConnection = CreateLdapConnection();
      return _ldapConnection;
   }
   static LdapConnection CreateLdapConnection()
   {
       // Pass your setup here
   }
}