Option to set WorkerThreads for SmppServerClient on Bind
Posted: Thu Jun 08, 2017 10:51 am
The WorkerThreads property in SmppClientBase class works only if set during the evConnected event. In my case, for SmppServerClient, I wanted to set this during the Bind event since this is the time I get to know which account is binding to my server and what is the allowed TPS for that account, then I can set the WorkerThreads accordingly. To achieve this, I added the following new method to ProducerConsumerQueue class,
and SmppClientBase class.
Now, inside SmppServerClient class, under OnBind method I did the following changes.
See if the above makes sense or if you have a better suggestion.
Code: Select all
//new method
public void IncreaseWorkers(int newWorkerCount)
{
//return if new count is same or less than old count
if (newWorkerCount <= Workers)
return;
lock (_threadLocker)
{
//return if stopped flag is raised
if (Thread.VolatileRead(ref _stopped) == 1)
return;
//get the previous count of workers
var workers = Workers;
//resize workers array to accommodate new threads
System.Array.Resize(ref _workers, newWorkerCount);
// Create and start a separate thread for each new worker
for (int i = workers; i < newWorkerCount; i++)
{
var thread = new Thread(Consume);
_workers[i] = thread;
thread.Start();
}
}
}
Code: Select all
//new method
internal void IncreaseWorkerThreads()
{
_receiveQueue.IncreaseWorkers(WorkerThreads);
}
Code: Select all
...
//get the initial WorkerThreads value
var workerThreads = this.WorkerThreads;
if (evBind != null)
...
if (data.Response != null && data.Response.Status == Common.CommandStatus.ESME_ROK)
{
//if the WorkerThreads value has increased, call the new method
if (this.WorkerThreads > workerThreads)
IncreaseWorkerThreads();
...