반응형

- 폴링이란? 

통신에서, "폴링"은 한 프로그램이나 장치에서 다른 프로그램이나 장치들이 어떤 상태에 있는지를 지속적으로 체크하는 전송제어 방식으로서, 대체로 그들이 아직도 접속되어 있는 지와 데이터 전송을 원하는지 등을 확인한다.

쉽게 말해서 Status Bar, Dash Board 등에 쓰이는 실시간처리 방식이다.

환경은 다양할 수 있다. 예를 들면 JSP 에서 response.setIntHeader를 사용하여 Auto Refresh 처리를 해주게 되면 이역시 폴링이다. 하지만 누가 요즘에 Refresh 방식의 폴링을 선호할까.. 아래는 Jquery 의 Ajax 기법을 통한 Polling 예제이다.

Traditional Polling

The setInterval Technique
setInterval(function(){
    $.ajax({ url: "server", success: function(data){
        //Update your dashboard gauge
        salesGauge.setValue(data.value);
    }, dataType: "json"});
}, 30000);

위의 소스는 전통적인 폴링 기법중  setInterval 을 사용한 기법으로 매 30초간 정해놓은 이벤트를 출력한다. 하지만 서버의 응답이 30초 보다도 더 느려질경우엔? 

어찌보면 이 기법은 도박이다. 반응성이나 사양이 좋지않은 서버나 네트워크 전체의 호스트를 고려한다면.. 이러한 할당된 시간에 리턴값이 오지않는 위험은 사전에 예방되어야한다. 최악의 경우 당신은 같은 호출안에서 Ajax 큐의 배열이 다 뻗어버릴 수 있다. Anyway,  별로 좋지않다.


The setTimeout Technique

(function poll(){
   setTimeout(function(){
      $.ajax({ url: "server", success: function(data){
        //Update your dashboard gauge
        salesGauge.setValue(data.value);

        //Setup the next poll recursively
        poll();
      }, dataType: "json"});
  }, 30000);
})();

이 기법은 settimeout을 통한 재귀호출방식인데 좋은점은 시간의 사이마다 무슨일이 일어났는지 파악하기 쉽다는점이다. 음 장단점은 있기 마련인데 Manual을 봐도 이해하기가 힘들다. 아래의 원문으로 읽으면 좋을것같다.

As you can see, jQuery's Ajax call can take as long as it wants to. So, this pattern doesn't guarantee execution on a fixed interval per se. But, it doesn't guarantee that the previous interval has completed before the next interval is called.


두가지 방식다 단점이 있고 감수해야하는 위험이 있다. 그래서 결과적으로 우리가 도입해야하는건 Long Polling 기법. 사실 위의 내용은 그닥 필요없고 이것만 보고 도입하면 된다.

Long Polling - An Efficient Server-Push Technique

(function poll(){
    $.ajax({ url: "server", success: function(data){
        //Update your dashboard gauge
        salesGauge.setValue(data.value);

    }, dataType: "json", complete: poll, timeout: 30000 });
})();

 데이터베이스 처리중 커넥션 풀 기법이 있는데 비슷한 개념이다. 서버와 접속을 열어둔 상태에서 요청이 오면 바로 바로 처리한다. 요청이 없을시에는 리소스를 거의 먹지않는다. 아래의 원문을 보면 이해가 쉬울 것 같다.

Applications built with Long Polling in mind attempt to offer realtime interaction, using a persistent or long-lasting HTTP connection between the server and the client.

The self executing JavaScript poll function combined jQuery Ajax's repeating timeout interval means this is a very efficient server-push technique.


정리를 했지만 쉽지않다. 쉽게 이해하면 일반 커넥션과 커넥션 풀의 차이? 그리고 Jquery Ajax 경우 Long Polling에 최적화되어 있다.


출처 : http://tkjeon.tistory.com/6

반응형

'코드자료실 > javascript' 카테고리의 다른 글

[jquery]onload 시점  (0) 2014.09.29
[jquery] ie7, ie6 json is undefined  (0) 2013.03.13
[jquery] jsonp 사용  (0) 2013.01.24
[jquery] jquery ui tabs  (0) 2013.01.16
[자바스크립트] 문자>수치, 수치>문자 변환  (0) 2012.12.23
블로그 이미지

cocy

조금은 가볍게! 시작은 새롭게!

,