87 lines
2.6 KiB
C#
87 lines
2.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using MongoDB.Driver;
|
|
|
|
|
|
using MONGO_DB_LIST = System.String;
|
|
|
|
|
|
namespace ServerCore;
|
|
|
|
|
|
// HANDOVER: MongoDB Wrapper 클래스 이고, MongoDB 연결을 위한 함수를 제공 한다.
|
|
|
|
public abstract class MongoDbConnectorBase : IDisposable
|
|
{
|
|
private IMongoClient? m_mongo_client;
|
|
|
|
public MongoDbConnectorBase()
|
|
{
|
|
}
|
|
|
|
public async Task<(bool, List<MONGO_DB_LIST>)> initAndVerifyDb( string connectionString
|
|
, int minConnectionPoolSize, int maxConnectionPoolSize
|
|
, int waitQueueTimeoutSecs )
|
|
{
|
|
var err_msg = string.Empty;
|
|
|
|
try
|
|
{
|
|
if (true == connectionString.isNullOrWhiteSpace())
|
|
{
|
|
throw new ArgumentException("Connection string is null or empty.", nameof(connectionString));
|
|
}
|
|
|
|
if (minConnectionPoolSize < 0 || maxConnectionPoolSize <= 0 || minConnectionPoolSize > maxConnectionPoolSize)
|
|
{
|
|
throw new ArgumentOutOfRangeException("Invalid pool size configuration.");
|
|
}
|
|
|
|
if (waitQueueTimeoutSecs < 0)
|
|
{
|
|
throw new ArgumentOutOfRangeException(nameof(waitQueueTimeoutSecs), "Wait queue timeout must be non-negative.");
|
|
}
|
|
|
|
var settings = MongoClientSettings.FromConnectionString(connectionString);
|
|
settings.MinConnectionPoolSize = minConnectionPoolSize;
|
|
settings.MaxConnectionPoolSize = maxConnectionPoolSize;
|
|
settings.WaitQueueTimeout = TimeSpan.FromSeconds(waitQueueTimeoutSecs);
|
|
|
|
var mongo_client = new MongoClient(settings);
|
|
|
|
var cursor = await mongo_client.ListDatabaseNamesAsync();
|
|
var db_list = await cursor.ToListAsync();
|
|
|
|
m_mongo_client = mongo_client;
|
|
|
|
return (true, db_list);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Log.getLogger().error($"Exception !!!, Failed to perform in MongoDbConnectorBase.initAndVerifyDb() !!! : exception:{e} - connectionString:{connectionString}");
|
|
}
|
|
|
|
return (false, new List<MONGO_DB_LIST>());
|
|
}
|
|
|
|
public IMongoClient getMongoClient()
|
|
{
|
|
NullReferenceCheckHelper.throwIfNull(m_mongo_client, () => $"m_mongo_client is null !!!");
|
|
return m_mongo_client;
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
m_mongo_client = null;
|
|
GC.SuppressFinalize(this);
|
|
}
|
|
|
|
public void disposeMongoDb()
|
|
{
|
|
Dispose();
|
|
}
|
|
}
|