This article mainly analyzes the Soul open source gateway HTTP long polling data synchronization backend management soul-admin source.

background

[11] – HTTP long polling synchronous analysis of the basic principle of HTTP long polling, And check the source code to learn that the gateway application soul-bootstrap end circulates the configuration change information of the HTTP request soul-admin end after startup, and sets the timeout time of 90s for each request.

This article mainly analyzes from the soul-admin side.

HTTP long polling synchronization source analysis

Soul – admin end analysis

  • Soul-admin received the soul-bootstrap polling HTTP request.

  • Soul-admin will compare the MD5 in the request with whether the current configuration is the same, that is, whether the configuration is changed. If there is a change, the soul-admin will send a response immediately.

  • If the data changes, not used regularly executed task thread pool ScheduledThreadPoolExecutor asynchronous response;

  • The asynchronous response task is scheduled after 60 seconds. The main operation is to remove the blocking queue and send the response.


Use of the soul-admin side timed thread pool

  • HttpLongPollingDataChangedListener initialization time to create a thread pool ScheduledExecutorService regularly;

    private final ScheduledExecutorService scheduler; public HttpLongPollingDataChangedListener(final HttpSyncProperties httpSyncProperties) { //... this.scheduler = new ScheduledThreadPoolExecutor(1, SoulThreadFactory.create("long-polling", true)); / /... }Copy the code
  • During initialization, the scheduled thread pool is scheduled every 5 minutes to refresh local configuration data. The default configuration is as follows:

  • When the soul-bootstrap end polling request comes in, it is added to the thread pool task, which mainly sends the response asynchronously.

    public void doLongPolling(final HttpServletRequest request, final HttpServletResponse response) {
    
        // ....
        scheduler.execute(new LongPollingClient(asyncContext, clientIp, HttpConstants.SERVER_MAX_HOLD_TIMEOUT));
    }
    Copy the code
  • When soul-admin monitors data changes in observer mode, it puts in the DataChangeTask DataChangeTask, which returns the changes made during the request to the requester.

conclusion

  • Compared to the previous analysisWebsocket synchronization 和 They are synchronousPolicy, HTTP long polling is onepullMode, not the gateway management side will take the initiative to change the configurationpushTo the gateway cluster, so the timeliness may be relatively poor.

series

  • Soul source learning [1] – preliminary exploration
  • Soul source learning [2] – divide load balancing plug-in
  • Soul source learning [3] – Dubbo plug-in
  • Soul source learning [4] – RateLimiter stream limiting plug-in
  • Soul source learning [5] – SpringCloud plug-in
  • Soul source learning [6] – Chain of responsibility mode
  • Soul source learning [7] – Data synchronization strategy websocket synchronization
  • [8] – Data synchronization strategy for ZooKeeper synchronization
  • [9] – ZooKeeper Synchronization (2)
  • Soul source learning [10] – Observer mode application
  • Soul source learning [11] – Data synchronization strategy HTTP long poll synchronization