1. 程式人生 > >linux c/c++ 後臺開發之—連線池

linux c/c++ 後臺開發之—連線池

在網際網路後臺開發中經常需要需要範圍一些公共資源,如DB,cache, MQ,  最典型的的就是mysql, memcached, redis, 以及一些代理服務;

通常在高併發,高訪問量的情況下,起停連線,是不合適的,也容易將連線佔滿, 尤其是工作執行緒多的情況,如果每個工作執行緒建立連線,如果服務多的情況會使得連線不夠用,而且連線有時候會出現空閒的情況;連線池的作用就是保持一定數量的連線,提供給多執行緒服務使用,通過壓力測試配置合適的連線數量,實現效能和資源的最優化。

下面是自己實現的一個通用的單例多執行緒安全的連線池模板,模板引數T必須是一個封裝好的連線類

/***************************************************************
function: connect pool template for mysql, redis, memcached ...
author: liuyi
date: 2016.04.13
version: 1.0
***************************************************************/

#ifndef CONNECT_POOL_H
#define COMMECT_POOL_H

#include <stdlib.h>
#include <iostream>
#include <vector>
#include <pthread.h>
using namespace std;

template<class T>
class connect_pool
{
	public:
		static connect_pool<T> * get_instance()
		{
			static connect_pool<T> s_instance;
			return &s_instance;
		}

		bool init(vector<T*> connect_ptrs)
		{
			if(connect_ptrs.empty())
				return false;

			pthread_mutex_lock(m_mutex);
			for(size_t i = 0; i < connect_ptrs.size(); i++)
			{
				m_used_index_vect.push_back(0);
				m_connect_vect.push_back(connect_ptrs[i]);
			}
			pthread_mutex_unlock(m_mutex);

			return true;
		}

		int get_connect_index()
		{
			int index = -1;
			int rand_index = 0;

			pthread_mutex_lock(m_mutex);

			if(0 != m_used_index_vect.size())
			{
				rand_index = rand() % m_used_index_vect.size();
			}

			for(int j = rand_index; j < m_used_index_vect.size(); j++)
			{
				if(0 == m_used_index_vect[j])
				{
					m_used_index_vect[j] = 1;
					index = j;
					break;
				}
			}

			if(index == -1)
			{
				for(int i = 0; i < rand_index; i++)
				{
					if(0 == m_used_index_vect[i])
					{
						m_used_index_vect[i] = 1;
						index = i;
						break;
					}
				}
			}

			pthread_mutex_unlock(m_mutex);
			
			return index;
		}

		T* get_connect(int index)const
		{
			pthread_mutex_lock(m_mutex);
			if(index >= 0 && index < m_connect_vect.size())
			{
				T* p = m_connect_vect[index];
				pthread_mutex_unlock(m_mutex);
				return p;
			}

			return NULL;
		}
		
		bool return_connect_2_pool(int index)
		{
			if(index < 0)
				return false;

			pthread_mutex_lock(m_mutex);
			if(index < m_used_index_vect.size())
			{
				m_used_index_vect[index] = 0;
				pthread_mutex_unlock(m_mutex);
				return true;
			}
			pthread_mutex_unlock(m_mutex);

			return false;
		}

		void remove_connect_from_pool(int index)
		{
			pthread_mutex_lock(m_mutex);
			if(index >= 0 && index < m_used_index_vect.size())
			{
				m_used_index_vect[index] = 1;
			}
			pthread_mutex_unlock(m_mutex);
		}

		bool replace_alive_connect(T* new_connect, int index)
		{
			bool ret = false;
			pthread_mutex_lock(m_mutex);
			if(index >= 0 && index < m_used_index_vect.size())
			{
				m_used_index_vect[index] = 0;
				m_connect_vect[index] = new_connect;
				ret = true;
			}
			pthread_mutex_unlock(m_mutex);

			return ret;
		}

	private:
		connect_pool()
		{
			m_mutex = new pthread_mutex_t;
			pthread_mutex_init(m_mutex, NULL);
			srand(time(NULL));
		}

		~connect_pool()
		{
			if(NULL != m_mutex)
			{
				delete m_mutex;
				m_mutex = NULL;
			}
		}

	private:
		pthread_mutex_t *m_mutex;
		vector<int> m_used_index_vect;
		vector<T*> m_connect_vect;
};

#endif