SpringWebflux
[SpringWebflux] reactor operator - 4
jolocal
2024. 3. 8. 15:21
728x90
Backpressure 관련 연산자
- delaySequence / limitRate
- sample
delaySequence
주어진 duration 만큼 지연시킵니다.
limitRate()
backpressure에 request를 조절하는 기능입니다.
limitRate()에서 적힌 수 많은 request를 강제합니다. 즉 request를 2번만 요청할 수 있습니다.
TestCode
public class Operator4 {
// limit
public Flux<Integer> fluxDelayAndLimit(){
return Flux.range(1,10)
.delaySequence(Duration.ofSeconds(1))
.log()
.limitRate(2);
}
}
class Operator4Test {
private Operator4 operator4 = new Operator4();
@Test
void fluxDelayAndLimit() {
StepVerifier.create(operator4.fluxDelayAndLimit())
.expectNext(1,2,3,4,5,6,7,8,9,10)
.verifyComplete();
}
}
sample
선별적으로 데이터를 Sampling합니다.
TestCode
public class Operator4 {
// sample
public Flux<Integer> fluxSample(){
return Flux.range(1,100)
.delayElements(Duration.ofMillis(100))
.sample(Duration.ofMillis(300))
.log();
}
}
class Operator4Test {
private Operator4 operator4 = new Operator4();
@Test
void fluxSample() {
StepVerifier.create(operator4.fluxSample())
.expectNextCount(1000)
.verifyComplete();
}
}
왼쪽의 테스트 결과를 보면 숫자가 순차적으로 나오긴 하지만, 사이사이가 비어있는것을 확인할 수 있습니다.
이는 sampling을 통해 데이터를 선별적으로 전달받을 수 있음을 의미합니다.
728x90