一:介绍

指数退避算法的定义和使用可以在网上搜搜。提供一下wiki的介绍部分定义:an algorithm that uses feedback to multiplicatively decrease the rate of some process, in order to gradually find an acceptable rate.

这边我介绍的是,指数退避的算法其实不只可以在网络包的重传机制中实现网络包的合理的重传机制。而且可以封装好相应的接口,从而利用这样的接口产生任何一种你需要的时间,或者你需要的重传的计数等。这些的应用只是利用了指数退避算法的本质,即根据要求产生需要的范围内的一个随机数,供需要的Action调用

二:C代码的实现封装的接口

网上有很多只是介绍了指数退避算法的定义,和一份blog的伪代码和Javacode的实现。然后就是更种Copy了。这里上了部分笔记和C封装的代码,这样的接口调用,根据接口里的注释,可以做一定的修改,然后可以产生一个你需要的指数的随机数,供你程序的动态的使用。

/*the pesudocode Do some asynchronous operation:retries = 0Do wait for (2^retries * 100) millisecondsstatus = Get the result of the asynchronous operationIf satatus = Success retry = FalseElse If status = Not_Ready retry = trueElse if satus = Throttled retry = true Elsesom other error occurrred, so stop calling the API retry = false End ifretries = retries + 1While(retry AND (reties < MAX_RETRIES))
*/#include <math.h>/* Define the status of the retry
** The flag of the result you can define as you need
**
***/
typedef ResStaus
{eRES_None,eRES_Success,eRES_OtherError
}st_ResStatus;/* The MinTime Your action need to be active */
#define MAX_ACTIVE_TIME 100
#define MAX_ACTIVE_RETRIES 16/* The times of exponential backoff */
double getWaitTimeExp(int RetryCount, long BaseTimes)
{double TempWait = 0;TempWait = (pow(2, RetryCount) * BaseTimes);return TempWait;
}/* The operations */
int OperationWaitForResult()
{long Token = 0;int Retries = 0;bool RetryFlag = false;st_ResStatus Result = eRES_None;/* The BaseTime is the one times your action need to active */double BaseTimes = 0;double WaitTime = 0;do{/* Get the waittime times first */WaitTime = (MIN_ACTIVE_TIME > getWaitTimeExp(RetryCount, BaseTimes) ? getWaitTimeExp(RetryCount, BaseTimes) : MAX_ACTIVE_TIME);printf("WaitTime :%ld\n", WaitTime);/* The thread hangup waittime */sleep(WaitTime);/* Your action functions return the status of the executed the functions */   Result = YourActionFunc();switch(Result){/* Do your actions process */case eRES_Success:RetryFlag = false;break;/* Do your cation the error process */case eRES_OtherError:RetryFlag = true;break;default:/* The stop flag of your action need to process */break;}}while(RetryFlag && ((Retries++) < (MAX_ACTIVE_RETRIES))/* Do your later process */
}

其实这里面的精华部分,就是取得指数规避的动态的指数参数,并且这个返回的Times, 你可以用在定时器,或者你需要重复调用的Action的限制的时间,次数等。

三:实例的介绍

这里具体的代码就不上了,不过提供一个思路。比如你想定时的去做一action,但是随着不同的返回值(程序接口运行的状态)你想动态的合理的规划定时器的调用时间,你就可以按照上面的指数退避的接口,产生一个合理的指数Times,然后设定一个最大,最小值,然后将此接口获得值提供给定时器的超时时间。这样可以合理的利用定时器的资源,从而使你的代码达到一种合理的利用资源的效果。其他的比如你重新注册等。这里的时间或许你可以在各个方面去实现,这样你不只在使用指数,其他的一些实用都可以应用。

四:Given a method to gerenated a uniforms times

这里直接上了参照wiki的方法,代码和对应的算法公式。只需将上面的codes funcation 进行一些改变就可以了:

/*Give the uniform distribution of backoff times The expected backoff time is the mean of the possibilities.That is ,after c collisions,the number of backoff slots is in [0, 1, ..., N], where N = 2^c -1 and the expected backoff time (in lsots) is (1)/(N + 1)(Sigma(1 -- N))for example:when c = 3;N = 2^C - 1;N = 7;and the calculate the mean of the back off time possibilities E(c) = (1)/(N + 1)(Sigma(1 -- N))= N / 2 = (2^C - 1) / 2;which is, for the example, E(3) = 3.5 slots
*/
/* The times of exponential backoff */
double getWaitTimeExp(int RetryCount, long BaseTimes)
{double TempWait = 0;TempWait = (pow(2, RetryCount) * BaseTimes);/* Give the uniform distribution of backoff times */TempWait = (TempWait - 1) / 2;return TempWait;
}

这样每次产生的time,就是一个唯一的Uniform time .

以上的仅供参考,欢迎指教

Exponetial BackOff(指数退避算法)相关推荐

  1. python udp socket解决服务端响应时间长的指数退避算法

    UDP连接是一个不可靠的连接,也就是说,UDP通信过程中可能出现数据包丢失的情况,或者是服务端宕机后,客户端不知道服务端状态,仍然不停的访问服务端的情况.针对这一情况,UDP客户端必须选择一个等待时间 ...

  2. 截断二进制指数退避算法c++实现

    算法概述: 二进制指数类型退避算法 (truncated binary exponential type)(CSMA/CA检测到冲突,中止后随机重发使用的算法) 发生碰撞的站在停止发送数据后,要推迟( ...

  3. 通过Akka学习指数退避(Exponential Backoff)

    原文连接:https://mincong.io/cn/exponential-backoff-in-akka/ 前言 在软件开发中,我们免不了要跟各种错误异常打交道.比如说,当一个服务在调用另一个服务 ...

  4. flume之退避算法backoff algorithm

    flume之退避算法backoff algorithm 什么是退避算法: In a single channel contention based medium access control (MAC ...

  5. [Android Traffic] 调整定时更新的频率(C2DM与退避算法)

    转载自: http://blog.csdn.net/kesenhoo/article/details/7395253 Minimizing the Effect of Regular Updates[ ...

  6. flume backoff 退避算法

    原文链接: https://qiuqiang1985.iteye.com/blog/1513049 发生冲突时,每个节点等待一定的时间后重新发送,二进制退避算法中,等待时间以以2位底的指数级增长,失败 ...

  7. 【计算机网络】数据链路层 : CSMA/CD 协议 ( 载波监听多点接入 / 碰撞检测 协议 | 单程端到端传播时延 | 截断二进制指数规避算法 | 计算示例 | 最小帧长问题 )★

    文章目录 一. CSMA/CD 协议 二. 传播时延对于 载波监听 的影响 三. 单程端到端传播时延 相关概念 四. 碰撞后重传时机 ( 截断二进制指数规避算法 ) 五.截断二进制指数规避算法 计算示 ...

  8. 时间序列挖掘-预测算法-三次指数平滑法(Holt-Winters)——三次指数平滑算法可以很好的保存时间序列数据的趋势和季节性信息...

    from:http://www.cnblogs.com/kemaswill/archive/2013/04/01/2993583.html 在时间序列中,我们需要基于该时间序列当前已有的数据来预测其在 ...

  9. 【学习笔记】数据链路层——随机访问介质访问控制(ALOHA、CSMA、CSMA/CD、CSMA/CA),截断二进制指数规避算法

    文章目录 小前言 一. ALOHA协议 纯ALOHA协议 时隙ALOHA协议 ALOHA对比 CSMA协议 定义与分类 ① 1-坚持CSMA ② 非坚持CSMA ③ p-坚持CSMA 总结 CSMA/ ...

最新文章

  1. 基于Erlang语言的视频相似推荐系统 | 深度
  2. 关于Verilog HDL的一些技巧、易错、易忘点(不定期更新)
  3. margin折叠问题
  4. Android属性之build.prop生成过程分析
  5. 计算机科技手抄报内容,科技手抄报内容云计算文字稿
  6. C中计算程序运行时间差(毫秒级)
  7. MAUI中构建跨平台原生控件实现
  8. CreateFile
  9. ddns 被解析为127.0.0.1_我为北京冬奥加油,2020.2.1-2.10冬奥知识分享
  10. LeetCode 4. Median of Two Sorted Arrays
  11. 程序员的算法课(13)-分治法
  12. java最大最小距离算法缺点_java算法(蓝桥杯)- 算法提高 题目1 最大最小值
  13. 【BZOJ】1002: [FJOI2007]轮状病毒 递推+高精度
  14. Vue:vue借助全局过滤器、moment、实现实时更新时间
  15. select2使用帮助
  16. 人工智能时代都需要哪些数学知识?这些经典教材给你划重点
  17. 软件分享 AirPlayer
  18. 数据连接池的工作原理
  19. CF 346 B vectorpair s[100]
  20. 机器学习 | 特征重要性判断

热门文章

  1. Matlab实现蒙特卡罗方法(随机模拟法)
  2. Hacking JWT(JSON Web Token)
  3. 年纪一大把,胡子一大堆,还能学好编程吗?今天我问了我自己
  4. String Shifting(今日头条2017秋招真题)
  5. C语言中调用数组元素的三种方法:下标法、数组名法、指针法
  6. 有华为的HCIP证书会更好找工作吗?
  7. 路由协议 RIP、IGRP、OSPF和EIGRP 的对比
  8. 西门子1500和300哪个贵_西门子s71500与s7300的区别
  9. 天刀手游pc端显示服务器维护,天刀手游电脑版无法更新网络修复工具
  10. 计算机网络知识点之五