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?

4

1 回答 1

1

这看起来不是一个好的策略。基本上,当您在控制器内部时,已经为您分配了一个线程:通过调用 sleep() 您当前持有这样的线程,防止其他请求得到服务并限制整个服务器可以执行的并发请求的数量。sleep() 调用还有一个缺点是不释放任何持有的锁,所以这是一个潜在的并发问题。

目前,我还在为我的 REST API 寻找一个不错的 QoS 解决方案,不幸的是我无法为您提供明确的更好的答案,但如果您的服务真的很安静,您可能可以使用加载了 mod_qos 等模块的 Apache 代理(http ://opensource.adnovum.ch/mod_qos/ )

于 2013-10-22T20:29:09.700 回答