4

I have an object that wraps some Active Directory functions which are used quite frequently in my codebase. I have a convenience function to create it, but each time it is creating an SSL connection which is slow and inefficient. The way I can improve this in some places is to pass it to functions in a loop but this is not always convenient.

The class is state-free so it's thread-safe and could be shared within each Django instance. It should maintain its AD connection for at least a few minutes, and ideally not longer than an hour. There are also other non-AD objects I would like to do the same with.

I have used the various cache types, including in-memory, is it appropriate to use these for functional objects? I thought they were only meant for (serializable) data.

Alternatively: is there a Django suitable pattern for service locators or connection pooling like you often seen in Java apps?

Thanks, Joel

jbyrnes
  • 363
  • 4
  • 13

1 Answers1

3

I have found a solution that appears to work well, which is simply a Python feature that is similar to a static variable in Java.

def get_ad_service():
    if "ad_service" not in get_ad_service.__dict__:
        logger.debug("Creating AD service")
        get_ad_service.ad_service = CompanyADService(settings.LDAP_SERVER_URL,
                            settings.LDAP_USER_DN,
                            settings.LDAP_PASSWORD)

        logger.debug("Created AD service")
    else:
        logger.debug("Returning existing AD service")

    return get_ad_service.ad_service

My code already calls this function to get an instance of the AD service so I don't have to do anything further to make it persistent.

I found this and similar solutions here: What is the Python equivalent of static variables inside a function?

Happy to hear alternatives :)

Community
  • 1
  • 1
jbyrnes
  • 363
  • 4
  • 13
  • 1
    Does the object stored this way live thru the entire life of the django instance? Is that like a global top level function or a method of an object you are not showing? – drakorg Apr 06 '18 at 16:56
  • If I recall and understand correctly it's essentially a static member of the function itself, which means it should last as long as the thread is running and the module is in memory. As my app is driven by Apache HTTPD via WSGI, it is therefore probably stored as long as that handler is live, though I'm not sure if they are reused. To be honest I don't think this solution is very good due to these complexities and the fact that the lifetime could be very variable, so it may be essentially pointless. – jbyrnes Sep 19 '18 at 04:30