| 183 | } |
| 184 | |
| 185 | public <TArgument extends Serializable, TResult extends Serializable> |
| 186 | TaskCompletionSourceX<RaftRpc<TArgument, TResult>> sendForWait(RaftRpc<TArgument, TResult> rpc) { |
| 187 | if (pendingLimit > 0 && pending.size() > pendingLimit) // UrgentPending不限制。 |
| 188 | throw new IllegalStateException("too many pending"); |
| 189 | // 由于interface不能把setter弄成保护的,实际上外面可以修改。 |
| 190 | // 简单检查一下吧。 |
| 191 | if (rpc.getUnique().getRequestId() != 0) |
| 192 | throw new IllegalStateException("RaftRpc.UniqueRequestId != 0. Need A Fresh RaftRpc"); |
| 193 | |
| 194 | rpc.getUnique().setRequestId(uniqueRequestIdGenerator.next()); |
| 195 | // 外面在发送前可以设置clientId |
| 196 | if (rpc.getUnique().getClientId().isEmpty()) |
| 197 | rpc.getUnique().setClientId(uniqueRequestIdGenerator.getName()); |
| 198 | rpc.setCreateTime(System.currentTimeMillis()); |
| 199 | rpc.setSendTime(rpc.getCreateTime()); |
| 200 | if (rpc.getTimeout() == 0) // set default timeout |
| 201 | rpc.setTimeout(raftConfig.getAgentTimeout()); |
| 202 | |
| 203 | var future = new TaskCompletionSourceX<RaftRpc<TArgument, TResult>>(); |
| 204 | rpc.future = future; |
| 205 | if (pending.putIfAbsent(rpc.getUnique().getRequestId(), rpc) != null) |
| 206 | throw new IllegalStateException("duplicate requestId rpc=" + rpc); |
| 207 | |
| 208 | rpc.setResponseHandle(p -> sendForWaitHandle(p, rpc)); |
| 209 | ConnectorProxy leader = this.leader; |
| 210 | if (!ProxyAgent.send(client, proxyAgent, rpc, leader, |
| 211 | leader != null ? leader.getConnector().TryGetReadySocket() : null)) |
| 212 | logger.debug("sendForWait failed: leader={}, rpc={}", leader, rpc); |
| 213 | return future; |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Connector 代理, |