resolver
<p>[TOC]</p>
<h3>简要描述</h3>
<ul>
<li>数据处理线程,作为服务来运行</li>
</ul>
<h3><code>ResolverServer</code> 数据结构</h3>
<pre><code class="language-golang">type (
ResolverServer struct {
resolverTag string // resolver tag
exporterTag string // 对应的 exporter tag,从 mq 消费消息
mqServerAddr string
mqClient *rpc.Client
hasShutdown bool
lazyShutdown bool
stopCh chan bool
resolverHandler resolver_common.ResolverHandler
}
)</code></pre>
<h3>方法接口列表</h3>
<ul>
<li><code>func (s *ResolverServer) Start()</code></li>
<li><code>func (s *ResolverServer) Shutdown(lazyShutdown bool)</code></li>
<li><code>func (s *ResolverServer) consume() (protocol.MqMessage, bool, bool)</code></li>
<li><code>func (s *ResolverServer) resolve(message protocol.MqMessage)</code></li>
</ul>
<h3><code>Start</code> 方法接口</h3>
<h5>方法参数介绍</h5>
<ul>
<li>无</li>
</ul>
<h5>返回值介绍</h5>
<ul>
<li>无</li>
</ul>
<h5>调用逻辑</h5>
<ul>
<li>Start 是一种循环逻辑,采用定时轮询以及贪心迭代两种运行方式。</li>
<li>定时轮询,指的是,resolver 会定时地消费 MQ 中的消息。</li>
<li>贪心迭代,指的是,利用 <code>FilterMessage</code> 接口的 <code>HasNext</code> 返回值,如果为 true 则立即进行下一次消费。</li>
</ul>
<h5>注意事项</h5>
<ul>
<li>其中值得注意的是,定时轮询中采用的是 <code>timer</code>,而不是 <code>ticker</code>,这是为了避免轮询和迭代可能发生并发进行的情况,使得从 mq 中消费的消息是二次乱序的。</li>
<li>由于采用 <code>timer</code>,实现了,定时轮询和贪心迭代两种方式,resolver 只会同时启用一种,也就是二者是交替的,因此避免了二次乱序。</li>
<li>因为本身 mq 中的消息可能由于前置的 http 和 rpc 调用发生乱序,而如果并发进行就可能发生二次乱序,对后续处理不友好。</li>
</ul>
<h3><code>Shutdown</code> 方法接口</h3>
<h5>方法参数介绍</h5>
<table>
<thead>
<tr>
<th style="text-align: left;">参数名</th>
<th style="text-align: left;">类型</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align: left;">lazyShutdown</td>
<td style="text-align: left;">bool</td>
<td>是否启用 lazy shutdown 模式</td>
</tr>
</tbody>
</table>
<h5>返回值介绍</h5>
<ul>
<li>无</li>
</ul>
<h5>调用逻辑</h5>
<ul>
<li>如果 lazy 为 false,那么直接清空目标 MQ,然后退出当前线程。</li>
<li>如果 lazy 为 true,那么设置一个全局的 lazy 标记,直到下一次 <code>filter message</code> 返回的 <code>has next</code> 为 false 时再退出线程。</li>
</ul>
<h5>注意事项</h5>
<ul>
<li>无</li>
</ul>
<h3><code>consume</code> 方法接口</h3>
<ul>
<li>consume 是具体的消费逻辑,从 mq 读取消息然后返回。</li>
<li>前面提到的 lazy shutdown 是在这里完成的。</li>
</ul>
<h3><code>resolve</code> 方法接口</h3>
<ul>
<li>resolve 将消息依次传递给 <code>resolver handler</code> 来处理。</li>
</ul>