为什么LDAP查找需要这么长时间?

时间:2018-01-25 19:33:48

标签: ios objective-c iphone dns ldap

我在iOS应用程序中使用LDAP进行活动目录查找,并且查找大约需要7-8秒。我有一个2600人的活动目录。我还检查了服务器的连接时间,并且它快速闪电。最后,在调试之后我意识到我的for循环通过DN列表循环需要很长时间。当我在for循环中注释代码时,速度要快得多。这是我的查找代码:

for(baseDN in searchDnList)
{
    // initiates search
    msgid = [self searchBaseDN:baseDN scope:searchScope filter:searchFilter
                    attributes:attrs attributesOnly:searchAttributesOnly];
    if (!(self.isSuccessful))
    {
        [self freeAttributeArray:(&attrs)];
        return(self.isSuccessful);
    };

    // verifies operation has not been cancelled
    if ((self.isCancelled))
    {
        @synchronized(session)
        {
            if ((session.ld))
            ldap_abandon_ext(session.ld, msgid, NULL, NULL);
        };
        self.errorCode = LDAP_USER_CANCELLED;
        [self freeAttributeArray:(&attrs)];
        return(self.isSuccessful);
    };
    ///////////////////////starts here////////////////////////////////////
    // This part is taking long! Without this part, it works fast
    // waits for result
    if ((res = [self resultWithMessageID:msgid resultEntries:nil]) == NULL)
    {
        [self freeAttributeArray:(&attrs)];
        return(self.isSuccessful);
    };
    //////////////////////////ends here//////////////////////////////////

    // parses result
    if (!([self parseResult:res referrals:nil]))
    {
        [self freeAttributeArray:(&attrs)];
        return(self.isSuccessful);
    };
}

这是我的resultWithMessageID方法的代码

- (LDAPMessage *) resultWithMessageID:(int)msgid
                    resultEntries:(NSMutableArray *)results
{
int               msgtype;
struct timeval    timeout;
LDAPMessage     * res;
int               err;
char            * dn;
char            * attribute;
BerElement      * ber;
BerValue       ** vals;
LKEntry         * entry;

// initializes ivars
res = NULL;
if ((results))
[results removeAllObjects];

// sets limits
timeout.tv_sec    = 0;
timeout.tv_usec   = 250000; // 0.25 seconds

// loops through results
msgtype = LDAP_RES_SEARCH_ENTRY;
while(msgtype == LDAP_RES_SEARCH_ENTRY) {
    // slight pause to prevent race condition
    //usleep(250000);

    // verifies operation has not been cancelled
    if ((self.isCancelled)) {
        @synchronized(session) {
            if ((session.ld)) {
                ldap_abandon_ext(session.ld, msgid, NULL, NULL);
            }
        };
        self.errorCode = LDAP_USER_CANCELLED;
        return(NULL);
    };

    // retrieves result
    @synchronized(session)
    {
        msgtype = ldap_result(session.ld, msgid, 0, &timeout, &res);
        switch(msgtype)
        {
            // encountered an error
            case -1:
                ldap_get_option(session.ld, LDAP_OPT_RESULT_CODE, &err);
                [self resetErrorWithTitle:@"LDAP Result" andCode:err];
                return(NULL);

            // timeout was exceeded
            case 0:
                break;

            // result was returned
            default:
                break;
        };
    };

    // determines result type
    msgtype = ldap_msgtype(res);
    if (msgtype != LDAP_RES_SEARCH_ENTRY) {
        continue;
    }

    // processes entry
    @synchronized(session) {
        // creates entry with DN
        dn = ldap_get_dn(session.ld, res);
        entry = [[[LKEntry alloc] initWithDn:dn] autorelease];
        ldap_memfree(dn);

        attribute = ldap_first_attribute(session.ld, res, &ber);
        while((attribute)) {
            vals = ldap_get_values_len(session.ld, res, attribute);
            [entry setBerValues:vals forAttribute:attribute];
            ldap_value_free_len(vals);
            attribute = ldap_next_attribute(session.ld, res, ber);
        };
        ber_free(ber, 0);

        // stores entry for later use
        if ((results)) {
            [results addObject:entry];
        }
        else {
            [self willChangeValueForKey:@"entries"];
            @synchronized(self) {
                if (!(entries))
                entries = [[NSMutableArray alloc] initWithCapacity:1];
                [entries addObject:entry];
            };
            [self didChangeValueForKey:@"entries"];
        };
    };

    // frees result
    ldap_memfree(res);
};

return(res);
}

我是LDAP的新手,一般是堆栈溢出的新手,所以如果我没有正确陈述我的问题,请原谅我。有人可以帮我这个吗?此外,我使用基本搜索范围进行查找。

0 个答案:

没有答案