没有不值得去解决的问题,也没有不值得去学习的技术!

Integrating With WeChat Open Platform: Fixing a One-Hour Token Downtime Within a 24-Hour Cycle

View the specific time points of each HTTP request to fetch the token

1. In the current implementation for integrating with the WeChat third-party platform, the interface for receiving the verification ticket (receiving the push from the WeChat server) checks whether the token (component_access_token) exists in Redis. If it does not exist, it fetches the token (HTTP request). This effectively treats the WeChat server’s push, which occurs at 10-minute intervals, as a scheduled command. The specific details based on chronological order are as follows:

Plaintext
11:25 Get verification ticket (component_verify_ticket)

11:25 Get and generate token (component_access_token), store it in Redis, set expiration time: 13:20

13:15 Get verification ticket, will not be stored in Redis

13:20 Token expires

13:25 Get verification ticket, store it in Redis

Conclusion: Between 13:20 and 13:25, within these 5 minutes, the WeChat token does not exist, making WeChat publishing unavailable.

2. The readjusted rule minimizes the number of HTTP requests for fetching the token while ensuring the token remains absolutely and permanently valid. The specific details based on chronological order are as follows:

Plaintext
11:25 Get verification ticket (component_verify_ticket)

11:25 Get and generate token (component_access_token), add field: valid until (13:25), store it in Redis

13:05 Evaluation rule: valid until (13:25) - 20 minutes < 13:05. Result is false, will not fetch token, will not store in Redis

13:15 Evaluation rule: valid until (13:25) - 20 minutes < 13:15. Result is true, fetch token, add field: valid until (15:15), store it in Redis. Note: The token data has changed at this point; executing the HTTP request to fetch the token will generate a new token

13:25 Evaluation rule: valid until (15:15) - 20 minutes < 13:25. Result is false, will not fetch token, will not store in Redis

Conclusion:
1. The official recommendation is to re-fetch the token after 1 hour and 50 minutes. The channel publishing starts checking whether to re-fetch the token after 1 hour and 40 minutes. That is, within a 2-hour window, to ensure the token is re-fetched after 1 hour and 50 minutes.
2. The official push for the verification ticket (component_verify_ticket) has an average interval of 10 minutes, but in reality, there is an error of about 30 seconds. Below are the timestamps of the request parameters for the official verification ticket (component_verify_ticket) push and their converted standard time:
1625109905 2021-07-01 11:25:05
1625196309 2021-07-02 11:25:09
1625205285 2021-07-02 13:54:45
1625222097 2021-07-02 18:34:57
3. At the time points 13:05 and 13:15, it is possible to re-fetch the token.

3. View the specific time points of each HTTP request to fetch the token. See Figure 1.

View the specific time points of each HTTP request to fetch the token
Figure 1
Plaintext

-rw-r--r-- 1 nginx nginx  231 Jul  2 18:15 frontend-controllers-WxMsgEventController-componentAccessTokenData-1625220904.5864-1284321044.txt
-rw-r--r-- 1 nginx nginx  231 Jul  2 20:05 frontend-controllers-WxMsgEventController-componentAccessTokenData-1625227518.8292-2034263273.txt
-rw-r--r-- 1 nginx nginx  231 Jul  2 21:54 frontend-controllers-WxMsgEventController-componentAccessTokenData-1625234060.9016-2037842134.txt
-rw-r--r-- 1 nginx nginx  231 Jul  2 23:34 frontend-controllers-WxMsgEventController-componentAccessTokenData-1625240074.6086-129234930.txt
-rw-r--r-- 1 nginx nginx  231 Jul  3 01:24 frontend-controllers-WxMsgEventController-componentAccessTokenData-1625246642.1858-1357575717.txt
-rw-r--r-- 1 nginx nginx  231 Jul  3 03:04 frontend-controllers-WxMsgEventController-componentAccessTokenData-1625252663.3032-96333903.txt
-rw-r--r-- 1 nginx nginx  231 Jul  3 04:53 frontend-controllers-WxMsgEventController-componentAccessTokenData-1625259239.382-1358398298.txt
-rw-r--r-- 1 nginx nginx  231 Jul  3 06:34 frontend-controllers-WxMsgEventController-componentAccessTokenData-1625265273.2847-949998836.txt
-rw-r--r-- 1 nginx nginx  231 Jul  3 08:14 frontend-controllers-WxMsgEventController-componentAccessTokenData-1625271273.5153-1059028888.txt
-rw-r--r-- 1 nginx nginx  231 Jul  3 09:54 frontend-controllers-WxMsgEventController-componentAccessTokenData-1625277298.7813-310601964.txt
-rw-r--r-- 1 nginx nginx  231 Jul  3 11:44 frontend-controllers-WxMsgEventController-componentAccessTokenData-1625283877.9521-412669108.txt
-rw-r--r-- 1 nginx nginx  231 Jul  3 13:24 frontend-controllers-WxMsgEventController-componentAccessTokenData-1625289886.807-2092152976.txt
-rw-r--r-- 1 nginx nginx  231 Jul  3 15:14 frontend-controllers-WxMsgEventController-componentAccessTokenData-1625296463.0599-1080955996.txt
-rw-r--r-- 1 nginx nginx  231 Jul  3 16:54 frontend-controllers-WxMsgEventController-componentAccessTokenData-1625302484.4032-1651010868.txt
-rw-r--r-- 1 nginx nginx  231 Jul  3 18:44 frontend-controllers-WxMsgEventController-componentAccessTokenData-1625309067.7121-1067986815.txt
-rw-r--r-- 1 nginx nginx  231 Jul  3 20:24 frontend-controllers-WxMsgEventController-componentAccessTokenData-1625315074.8086-1306110047.txt
-rw-r--r-- 1 nginx nginx  231 Jul  3 22:14 frontend-controllers-WxMsgEventController-componentAccessTokenData-1625321659.2166-403019843.txt
-rw-r--r-- 1 nginx nginx  231 Jul  4 00:04 frontend-controllers-WxMsgEventController-componentAccessTokenData-1625328259.0217-93337953.txt
-rw-r--r-- 1 nginx nginx  231 Jul  4 01:44 frontend-controllers-WxMsgEventController-componentAccessTokenData-1625334265.2403-81093374.txt
-rw-r--r-- 1 nginx nginx  231 Jul  4 03:34 frontend-controllers-WxMsgEventController-componentAccessTokenData-1625340861.6377-119559848.txt
-rw-r--r-- 1 nginx nginx  231 Jul  4 05:14 frontend-controllers-WxMsgEventController-componentAccessTokenData-1625346863.7426-1003494675.txt
-rw-r--r-- 1 nginx nginx  231 Jul  4 06:54 frontend-controllers-WxMsgEventController-componentAccessTokenData-1625352868.7146-1351258397.txt
-rw-r--r-- 1 nginx nginx  231 Jul  4 08:34 frontend-controllers-WxMsgEventController-componentAccessTokenData-1625358895.2811-350216025.txt
-rw-r--r-- 1 nginx nginx  231 Jul  4 10:15 frontend-controllers-WxMsgEventController-componentAccessTokenData-1625364905.1292-483282692.txt
-rw-r--r-- 1 nginx nginx  231 Jul  4 12:05 frontend-controllers-WxMsgEventController-componentAccessTokenData-1625371510.5863-1975502382.txt
-rw-r--r-- 1 nginx nginx  231 Jul  4 13:54 frontend-controllers-WxMsgEventController-componentAccessTokenData-1625378081.9596-379002381.txt
-rw-r--r-- 1 nginx nginx  231 Jul  4 15:34 frontend-controllers-WxMsgEventController-componentAccessTokenData-1625384084.4452-1094694829.txt
-rw-r--r-- 1 nginx nginx  231 Jul  4 17:14 frontend-controllers-WxMsgEventController-componentAccessTokenData-1625390088.3557-588511027.txt
-rw-r--r-- 1 nginx nginx  231 Jul  4 19:05 frontend-controllers-WxMsgEventController-componentAccessTokenData-1625396707.8423-1886295111.txt
-rw-r--r-- 1 nginx nginx  231 Jul  4 20:54 frontend-controllers-WxMsgEventController-componentAccessTokenData-1625403282.8031-1069721276.txt
-rw-r--r-- 1 nginx nginx  231 Jul  4 22:44 frontend-controllers-WxMsgEventController-componentAccessTokenData-1625409877.6077-1660123114.txt
-rw-r--r-- 1 nginx nginx  231 Jul  5 00:34 frontend-controllers-WxMsgEventController-componentAccessTokenData-1625416464.5966-45554414.txt
-rw-r--r-- 1 nginx nginx  231 Jul  5 02:23 frontend-controllers-WxMsgEventController-componentAccessTokenData-1625423039.146-1550811908.txt
-rw-r--r-- 1 nginx nginx  231 Jul  5 04:04 frontend-controllers-WxMsgEventController-componentAccessTokenData-1625429059.9922-66909225.txt
-rw-r--r-- 1 nginx nginx  231 Jul  5 05:54 frontend-controllers-WxMsgEventController-componentAccessTokenData-1625435642.6323-187433523.txt
-rw-r--r-- 1 nginx nginx  231 Jul  5 07:34 frontend-controllers-WxMsgEventController-componentAccessTokenData-1625441682.8374-716247506.txt

4. Out of a total of 45 requests, the majority of calls have an average interval of around 1 hour and 50 minutes (28 times), while a smaller portion have an average interval of around 1 hour and 40 minutes (17 times).

Plaintext
1 hour 50 minutes:
18:15 - 20:05
20:05 - 21:54
23:34 - 01:24
03:04 - 04:53
09:54 - 11:44
13:24 - 15:14
16:54 - 18:44
20:24 - 22:14
22:14 - 00:04
01:44 - 03:34
10:15 - 12:05
12:05 - 13:54
17:14 - 19:05
19:05 - 20:54
20:54 - 22:44
22:44 - 00:34
00:34 - 02:23
04:04 - 05:54

1 hour 40 minutes:
21:54 - 23:34
01:24 - 03:04
04:53 - 06:34
06:34 - 08:14
08:14 - 09:54
11:44 - 13:24
15:14 - 16:54
18:44 - 20:24
00:04 - 01:44
03:34 - 05:14
05:14 - 06:54
06:54 - 08:34
08:34 - 10:15
13:54 - 15:34
15:34 - 17:14
02:23 - 04:04
05:54 - 07:34

5. View the content of its response. Samples: 20:05, 21:54, 23:34. The expiration times of the access tokens in the responses are: 2021-07-02 22:05:18, 2021-07-02 23:54:20, 2021-07-03 01:34:34, respectively. This meets expectations. See Figure 2.

View the content of its response. Samples: 20:05, 21:54, 23:34. The expiration times of the access tokens in the responses are: 2021-07-02 22:05:18, 2021-07-02 23:54:20, 2021-07-03 01:34:34, respectively. This meets expectations
Figure 2
Plaintext

[root@api-7f9d76b849-kckxz runtime]# cat frontend-controllers-WxMsgEventController-componentAccessTokenData-1625227518.8292-2034263273.txt
Array
(
    [component_access_token] => 46_yRwzjrgTGzx94RykB9pycN-nlEXnvYQNSbXC0gfWJ9b091k-wLhFgyeZTIovEIVVJpWvBk4em_byyQrNzfxjjMGj6mKfz-tKHWUKJmxGZDA02t5rdWOvcaYez7EmRZYJ6szrAR4qcHDHeXgPZTXjAGAEYC
    [expires_at] => 1625234718
)
[root@api-7f9d76b849-kckxz runtime]# cat frontend-controllers-WxMsgEventController-componentAccessTokenData-1625234060.9016-2037842134.txt
Array
(
    [component_access_token] => 46_2hBMmx4Ww0pMYSsLCun4EW80WOcNHUMrOE0i-niX0599OTCi5xOHw5yrJ3It7Um9tyXt9x3MiGuazXgPLAXKUP-3WMIO9Du8ECPk4nM8Ls_iKOGGLrn5QAyGlXueXI4d8glGjPexUdFi53iSERYdAFALYF
    [expires_at] => 1625241260
)
[root@api-7f9d76b849-kckxz runtime]# cat frontend-controllers-WxMsgEventController-componentAccessTokenData-1625240074.6086-129234930.txt
Array
(
    [component_access_token] => 46_FfM2ZagUut2Z33lVf-PZrUzKrauKTrMU4EcDjZ39sIB3B6RmbKKTNqWfioZgpSEubmUkQEF3foT6gUO5JR-ILanvR8zF7eafBgKh22CRFX8yLcGEMlPDw9zVJPiXWt774FVh2H-u5oV1taXJGQVhAHANXC
    [expires_at] => 1625247274
)
[root@api-7f9d76b849-kckxz runtime]# 

6. Finally, adjust the 20 minutes to 25 minutes, meaning that within a 2-hour window, it ensures the token is re-fetched after 1 hour and 40 minutes. If the fetch fails, it can continue to re-fetch the token after 1 hour and 50 minutes. This provides a total of 2 opportunities, resulting in stronger fault tolerance.

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系