Update project structure to org.onap
[dmaap/datarouter.git] / datarouter-node / src / main / java / org / onap / dmaap / datarouter / node / RateLimitedOperation.java
1 /*******************************************************************************\r
2  * ============LICENSE_START==================================================\r
3  * * org.onap.dmaap\r
4  * * ===========================================================================\r
5  * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
6  * * ===========================================================================\r
7  * * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * * you may not use this file except in compliance with the License.\r
9  * * You may obtain a copy of the License at\r
10  * * \r
11  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * * \r
13  *  * Unless required by applicable law or agreed to in writing, software\r
14  * * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * * See the License for the specific language governing permissions and\r
17  * * limitations under the License.\r
18  * * ============LICENSE_END====================================================\r
19  * *\r
20  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
21  * *\r
22  ******************************************************************************/\r
23 \r
24 \r
25 package org.onap.dmaap.datarouter.node;\r
26 \r
27 import java.util.*;\r
28 \r
29 /**\r
30  *      Execute an operation no more frequently than a specified interval\r
31  */\r
32 \r
33 public abstract class RateLimitedOperation implements Runnable  {\r
34         private boolean marked; // a timer task exists\r
35         private boolean executing;      // the operation is currently in progress\r
36         private boolean remark; // a request was made while the operation was in progress\r
37         private Timer   timer;\r
38         private long    last;   // when the last operation started\r
39         private long    mininterval;\r
40         /**\r
41          *      Create a rate limited operation\r
42          *      @param mininterval      The minimum number of milliseconds after the last execution starts before a new execution can begin\r
43          *      @param timer    The timer used to perform deferred executions\r
44          */\r
45         public RateLimitedOperation(long mininterval, Timer timer) {\r
46                 this.timer = timer;\r
47                 this.mininterval = mininterval;\r
48         }\r
49         private class deferred extends TimerTask        {\r
50                 public void run() {\r
51                         execute();\r
52                 }\r
53         }\r
54         private synchronized void unmark() {\r
55                 marked = false;\r
56         }\r
57         private void execute() {\r
58                 unmark();\r
59                 request();\r
60         }\r
61         /**\r
62          *      Request that the operation be performed by this thread or at a later time by the timer\r
63          */\r
64         public void request() {\r
65                 if (premark()) {\r
66                         return;\r
67                 }\r
68                 do {\r
69                         run();\r
70                 } while (demark());\r
71         }\r
72         private synchronized boolean premark() {\r
73                 if (executing) {\r
74                         // currently executing - wait until it finishes\r
75                         remark = true;\r
76                         return(true);\r
77                 }\r
78                 if (marked) {\r
79                         // timer currently running - will run when it expires\r
80                         return(true);\r
81                 }\r
82                 long now = System.currentTimeMillis();\r
83                 if (last + mininterval > now) {\r
84                         // too soon - schedule a timer\r
85                         marked = true;\r
86                         timer.schedule(new deferred(), last + mininterval - now);\r
87                         return(true);\r
88                 }\r
89                 last = now;\r
90                 executing = true;\r
91                 // start execution\r
92                 return(false);\r
93         }\r
94         private synchronized boolean demark() {\r
95                 executing = false;\r
96                 if (remark) {\r
97                         remark = false;\r
98                         return(!premark());\r
99                 }\r
100                 return(false);\r
101         }\r
102 }\r