Connection pool

For convenience we have created two classes to enable pool-based connections to the MT4 server: ManagerPool and ManagerPoolEx.

For convenience we have created two classes to enable pool-based connections to the MT4 server: ManagerPool and ManagerPoolEx.

Suppose you need to manipulate data on MT4 from multiple threads. The Manager API is single-threaded, so ManagerPool makes multiple connections to the MT4 server and lets you take an available connection from the pool. This increases total throughput at the expense of additional load on the MT4 server.

The example below shows a pool with 8 elements, with 100 threads simultaneously trying to read data from MT4.

public static class ManagerPollExamples
{
    private static readonly Logger Log = LogManager.GetCurrentClassLogger();
 
    public static void GetUsers()
    {
        var pool = new ManagerPool(Constants.Server, Constants.Login, Constants.Password, maxPollSize: 8)
        {
            Logger = (ctx, type, message, exception) =>
            {
                if (exception != null)
                    Log.Error(exception);
                else
                    Log.Info($"[{type}] {message}");
            }
        };
 
        pool.Run();
 
        Parallel.For(0, 100, (l, state) =>
        {
            using (var poolElement = pool.Get(TimeSpan.FromSeconds(3)))
            {
                var sw = new Stopwatch();
                sw.Start();
                Log.Debug($"[{l}] requesting users...");
                var allUsers = poolElement.Data.Value.UsersRequest();
                sw.Stop();
                Log.Debug($"[{l}] {allUsers.Count} users requested within {sw.Elapsed}");
            }
        });
    }
}