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-2011 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 "lber_pvt.h"
24#include "lutil.h"
25
26
27/* Congestion avoidance code
28 * for Deadlock Rollback
29 */
30
31void
32bdb_trans_backoff( int num_retries )
33{
34	int i;
35	int delay = 0;
36	int pow_retries = 1;
37	unsigned long key = 0;
38	unsigned long max_key = -1;
39	struct timeval timeout;
40
41	lutil_entropy( (unsigned char *) &key, sizeof( unsigned long ));
42
43	for ( i = 0; i < num_retries; i++ ) {
44		if ( i >= 5 ) break;
45		pow_retries *= 4;
46	}
47
48	delay = 16384 * (key * (double) pow_retries / (double) max_key);
49	delay = delay ? delay : 1;
50
51	Debug( LDAP_DEBUG_TRACE,  "delay = %d, num_retries = %d\n", delay, num_retries, 0 );
52
53	timeout.tv_sec = delay / 1000000;
54	timeout.tv_usec = delay % 1000000;
55	select( 0, NULL, NULL, NULL, &timeout );
56}
57