Fledge
An open source edge computing platform for industrial users
connection_manager.h
1 #ifndef _CONNECTION_MANAGER_H
2 #define _CONNECTION_MANAGER_H
3 /*
4  * Fledge storage service.
5  *
6  * Copyright (c) 2017 OSisoft, LLC
7  *
8  * Released under the Apache 2.0 Licence
9  *
10  * Author: Mark Riddoch
11  */
12 
13 #include <plugin_api.h>
14 #include <list>
15 #include <mutex>
16 #include <thread>
17 
18 class Connection;
19 class DiskSpaceMonitor;
20 
24 class ConnectionManager {
25  public:
27  void growPool(unsigned int);
28  unsigned int shrinkPool(unsigned int);
30  void release(Connection *);
31  void shutdown();
32  void setError(const char *, const char *, bool);
33  PLUGIN_ERROR *getError()
34  {
35  return &lastError;
36  }
37  void background();
38  void setVacuumInterval(long hours) {
39  m_vacuumInterval = 60 * 60 * hours;
40  };
41  void setPersist(bool persist, const std::string& filename = "")
42  {
43  m_persist = persist;
44  m_filename = filename;
45  }
46  bool persist() { return m_persist; };
47  std::string filename() { return m_filename; };
48  void setPurgeBlockSize(unsigned long purgeBlockSize);
49  protected:
51 
52  private:
53  static ConnectionManager *instance;
54  protected:
55  std::list<Connection *> idle;
56  std::list<Connection *> inUse;
57  std::mutex idleLock;
58  std::mutex inUseLock;
59  std::mutex errorLock;
60  PLUGIN_ERROR lastError;
61  bool m_trace;
62  bool m_shutdown;
63  std::thread *m_background;
64  long m_vacuumInterval;
65  bool m_persist;
66  std::string m_filename;
67  unsigned long m_purgeBlockSize;
68  DiskSpaceMonitor *m_diskSpaceMonitor;
69 };
70 
71 #endif
void growPool(unsigned int)
Grow the connection pool by the number of connections specified.
Definition: connection_manager.cpp:59
unsigned int shrinkPool(unsigned int)
Attempt to shrink the number of connections in the idle pool.
Definition: connection_manager.cpp:82
Connection * allocate()
Allocate a connection from the idle pool.
Definition: connection_manager.cpp:110
Singleton class to manage Postgres connection pool.
Definition: connection_manager.h:22
void shutdown()
Called at shutdown.
Definition: connection_manager.cpp:35
static ConnectionManager * getInstance()
Return the singleton instance of the connection manager.
Definition: connection_manager.cpp:44
void release(Connection *)
Release a connection back to the idle pool for reallocation.
Definition: connection_manager.cpp:142
void background()
Background thread used to execute periodic tasks and oversee the database activity.
Definition: connection_manager.cpp:535
void setError(const char *, const char *, bool)
Set the last error information for a plugin.
Definition: connection_manager.cpp:159
A class to monitor the free disk space used to store the various storage databases.
Definition: disk_monitor.h:22
void setPurgeBlockSize(unsigned long purgeBlockSize)
Set the purge block size in each of the connections.
Definition: connection_manager.cpp:102
Definition: connection.h:32
Structure used by plugins to return error information.
Definition: plugin_api.h:44