forked from apolloconfig/apollo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
do async notification if there are too many clients to notify
- Loading branch information
Showing
3 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ | |
import com.google.gson.Gson; | ||
import com.google.gson.reflect.TypeToken; | ||
|
||
import com.ctrip.framework.apollo.biz.config.BizConfig; | ||
import com.ctrip.framework.apollo.biz.entity.ReleaseMessage; | ||
import com.ctrip.framework.apollo.biz.message.ReleaseMessageListener; | ||
import com.ctrip.framework.apollo.biz.message.Topics; | ||
|
@@ -22,6 +23,7 @@ | |
import com.ctrip.framework.apollo.configservice.util.WatchKeysUtil; | ||
import com.ctrip.framework.apollo.core.ConfigConsts; | ||
import com.ctrip.framework.apollo.core.dto.ApolloConfigNotification; | ||
import com.ctrip.framework.apollo.core.utils.ApolloThreadFactory; | ||
import com.ctrip.framework.apollo.tracer.Tracer; | ||
|
||
import org.slf4j.Logger; | ||
|
@@ -41,6 +43,9 @@ | |
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* @author Jason Song([email protected]) | ||
|
@@ -61,6 +66,8 @@ public class NotificationControllerV2 implements ReleaseMessageListener { | |
new TypeToken<List<ApolloConfigNotification>>() { | ||
}.getType(); | ||
|
||
private final ExecutorService largeNotificationBatchExecutorService; | ||
|
||
@Autowired | ||
private WatchKeysUtil watchKeysUtil; | ||
|
||
|
@@ -76,6 +83,14 @@ public class NotificationControllerV2 implements ReleaseMessageListener { | |
@Autowired | ||
private Gson gson; | ||
|
||
@Autowired | ||
private BizConfig bizConfig; | ||
|
||
public NotificationControllerV2() { | ||
largeNotificationBatchExecutorService = Executors.newSingleThreadExecutor(ApolloThreadFactory.create | ||
("NotificationControllerV2", true)); | ||
} | ||
|
||
@RequestMapping(method = RequestMethod.GET) | ||
public DeferredResult<ResponseEntity<List<ApolloConfigNotification>>> pollNotification( | ||
@RequestParam(value = "appId") String appId, | ||
|
@@ -220,6 +235,27 @@ public void handleMessage(ReleaseMessage message, String channel) { | |
//create a new list to avoid ConcurrentModificationException | ||
List<DeferredResult<ResponseEntity<List<ApolloConfigNotification>>>> results = | ||
Lists.newArrayList(deferredResults.get(content)); | ||
|
||
//do async notification if too many clients | ||
if (results.size() > bizConfig.releaseMessageNotificationBatch()) { | ||
largeNotificationBatchExecutorService.submit(() -> { | ||
logger.debug("Async notify {} clients for key {} with batch {}", results.size(), content, | ||
bizConfig.releaseMessageNotificationBatch()); | ||
for (int i = 0; i < results.size(); i++) { | ||
if (i > 0 && i % bizConfig.releaseMessageNotificationBatch() == 0) { | ||
try { | ||
TimeUnit.MILLISECONDS.sleep(bizConfig.releaseMessageNotificationBatchIntervalInMilli()); | ||
} catch (InterruptedException e) { | ||
//ignore | ||
} | ||
} | ||
logger.debug("Async notify {}", results.get(i)); | ||
results.get(i).setResult(notification); | ||
} | ||
}); | ||
return; | ||
} | ||
|
||
logger.debug("Notify {} clients for key {}", results.size(), content); | ||
|
||
for (DeferredResult<ResponseEntity<List<ApolloConfigNotification>>> result : results) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters