00001 /** @file 00002 00003 A brief file description 00004 00005 @section license License 00006 00007 Licensed to the Apache Software Foundation (ASF) under one 00008 or more contributor license agreements. See the NOTICE file 00009 distributed with this work for additional information 00010 regarding copyright ownership. The ASF licenses this file 00011 to you under the Apache License, Version 2.0 (the 00012 "License"); you may not use this file except in compliance 00013 with the License. You may obtain a copy of the License at 00014 00015 http://www.apache.org/licenses/LICENSE-2.0 00016 00017 Unless required by applicable law or agreed to in writing, software 00018 distributed under the License is distributed on an "AS IS" BASIS, 00019 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00020 See the License for the specific language governing permissions and 00021 limitations under the License. 00022 */ 00023 00024 // 00025 #include "libts.h" 00026 #include "Map.h" 00027 00028 #ifndef _HTTP_CONNECTION_COUNT_H_ 00029 00030 /** 00031 * Singleton class to keep track of the number of connections per host 00032 */ 00033 class ConnectionCount 00034 { 00035 public: 00036 /** 00037 * Static method to get the instance of the class 00038 * @return Returns a pointer to the instance of the class 00039 */ 00040 static ConnectionCount *getInstance() { 00041 return &_connectionCount; 00042 } 00043 00044 /** 00045 * Gets the number of connections for the host 00046 * @param ip IP address of the host 00047 * @return Number of connections 00048 */ 00049 int getCount(const IpEndpoint& addr) { 00050 ink_mutex_acquire(&_mutex); 00051 int count = _hostCount.get(ConnAddr(addr)); 00052 ink_mutex_release(&_mutex); 00053 return count; 00054 } 00055 00056 /** 00057 * Change (increment/decrement) the connection count 00058 * @param ip IP address of the host 00059 * @param delta Default is +1, can be set to negative to decrement 00060 */ 00061 void incrementCount(const IpEndpoint& addr, const int delta = 1) { 00062 ConnAddr caddr(addr); 00063 ink_mutex_acquire(&_mutex); 00064 int count = _hostCount.get(caddr); 00065 _hostCount.put(caddr, count + delta); 00066 ink_mutex_release(&_mutex); 00067 } 00068 00069 struct ConnAddr { 00070 IpEndpoint _addr; 00071 00072 ConnAddr() { ink_zero(_addr); } 00073 ConnAddr(int x) { ink_release_assert(x == 0); ink_zero(_addr); } 00074 ConnAddr(const IpEndpoint& addr) : _addr(addr) { } 00075 operator bool() { return ats_is_ip(&_addr); } 00076 }; 00077 00078 class ConnAddrHashFns { 00079 public: 00080 static uintptr_t hash(ConnAddr& addr) { return (uintptr_t) ats_ip_hash(&addr._addr.sa); } 00081 static int equal(ConnAddr& a, ConnAddr& b) { return ats_ip_addr_eq(&a._addr, &b._addr); } 00082 }; 00083 00084 private: 00085 // Hide the constructor and copy constructor 00086 ConnectionCount() { 00087 ink_mutex_init(&_mutex, "ConnectionCountMutex"); 00088 } 00089 ConnectionCount(const ConnectionCount & /* x ATS_UNUSED */) { } 00090 00091 static ConnectionCount _connectionCount; 00092 HashMap<ConnAddr, ConnAddrHashFns, int> _hostCount; 00093 ink_mutex _mutex; 00094 }; 00095 00096 #endif