Spring-Cloud-Ribbon-IRule-RetryRule

package com.netflix.loadbalancer;

import com.netflix.client.config.IClientConfig;

/**
 * Given that
 * {@link IRule} can be cascaded, this {@link RetryRule} class allows adding a retry logic to an existing Rule.
 * 
 * @author stonse
 * 
 */
public class RetryRule extends AbstractLoadBalancerRule {
   IRule subRule = new RoundRobinRule();
   long maxRetryMillis = 500;

   public RetryRule() {
   }

   public RetryRule(IRule subRule) {
      this.subRule = (subRule != null) ? subRule : new RoundRobinRule();
   }

   public RetryRule(IRule subRule, long maxRetryMillis) {
      this.subRule = (subRule != null) ? subRule : new RoundRobinRule();
      this.maxRetryMillis = (maxRetryMillis > 0) ? maxRetryMillis : 500;
   }

   public void setRule(IRule subRule) {
      this.subRule = (subRule != null) ? subRule : new RoundRobinRule();
   }

   public IRule getRule() {
      return subRule;
   }

   public void setMaxRetryMillis(long maxRetryMillis) {
      if (maxRetryMillis > 0) {
         this.maxRetryMillis = maxRetryMillis;
      } else {
         this.maxRetryMillis = 500;
      }
   }

   public long getMaxRetryMillis() {
      return maxRetryMillis;
   }

   
   
   @Override
   public void setLoadBalancer(ILoadBalancer lb) {       
      super.setLoadBalancer(lb);
      subRule.setLoadBalancer(lb);
   }

   /*
    * Loop if necessary. Note that the time CAN be exceeded depending on the
    * subRule, because we're not spawning additional threads and returning
    * early.
    */
   public Server choose(ILoadBalancer lb, Object key) {
      long requestTime = System.currentTimeMillis();
      long deadline = requestTime + maxRetryMillis;

      Server answer = null;

      answer = subRule.choose(key);

      if (((answer == null) || (!answer.isAlive()))
            && (System.currentTimeMillis() < deadline)) {

         // 这里定义了一个InterruptTask,继承TimerTask;
         // 并且实现了抽象的run方法,run方法的实现很简单,就是判断,如果InterruptTask里面构建的当前线程不为null并且还活着,就中断该线程;
         // 这个InterruptTask里面实现的run方法什么时候执行呢,就是在当前时间+maxRetryMillis时执行,中断当前线程,这个run方法是在Timer里的mainloop里task.run()调用的
         InterruptTask task = new InterruptTask(deadline
               - System.currentTimeMillis());

         // 这里判断Thread.interrupted(),就是和上面InterruptTask的run方法呼应上了,在当前时间+maxRetryMillis之前,这个while为true,从而在当前时间到当前时间+maxRetryMillis这个时间段内,反复重试获取server
         while (!Thread.interrupted()) {
            answer = subRule.choose(key);

            if (((answer == null) || (!answer.isAlive()))
                  && (System.currentTimeMillis() < deadline)) {
               /* pause and retry hoping it's transient */
               Thread.yield();
            } else {
               break;
            }
         }
     // 最终要把这个InterruptTask取消掉,这样就能把Timer.TimerQueue中对于该task的引用移除
         task.cancel();
      }

      if ((answer == null) || (!answer.isAlive())) {
         return null;
      } else {
         return answer;
      }
   }

   @Override
   public Server choose(Object key) {
      return choose(getLoadBalancer(), key);
   }

   @Override
   public void initWithNiwsConfig(IClientConfig clientConfig) {
   }
}

转载于:https://www.cnblogs.com/rocker-pg/p/11496456.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值