View Source WeChat.Refresher.Default (wechat v0.15.1)

AccessToken 刷新器 - 用于定时刷新 AccessToken

官方说明

本模块为默认的刷新器

需要修改为自定义的刷新器,可以这样配置:

config :wechat, :refresher, YourRefresher

修改刷新器的配置,支持多种配置方式:

方式1

config :wechat, :refresh_settings, [ClientA, ClientB, ClientC]

以上配置会自动为三个 Client 定时刷新 AccessToken , 默认会在 AccessToken 过期前 30 分钟刷新, AccessToken 刷新失败的重试间隔为 1 分钟, 可以通过接口 获取默认的 AccessToken 刷新列表:WeChat.Refresher.DefaultSettings.get_refresh_options_by_client/1

方式2

config :wechat, :refresh_settings, [{ClientA, client_setting}, ClientB, ClientC]
# or
config :wechat, :refresh_settings, %{ClientA => client_setting, ClientB => client_setting, ClientC => client_setting}

client_setting 配置说明见:client_setting/0

为了适应 StorageRefresher 启动之后才启动,可以开启延时启动刷新:

config :wechat, WeChat.Refresher.Default, wait_for_signal: true

当所有的 Storage 都已经完成,可以即可通过 WeChat.Refresher.Default.start_monitor/0 方法刷新 AccessToken

不配置默认为立即启动刷新

Summary

Types

AccessToken 超时前多少秒刷新,单位:秒

刷新 AccessToken 失败的重试间隔,单位:秒

Types

@type client_setting() :: %{
  optional(:refresh_before_expired) => refresh_before_expired(),
  optional(:refresh_retry_interval) => refresh_retry_interval(),
  optional(:refresh_options) =>
    WeChat.Refresher.DefaultSettings.refresh_options()
}

option

  • :refresh_before_expired: 在 AccessToken 超时前多少秒刷新,单位:秒,可选, 为保证 hub & hub_client 刷新正常,请保持两者的时间一致; server_role=hub_client 时, 默认值:1800 + 30 秒; 其余角色默认值:1800
  • :refresh_retry_interval: 刷新 AccessToken 失败的重试间隔,单位:秒,可选,默认值:60000
  • :refresh_options: 刷新 AccessToken 配置,可选,默认值:WeChat.Refresher.DefaultSettings.get_refresh_options_by_client/1 的输出结果
@type client_settings() ::
  [WeChat.client()] | %{required(WeChat.client()) => client_setting()}
Link to this type

refresh_before_expired()

View Source
@type refresh_before_expired() :: non_neg_integer()

AccessToken 超时前多少秒刷新,单位:秒

如果server_role = hub, hub server 的值请大于 hub client

Link to this type

refresh_retry_interval()

View Source
@type refresh_retry_interval() :: non_neg_integer()

刷新 AccessToken 失败的重试间隔,单位:秒

@type state() :: %{
  :wait_for_signal => boolean(),
  :clients => [WeChat.client()],
  required(WeChat.client()) => client_setting()
}

Functions

Link to this function

add(client, opts \\ %{})

View Source
@spec add(WeChat.client(), client_setting()) :: :ok
Link to this function

append_work_agent(client, agent)

View Source
@spec append_work_agent(WeChat.client(), WeChat.Work.Agent.t()) ::
  :ok | :client_not_in

Returns a specification to start this module under a supervisor.

See Supervisor.

@spec client_options(WeChat.client()) :: client_setting() | nil
@spec clients() :: [WeChat.client()]
@spec components() :: %{
  required(WeChat.component_appid()) => %{
    keys: [WeChat.Storage.Adapter.store_key()],
    clients: [WeChat.client()]
  }
}
@spec refresh(WeChat.client()) :: :ok | :not_found
Link to this function

refresh(client, store_id, store_key)

View Source
Link to this function

refresh_component(component_appid, store_key)

View Source
@spec refresh_component(WeChat.component_appid(), WeChat.Storage.Adapter.store_key()) ::
  :ok | :not_found
Link to this function

refresh_key(client, store_id, store_key, value, expires)

View Source
@spec remove(WeChat.client()) :: :ok | :not_found
Link to this function

start_link(client_settings \\ %{})

View Source
@spec start_link(client_settings()) :: GenServer.on_start()
@spec start_monitor() :: :ok