1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements.  See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership.  The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License.  You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 #include "request_queue.h"
20 #include "ats_fastcgi.h"
21 using namespace ats_plugin;
22 
RequestQueue()23 RequestQueue::RequestQueue()
24 {
25   mutex                     = TSMutexCreate();
26   FcgiPluginConfig *gConfig = InterceptGlobal::plugin_data->getGlobalConfigObj();
27   max_queue_size            = gConfig->getRequestQueueSize();
28 }
29 
~RequestQueue()30 RequestQueue::~RequestQueue()
31 {
32   max_queue_size = 0;
33   TSMutexDestroy(mutex);
34 }
35 
36 uint
isQueueFull()37 RequestQueue::isQueueFull()
38 {
39   if (pending_list.size() >= max_queue_size) {
40     return 1;
41   } else {
42     return 0;
43   }
44 }
45 
46 uint
getSize()47 RequestQueue::getSize()
48 {
49   return pending_list.size();
50 }
51 
52 uint
isQueueEmpty()53 RequestQueue::isQueueEmpty()
54 {
55   if (pending_list.empty()) {
56     return 1;
57   } else {
58     return 0;
59   }
60 }
61 
62 uint
addToQueue(ServerIntercept * intercept)63 RequestQueue::addToQueue(ServerIntercept *intercept)
64 {
65   // TSMutexLock(mutex);
66   if (!isQueueFull()) {
67     pending_list.push(intercept);
68   }
69   // TODO: handle queue full use case
70   // TSMutexUnlock(mutex);
71   return 1;
72 }
73 
74 ServerIntercept *
popFromQueue()75 RequestQueue::popFromQueue()
76 {
77   ServerIntercept *intercept = nullptr;
78   // TSMutexLock(mutex);
79   if (!pending_list.empty()) {
80     intercept = pending_list.front();
81     pending_list.pop();
82   }
83   // TSMutexUnlock(mutex);
84   return intercept;
85 }
86