TimerClusteredRateLimitingLocalCache.java

  1. /*
  2.  * GovWay - A customizable API Gateway
  3.  * https://govway.org
  4.  *
  5.  * Copyright (c) 2005-2025 Link.it srl (https://link.it).
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License version 3, as published by
  9.  * the Free Software Foundation.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  *
  19.  */

  20. package org.openspcoop2.pdd.timers;

  21. import java.util.HashMap;
  22. import java.util.Map;
  23. import java.util.Set;
  24. import java.util.Map.Entry;

  25. import org.openspcoop2.core.controllo_traffico.beans.DatiCollezionati;
  26. import org.openspcoop2.core.controllo_traffico.beans.IDUnivocoGroupByPolicy;
  27. import org.openspcoop2.core.controllo_traffico.driver.IPolicyGroupByActiveThreadsInMemory;
  28. import org.openspcoop2.core.controllo_traffico.driver.PolicyException;
  29. import org.openspcoop2.core.controllo_traffico.driver.PolicyShutdownException;
  30. import org.openspcoop2.pdd.core.controllo_traffico.policy.driver.GestorePolicyAttiveInMemory;
  31. import org.openspcoop2.pdd.core.controllo_traffico.policy.driver.PolicyGroupByActiveThreads;
  32. import org.openspcoop2.pdd.core.controllo_traffico.policy.driver.hazelcast.PolicyGroupByActiveThreadsDistributedLocalCache;
  33. import org.openspcoop2.utils.threads.BaseThread;
  34. import org.slf4j.Logger;

  35. /**    
  36.  *  TimerClusteredRateLimitingLocalCache
  37.  *
  38.  * @author Francesco Scarlato (scarlato@link.it)
  39.  * @author $Author$
  40.  * @version $Rev$, $Date$
  41.  */
  42. public class TimerClusteredRateLimitingLocalCache extends BaseThread{
  43.    
  44.     private final GestorePolicyAttiveInMemory gestorePolicy;
  45.     private Logger log = null;
  46.    
  47.     public TimerClusteredRateLimitingLocalCache(Logger log, GestorePolicyAttiveInMemory gestorePolicy) {
  48.         this.log = log;
  49.         this.gestorePolicy = gestorePolicy;
  50.     }
  51.    
  52.     @Override
  53.     protected void process() {
  54.         try {
  55.             //System.out.println("TIMER DI AGGIORNAMENTO LOCAL CACHE MAP");
  56.            
  57.             this.log.info("Sync RateLimiting policy counters ...");
  58.            
  59.             updateLocalCacheMap();
  60.            
  61.             this.log.info("Sync RateLimiting policy counters finished");
  62.            
  63.         } catch (PolicyException e) {
  64.             this.log.error(e.getMessage(),e);
  65.         } catch (PolicyShutdownException e) {
  66.             this.setStop(true);
  67.         }
  68.        
  69.     }

  70.     private void updateLocalCacheMap() throws PolicyShutdownException, PolicyException {
  71.        
  72.         Set<Entry<String, IPolicyGroupByActiveThreadsInMemory>> activeThreadsPolicies = this.gestorePolicy.entrySet();
  73.        
  74.        
  75.         for (var policy : activeThreadsPolicies) {
  76.            
  77.             this.log.debug("["+policy.getKey()+"] update ...");
  78.            
  79.             PolicyGroupByActiveThreadsDistributedLocalCache distributedPolicy = (PolicyGroupByActiveThreadsDistributedLocalCache) policy.getValue();
  80.            
  81.             Map<IDUnivocoGroupByPolicy, DatiCollezionati> mapActiveThreads = new HashMap<IDUnivocoGroupByPolicy, DatiCollezionati>();
  82.             for (var entry : distributedPolicy.getDistributedMapActiveThreads().entrySet()) {
  83.                 mapActiveThreads.put(entry.getKey(), entry.getValue());
  84.             }
  85.            
  86.             PolicyGroupByActiveThreads localPolicy = distributedPolicy.getLocalPolicy();
  87.             localPolicy.setMapActiveThreads(mapActiveThreads);
  88.            
  89.             this.log.debug("["+policy.getKey()+"] update ok");
  90.         }
  91.        
  92.     }
  93. }