0

My RESTful Spring based Web Service receives differentiated user requests, say Gold, Silver and Bronze, where Gold requests have maximum priority, Bronze lowest one. So I want to implement some simple type of differentiated service provisioning. Which could be the simplest (I would say almost mocked) strategy to implement?

I'm thinking about blocking less priority for some amount of time if i'm serving more priority one. Something like this

@Controller
public class MyController {
    @Autowired
    private MyBusinessLogic businessLogic;

    private static final int GOLD=0;
    private static final int SILVER=1;
    private static final int BRONZE=2;
    private volatile int [] count = new int[3];

    @RequestMapping
    public String service(@RequestBody MyRequest request) {
        count[request.getType()]++;
        for(int i=0; i<request.getType(); i++)
            if(count[i]>0)
                Thread.sleep(500);
        String result = businessLogic.service(request);
        count[request.getType()]--;
        return result;
    }
}

Is it reasonable? Or has it some undesirable side-effect? Do you recommend a better strategy?

user1781028
  • 1,478
  • 4
  • 22
  • 45

1 Answers1

1

It does not look like a good strategy. Basically when you are inside the Controller a Thread has already been allocated to you: with a call to sleep() you are currently holding such thread, preventing other requests to be served and limiting the number of concurrent requests that overall your server can execute. The sleep() call has also the drawback to NOT release any lock being held, so it's a potential concurrency problem.

At the moment I am also looking for a decent QoS solution for my REST APIs and unfortunately I cannot provide you a definitive better answer, but if your services are really restful you can probably work with an Apache proxy loaded with a module like mod_qos (http://opensource.adnovum.ch/mod_qos/)

Bruno Bossola
  • 1,128
  • 1
  • 13
  • 24