역압

Backpressure

소비자가 느려지면 생산자에게 속도를 낮추라는 신호를 보냅니다.

···
html
<div class="v"><header><b>BACKPRESSURE</b><span id="state"></span></header><main><div class="meter"><div class="fill" id="fill"></div></div><div class="rates" id="rates"></div></main><footer>demand controls the source</footer></div>
css
.v{width:min(92vw,660px);height:min(86vh,310px);box-sizing:border-box;padding:clamp(9px,2.5vmin,18px);border:1px solid var(--line);border-radius:14px;background:var(--surface);display:flex;flex-direction:column;gap:7px;color:var(--fg);font:500 clamp(15px,3.2vmin,17px)/1.25 var(--font-sans, sans-serif)}.v header,.v footer{display:flex;justify-content:space-between;align-items:center;gap:8px;white-space:nowrap}.v header b{color:var(--accent);font-size:.9em}.v header span,.v footer{color:var(--muted);font-size:.82em}.v main{flex:1;min-height:0;position:relative;overflow:hidden}.v .mono{font-family:ui-monospace,SFMono-Regular,Consolas,monospace}.v .active{background:var(--accent)!important;color:var(--bg)!important;border-color:var(--accent)!important}.v .muted{opacity:.28} .meter{height:42%;margin-top:12px;border:1px solid var(--line);border-radius:7px;overflow:hidden}.fill{height:100%;background:var(--accent);transition:width .35s}.rates{height:38%;display:flex;align-items:center;justify-content:space-around;color:var(--accent);font:600 .85em ui-monospace,monospace}
js
let n=0;const fill=[20,48,80,95,66,35];function draw(){document.getElementById("fill").style.width=fill[n]+"%";document.getElementById("rates").textContent=n>=3?"producer  ↓  |  consumer →":"producer →  |  consumer →";document.getElementById("state").textContent=n>=3?"slow producer":"buffer filling";n=(n+1)%fill.length}draw();setInterval(draw,850)

역압은 소비 속도보다 생산 속도가 빠를 때 버퍼가 끝없이 커지지 않게 만드는 제어입니다. 소비자가 처리 가능한 만큼만 새 데이터를 요청하거나, 쓰기 완료 신호까지 생산을 잠시 멈춥니다.

버퍼 크기만 키우면 짧은 폭주는 흡수할 수 있어도 지속적인 속도 차이는 해결되지 않습니다. 데모는 버퍼가 차면서 생산 속도가 내려가는 상태를 보여줍니다.

언제 쓰나

스트림, 큐, 업로드 파이프라인의 메모리와 지연을 제어할 때 씁니다.

페이지로 열기 ↗