loadavg와 CPU 코어 수 간의 비율을 대충 체크해서 throttle 하는 방법.

const os = require('os');
 
const CPU_COUNT = os.cpus().length;
 
module.exports = {
    waitFor: function(thresholdPercentage){
        const self = this;
 
        return new Promise((resolved)=>{
            if (self.isOK(thresholdPercentage)) return resolved(true);
 
            setInterval(()=>{
                if (self.isOK(thresholdPercentage)) return resolved(true);
            }, 100);
        });
    },
 
    isOK: function(thresholdPercentage){
        return thresholdPercentage > ((os.loadavg()[0] / CPU_COUNT) * 100);
    }
};

 

위와 같은 loadavg.js 라는 파일이 있을 때 아래처럼 사용가능.

const LoadAvg = require('./loadavg.js');
 
 
(async()=>{
    // CPU 사용률이 10% 이하로 내려갈 때까지 대기
    await LoadAvg.waitFor(10);
 
    // 사소한 일들을 수행...
    ...
    ...
})();

 

 

 

 

 

Posted by bloodguy
,