-1

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
  • 1
    It's not clear to me what you are trying to achieve. What do you mean the sole method would be a bottleneck? Are you concerned with being CPU-bound and unable to take care of parallelism, or is this networked among multiple machines? If it's just within a single application using multiple method calls or multiple objects makes no difference and you're just creating more work for the system. But dealing with parallelism, multiple machines, and networking are all very different use cases.
    – BrianH
    Sep 22, 2016 at 20:29
  • On the surface of it, its not possible to 'split the load' when all the work is done on the same computer
    – Ewan
    Sep 22, 2016 at 20:50
  • @Ewan: Most computers nowadays have multiple cores, so it's certainly possible. Sep 22, 2016 at 21:25
  • Oh yes, but "... To another method... Or object.."? I think the questioner is confused rather than asking about multithreading
    – Ewan
    Sep 23, 2016 at 2:44
  • @Ewan I just want to make some kind of imitation - I don't have any particular issues with the current implementation - all clients are in separate threads + one singleton. I just want to make that singleton imitate real-life server with load balancing.
    – lapots
    Sep 23, 2016 at 6:30

1 Answer 1

1

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
  • Well, it seems close to what I want. Though I am planning to extend number of message instances depeding on the load. I get it.
    – lapots
    Sep 23, 2016 at 11:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.