Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

117 řádky
3.8 KiB

před 5 roky
před 5 roky
před 5 roky
před 5 roky
před 5 roky
před 5 roky
před 5 roky
před 5 roky
před 5 roky
před 5 roky
před 5 roky
před 5 roky
před 5 roky
před 5 roky
před 5 roky
před 5 roky
před 5 roky
před 5 roky
před 5 roky
před 5 roky
před 5 roky
před 5 roky
před 5 roky
před 5 roky
před 5 roky
před 5 roky
před 5 roky
před 5 roky
před 5 roky
před 5 roky
před 5 roky
před 5 roky
před 5 roky
před 5 roky
před 5 roky
před 5 roky
před 5 roky
před 5 roky
před 5 roky
před 5 roky
před 5 roky
před 5 roky
před 5 roky
  1. -module(agAgencyUtils).
  2. -include("agHttpCli.hrl").
  3. -compile(inline).
  4. -compile({inline_size, 128}).
  5. -export([
  6. getQueue/1
  7. , addQueue/2
  8. , delQueue/1
  9. , clearQueue/0
  10. , cancelTimer/1
  11. , dealClose/3
  12. , reconnectTimer/2
  13. , agencyReply/2
  14. , agencyReply/4
  15. , initReconnectState/3
  16. , resetReconnectState/1
  17. , updateReconnectState/1
  18. ]).
  19. -spec getQueue(pos_integer()) -> undefined | miRequest().
  20. getQueue(RequestsIn) ->
  21. erlang:get(RequestsIn).
  22. -spec addQueue(pos_integer(), miRequest()) -> undefined.
  23. addQueue(RequestsIn, MiRequest) ->
  24. erlang:put(RequestsIn, MiRequest).
  25. -spec delQueue(pos_integer()) -> miRequest().
  26. delQueue(RequestsIn) ->
  27. erlang:erase(RequestsIn).
  28. -spec clearQueue() -> term().
  29. clearQueue() ->
  30. erlang:erase().
  31. -spec dealClose(srvState(), cliState(), term()) -> {ok, srvState(), cliState()}.
  32. dealClose(SrvState, #cliState{curInfo = CurInfo} = ClientState, Reply) ->
  33. agencyReply(CurInfo, Reply),
  34. agencyReplyAll(Reply),
  35. reconnectTimer(SrvState, ClientState#cliState{requestsIn = 1, requestsOut = 0, backlogNum = 0}).
  36. -spec reconnectTimer(srvState(), cliState()) -> {ok, srvState(), cliState()}.
  37. reconnectTimer(#srvState{reconnectState = undefined} = SrvState, CliState) ->
  38. {ok, {SrvState#srvState{socket = undefined}, CliState}};
  39. reconnectTimer(#srvState{reconnectState = ReconnectState} = SrvState, CliState) ->
  40. #reconnectState{current = Current} = MewReconnectState = agAgencyUtils:updateReconnectState(ReconnectState),
  41. TimerRef = erlang:send_after(Current, self(), ?miDoNetConnect),
  42. {ok, SrvState#srvState{reconnectState = MewReconnectState, socket = undefined, timerRef = TimerRef}, CliState}.
  43. -spec agencyReply(term(), term()) -> ok.
  44. agencyReply({undefined, _RequestId, TimerRef}, _Reply) ->
  45. agAgencyUtils:cancelTimer(TimerRef);
  46. agencyReply({PidForm, RequestId, TimerRef}, Reply) ->
  47. agAgencyUtils:cancelTimer(TimerRef),
  48. catch PidForm ! #miAgHttpCliRet{requestId = RequestId, reply = Reply},
  49. ok;
  50. agencyReply(undefined, _RequestRet) ->
  51. ok.
  52. -spec agencyReply(undefined | pid(), requestId(), undefined | reference(), term()) -> ok.
  53. agencyReply(undefined, _RequestId, TimerRef, _Reply) ->
  54. agAgencyUtils:cancelTimer(TimerRef),
  55. ok;
  56. agencyReply(FormPid, RequestId, TimerRef, Reply) ->
  57. agAgencyUtils:cancelTimer(TimerRef),
  58. catch FormPid ! #miAgHttpCliRet{requestId = RequestId, reply = Reply},
  59. ok.
  60. -spec agencyReplyAll(term()) -> ok.
  61. agencyReplyAll(Reply) ->
  62. AllList = agAgencyUtils:clearQueue(),
  63. [agencyReply(FormPid, RequestId, undefined, Reply) || #miRequest{requestId = RequestId, fromPid = FormPid} <- AllList],
  64. ok.
  65. -spec cancelTimer(undefined | reference()) -> ok.
  66. cancelTimer(undefined) -> ok;
  67. cancelTimer(TimerRef) ->
  68. case erlang:cancel_timer(TimerRef) of
  69. false ->
  70. %% 找不到计时器,我们还没有看到超时消息
  71. receive
  72. {timeout, TimerRef, _Msg} ->
  73. %% 丢弃该超时消息
  74. ok
  75. after 0 ->
  76. ok
  77. end;
  78. _ ->
  79. %% Timer 已经运行了
  80. ok
  81. end.
  82. -spec initReconnectState(boolean(), pos_integer(), pos_integer()) -> reconnectState() | undefined.
  83. initReconnectState(IsReconnect, Min, Max) ->
  84. case IsReconnect of
  85. true ->
  86. #reconnectState{min = Min, max = Max, current = Min};
  87. false ->
  88. undefined
  89. end.
  90. -spec resetReconnectState(undefined | reconnectState()) -> reconnectState() | undefined.
  91. resetReconnectState(#reconnectState{min = Min} = ReconnectState) ->
  92. ReconnectState#reconnectState{current = Min}.
  93. -spec updateReconnectState(reconnectState()) -> reconnectState().
  94. updateReconnectState(#reconnectState{current = Current, max = Max} = ReconnectState) ->
  95. NewCurrent = Current + Current,
  96. ReconnectState#reconnectState{current = minCur(NewCurrent, Max)}.
  97. minCur(A, B) when B >= A ->
  98. A;
  99. minCur(_, B) ->
  100. B.