]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/trans.c
ITS#3556: 64-bit portability
[openldap] / servers / slapd / back-bdb / trans.c
1 /* trans.c - bdb backend transaction routines */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2000-2005 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16
17 #include "portable.h"
18
19 #include <stdio.h>
20 #include <ac/string.h>
21
22 #include "back-bdb.h"
23 #include "external.h"
24 #include "lber_pvt.h"
25 #include "lutil.h"
26
27
28 /* Congestion avoidance code
29  * for Deadlock Rollback
30  */
31
32 void
33 bdb_trans_backoff( int num_retries )
34 {
35         int i;
36         int delay = 0;
37         int pow_retries = 1;
38         unsigned long key = 0;
39         unsigned long max_key = -1;
40         struct timeval timeout;
41
42         lutil_entropy( (unsigned char *) &key, sizeof( unsigned long ));
43
44         for ( i = 0; i < num_retries; i++ ) {
45                 if ( i >= 5 ) break;
46                 pow_retries *= 4;
47         }
48
49         delay = 16384 * (key * (double) pow_retries / (double) max_key);
50         delay = delay ? delay : 1;
51
52 #ifdef NEW_LOGGING
53         LDAP_LOG( OPERATION, ERR, "delay = %d, num_retries = %d\n", delay, num_retries, 0 );
54 #else
55         Debug( LDAP_DEBUG_TRACE,  "delay = %d, num_retries = %d\n", delay, num_retries, 0 );
56 #endif
57
58         timeout.tv_sec = delay / 1000000;
59         timeout.tv_usec = delay % 1000000;
60         select( 0, NULL, NULL, NULL, &timeout );
61 }