imitate load balancing programmatically

I have implemented some simple messaging system IMS for my own use inside the application, that allows sending and recieving message for objects.

I have a singleton object IMSContext that has a method transfer which is used to pass messages by every object

IMSContext.instance.transfer(message)

Also that IMSContext stores all IMS objects.

As I use sole method to transfer message, I want to imitate some kind of load balancing – assuming that this sole method is a bottleneck – when there are too much messages are being transferred I want to use some kind of mechanism that would move the flow of messages to another method…or maybe even object.

But I have no idea how to do it. Could recommend something or help with the approach?

6

Not really sure I understand what you are trying to achieve. But here goes.

Problem summary

  • You have a Messaging class with method transfer
  • You want to spread the calls to transfer over multiple instances of the Messaging class
  • You want the calling code to be unaware of the many instances

first off. Don’t use singletons.

second. wrap Messaging in a new class MessagingLoadBalanced which holds a list of Messaging instances. something like:

class MessagingLoadBalanced : IMessaging
{
    private List<Messaging> messagings;
    private int i = 0;

    Public void Transfer(object message)
    {
        messagings[i].Transfer(message);
        i++;
        if(i >= messagings.Length) { i=0;}
    }
}

lastly, use this new class in your calling code as if it was a normal IMessaging

1

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *