arangodb erlang数据库驱动
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

95 wiersze
3.5 KiB

  1. -module(agAgencyUtils).
  2. -include("agVstCli.hrl").
  3. -compile(inline).
  4. -compile({inline_size, 128}).
  5. -export([
  6. cancelTimer/1
  7. , dealClose/3
  8. , reconnectTimer/2
  9. , agencyReply/2
  10. , agencyReply/4
  11. , initReconnectState/3
  12. , resetReconnectState/1
  13. , updateReconnectState/1
  14. ]).
  15. -spec dealClose(srvState(), cliState(), term()) -> {ok, srvState(), cliState()}.
  16. dealClose(SrvState, #cliState{requestsIns = RequestsIns, requestsOuts = RequestsOuts, curInfo = CurInfo} = ClientState, Reply) ->
  17. agencyReply(CurInfo, Reply),
  18. agencyReplyAll(RequestsOuts, RequestsIns, Reply),
  19. reconnectTimer(SrvState, ClientState#cliState{requestsIns = [], requestsOuts = [], backlogNum = 0, revStatus = leisure, curInfo = undefined, recvState = undefined}).
  20. -spec reconnectTimer(srvState(), cliState()) -> {ok, srvState(), cliState()}.
  21. reconnectTimer(#srvState{reconnectState = undefined} = SrvState, CliState) ->
  22. {ok, {SrvState#srvState{socket = undefined}, CliState}};
  23. reconnectTimer(#srvState{reconnectState = ReconnectState} = SrvState, CliState) ->
  24. #reConnState{current = Current} = MewReconnectState = agAgencyUtils:updateReconnectState(ReconnectState),
  25. TimerRef = erlang:send_after(Current, self(), ?AgMDoNetConn),
  26. {ok, SrvState#srvState{reconnectState = MewReconnectState, socket = undefined, timerRef = TimerRef}, CliState}.
  27. -spec agencyReply(term(), term()) -> ok.
  28. agencyReply({undefined, _RequestId, TimerRef}, _Reply) ->
  29. agAgencyUtils:cancelTimer(TimerRef);
  30. agencyReply({PidForm, RequestId, TimerRef}, Reply) ->
  31. agAgencyUtils:cancelTimer(TimerRef),
  32. catch PidForm ! #agReqRet{messageId = RequestId, reply = Reply},
  33. ok;
  34. agencyReply(undefined, _RequestRet) ->
  35. ok.
  36. -spec agencyReply(undefined | pid(), messageId(), undefined | reference(), term()) -> ok.
  37. agencyReply(undefined, MessageId, TimerRef, _Reply) ->
  38. agAgencyUtils:cancelTimer(TimerRef);
  39. agencyReply(FormPid, RequestId, TimerRef, Reply) ->
  40. agAgencyUtils:cancelTimer(TimerRef),
  41. catch FormPid ! #agReqRet{messageId = RequestId, reply = Reply},
  42. ok.
  43. -spec agencyReplyAll(list(), list(), term()) -> ok.
  44. agencyReplyAll(RequestsOuts, RequestsIns, Reply) ->
  45. [agencyReply(FormPid, RequestId, undefined, Reply) || #agReq{messageId = RequestId, fromPid = FormPid} <- RequestsOuts],
  46. [agencyReply(FormPid, RequestId, undefined, Reply) || #agReq{messageId = RequestId, fromPid = FormPid} <- lists:reverse(RequestsIns)],
  47. ok.
  48. -spec cancelTimer(undefined | reference()) -> ok.
  49. cancelTimer(undefined) -> ok;
  50. cancelTimer(TimerRef) ->
  51. case erlang:cancel_timer(TimerRef) of
  52. false ->
  53. receive
  54. {timeout, TimerRef, _Msg} ->
  55. %% discard the timeout msg
  56. ok
  57. after 0 ->
  58. ok
  59. end;
  60. _ ->
  61. %% Timer already run
  62. ok
  63. end.
  64. -spec initReconnectState(boolean(), pos_integer(), pos_integer()) -> reconnectState() | undefined.
  65. initReconnectState(IsReconnect, Min, Max) ->
  66. case IsReconnect of
  67. true ->
  68. #reConnState{min = Min, max = Max, current = Min};
  69. false ->
  70. undefined
  71. end.
  72. -spec resetReconnectState(undefined | reconnectState()) -> reconnectState() | undefined.
  73. resetReconnectState(#reConnState{min = Min} = ReconnectState) ->
  74. ReconnectState#reConnState{current = Min}.
  75. -spec updateReconnectState(reconnectState()) -> reconnectState().
  76. updateReconnectState(#reConnState{current = Current, max = Max} = ReconnectState) ->
  77. NewCurrent = Current + Current,
  78. ReconnectState#reConnState{current = minCur(NewCurrent, Max)}.
  79. minCur(A, B) when B >= A ->
  80. A;
  81. minCur(_, B) ->
  82. B.