标签: reactive-programming

Spring WebFlux 时不时会死机

我有一个Spring Boot WebFluxI/O 密集型服务并调用外部资源。

通常情况下,一切正常。但有时,我注意到该服务看起来像是冻结/冬眠。这意味着它可以在通常需要几毫秒的两行代码之间空闲几分钟。

这通常与 I/O 操作相关。例如,从资源 A 获取响应,然后调用资源 B。

我们注意到有时这些时间间隔为 10 分钟 - 就好像服务没有可用线程来处理请求或者它正在某处排队。

这种冻结可能与我们调用的外部服务响应缓慢有关。我们期望 WebFlux 能够毫无问题地处理它,但外部资源的缓慢似乎正在影响我们的服务。

伪正常场景:

10:00:00  - call external service A (responses within 2 seconds)
10:00:02 - getting a response from service A 
10:00:02:10 - calling service B  after 10 Milliseconds
Run Code Online (Sandbox Code Playgroud)

伪故障场景:

10:00  - call external service A (responses are getting slower, say to 1 minute)
10:01 - getting a response from service A
10:11 - calling service B  <----- why did …
Run Code Online (Sandbox Code Playgroud)

reactive-programming spring-boot spring-webflux

5
推荐指数
0
解决办法
817
查看次数

Mono.fromCallable 和 Mono.justOrEmpty 的区别

下面两个执行有什么不同?

Mono.justOrEmpty(someFunction())
  .doOnNext(() -> doSomeTask()).subscribe();

Mono.fromCallable(() -> someFunction())
  .doOnNext(() -> doSomeTask()).subscribe();
Run Code Online (Sandbox Code Playgroud)

java reactive-programming project-reactor spring-webflux

5
推荐指数
2
解决办法
1万
查看次数

等待所有 Mono 响应从下游 API 返回的正确方法是什么

我对 Mono 和 Flux 还很陌生。我正在尝试加入几个下游 API 响应。这是一个传统的阻塞应用程序。我不想收集 Mono 列表,我想要从下游 API 返回的有效负载列表,我从 Mono 获取该列表。然而,返回到控制器的“结果”有时只有一些或没有下游 API 响应。这样做的正确方法是什么?我读过几篇文章如何迭代 Flux 并与 Mono状态混合

你不应该在网络应用程序的任何地方调用 subscribe 。如果这绑定到 HTTP 请求,那么您基本上会触发反应式管道,而无法保证资源或完成情况。调用 subscribe 会触发管道,但不会等到它完成

我应该使用 CompletableFuture 吗?

在我的服务中我尝试过

var result = new ArrayList<List<>>();
List<Mono<X>> monoList = apiCall();
Flux.fromIterable(monoList)
            .flatMap(m -> m.doOnSuccess(
                        x -> {  
                            result.add(x.getData());
                        }
        )).subscribe();
Run Code Online (Sandbox Code Playgroud)

我还在控制器中尝试了以下操作,但该方法返回而不等待订阅完成

var result = new ArrayList<List<X>>();
        Flux.concat(
                this.service.callApis(result, ...)
        ).subscribe();
        return result;
Run Code Online (Sandbox Code Playgroud)

在我的服务中

public Mono<Void> callApis(List<List<x>> result, ..) {
...
return Flux.fromIterable(monoList)
                .flatMap(m -> m.doOnSuccess(
                        x -> {  
                            result.add(x.getData()...);
                        } …
Run Code Online (Sandbox Code Playgroud)

java reactive-programming project-reactor spring-webflux

5
推荐指数
1
解决办法
4518
查看次数

Spring Reactor:以非阻塞方式添加延迟

关于如何在方法中添加延迟但以非阻塞方式添加延迟的小问题。

模拟长流程的一种非常流行的方法是使用Thread.sleep(); 但是,对于Reactor项目来说,这是一个阻塞操作。众所周知,在反应式项目中,我们不应该阻塞。

我想实验和模拟长过程。某种方法将花费大量时间,但以非阻塞方式,无需交换线程。这是为了模拟一个非常冗长的方法,但经过 BlockHound 等证明是非阻塞的。

这种结构非常流行:

@Test
    public void simulateLengthyProcessingOperationReactor() {
        Flux.range(1,5000)
                .map(a -> simulateLengthyProcessingOperation(a))
                .subscribe(System.out::println);
    }

    public String simulateLengthyProcessingOperation(Integer input) {
        simulateDelayBLOCKING();
        return String.format("[%d] on thread [%s] at time [%s]", input, Thread.currentThread().getName(), new Date());
    }

    public void simulateDelayBLOCKING() {
        try {
            Thread.sleep(4000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
Run Code Online (Sandbox Code Playgroud)

但它正在阻塞。(我知道有,Mono.fromCallable(() ->但这不是问题)

是否可以做同样的事情,模拟延迟,但非阻塞?另外,.delay不会达到预期的结果(在同一反应管道上模拟非阻塞的冗长方法)

@Test
    public void simulateLengthyProcessingOperationReactor() {
        Flux.range(1,5000)
                .map(a -> simulateLengthyProcessingOperation(a))
                .subscribe(System.out::println);
    }

    public String simulateLengthyProcessingOperation(Integer input) {
        simulateDelay_NON_blocking();
        return String.format("[%d] on …
Run Code Online (Sandbox Code Playgroud)

java spring reactive-programming project-reactor spring-webflux

5
推荐指数
1
解决办法
2387
查看次数

R2DBC 是否支持 SQL Server 连接的“ApplicationIntent=ReadOnly”参数?

我正在开发一个利用 R2DBC 库进行反应式数据库访问的项目。但是,我不确定 R2DBC 是否支持ApplicationIntent=ReadOnlySQL Server 连接的参数。此参数通常用在 Microsoft SQL Server 连接字符串中,指示应用程序仅从数据库中读取,从而允许 SQL Server 将连接重定向到只读副本(如果可用)。

我查看了 R2DBC 官方文档,但找不到有关此参数的任何具体信息。我想知道是否有人有在 SQL Server 中使用 R2DBC 的经验,并且可以提供有关 R2DBC 是否支持"ApplicationIntent=ReadOnly"或是否有任何解决方法来实现类似行为的见解。

如果 R2DBC 不直接支持此参数,我希望您能提供有关替代方法的建议,以确保 R2DBC 与 SQL Server 的连接被视为只读,例如在数据库级别配置 SQL Server 连接。

sql-server database-connection reactive-programming r2dbc r2dbc-mssql

5
推荐指数
1
解决办法
304
查看次数

如何在反应函数内保留程序反应性?

我正在开发一个基于 python 的闪亮应用程序,用于通过串行传输线驱动流体泵。配置具有设定幅度和运行时间的流量曲线后,可以通过按操作按钮“p1”启动串行传输。

我面临的问题是与按钮“p1”相关的内部缺乏反应性reactive.event(input.p1):我想确保通过单击“p1”开始传输,从而触发可以reactive.event(input.p1)随时通过单击“p2”终止传输。

然而,当前的实现导致停止消息 inreactive.event(input.p2)排队并在传输reactive.event(input.p1)结束后发送。

我怎样才能解决这个问题?有什么方法可以确保我的程序仍然对其他输入做出反应?我希望单击“p2”后立即停止传输。两个按钮的实现都放在下面。

@reactive.Effect
@reactive.event(input.p1)
def _():
    y = yg.get() # fetch np.array y from reactive value yg, numbers in y correspond to driving voltages

     for e in y: # iterate over array
         msg = "1:1:"+str(e)+":100" # formatted string containing the driving voltage
         #print(msg) # print to console in debug mode
         ser.write(bytes(msg,'utf-8')) # send the formatted string
         t0 = time.time() # time stamp
        
         while(((time.time()-t0)<=2)): # next driving voltage …
Run Code Online (Sandbox Code Playgroud)

python reactive-programming py-shiny

5
推荐指数
1
解决办法
191
查看次数

在dispatch_queue sendComplete上创建的RACSubject不会使其合并信号

在DISPATCH_QUEUE_PRIORITY_DEFAULT gcd队列中运行的dispatch_async块中:我创建两个RACSubject对象,使用RACSignal merge:然后订阅完成.然后,出于此测试的目的(并在我的实际代码中复制场景),我在它们上发送sendComplete.合并的信号完成订阅永远不会触发.我独立地为这些主题附加了两个完成订阅,那些就是开火.如果我在主线程而不是gcd队列上执行相同的测试,那么它将按预期工作.

有没有办法使这项工作或我将不得不重构以获得主线程上的所有主题?

#import <ReactiveCocoa/ReactiveCocoa.h>

@interface rac_signal_testTests: SenTestCase
@end

@implementation rac_signal_testTests

- (void)setUp
{
    [super setUp];

    // Set-up code here.
}

- (void)tearDown
{
    // Tear-down code here.

    [super tearDown];
}

-(void)test_merged_subjects_will_complete_on_main_thread{
    RACSubject *subject1 = [[RACSubject subject] setNameWithFormat:@"subject1"];
    RACSubject *subject2 = [[RACSubject subject] setNameWithFormat:@"subject2"];

    RACSignal *merged = [RACSignal merge:@[subject1, subject2]];

    __block BOOL completed_fired = NO;

    [merged subscribeCompleted:^{
        completed_fired = YES;
    }];

    [subject1 sendNext:@"1"];
    [subject2 sendNext:@"2"];

    [subject1 sendCompleted];
    [subject2 sendCompleted];

    STAssertTrue(completed_fired, nil);
}

//test proving that throttling isn't breaking the …
Run Code Online (Sandbox Code Playgroud)

objective-c frp reactive-programming reactive-cocoa

4
推荐指数
1
解决办法
1195
查看次数

使用Reactive Cocoa RACSignal启用UIButton

我UIButton添加了一个视图.我的观点还有三个文本框即.用户名,密码和confirmPassword.根据这些文本框的合法内容,我需要启用我的signUp按钮.

这是我的代码片段: -

    UIButton *signUp = [[UIButton alloc]initWithFrame:CGRectMake(10, 100, 50, 20)];
    signUp.backgroundColor = [UIColor greenColor];

    signUp.enabled = NO ;
    [self.view addSubview:signUp];

    RACSignal *formValid = [RACSignal
    combineLatest:@[
    username.rac_textSignal,
    password.rac_textSignal,
    confirmPassword.rac_textSignal
    ]
    reduce:^(NSString *username, NSString *password, NSString *passwordVerification)        {
    return @([username length] > 0 && [password length] > 8 && [password      isEqual:passwordVerification]);
    }];

    RAC(signUp.enabled) = formValid; //Error is here
Run Code Online (Sandbox Code Playgroud)

在最后一行,我遇到两个错误: -

  1. ARC不允许将'BOOL'(又名'signed char')隐式转换为'id'
  2. 预期的标识符

我是Reactive Cocoa的新手.请忽略错误.

objective-c uibutton reactive-programming ios reactive-cocoa

4
推荐指数
2
解决办法
1638
查看次数

如何在没有主题的情况下利用被动扩展来进行缓存?

我希望能够从外部Api获取特定请求的数据,但是当返回该数据时,也使其在缓存中可用,以表示应用程序的当前状态.

这个解决方案似乎有效:

var Rx = require('rx');

var cached_todos = new Rx.ReplaySubject(1);

var api = {
  refresh_and_get_todos: function() {
    var fetch_todos = Rx.Observable.fromCallback($.get('example.com/todos'));
    return fetch_todos()
      .tap(todos => cached_todos.onNext(todos));
  },
  current_todos: function() {
    return cached_todos;
  }
};
Run Code Online (Sandbox Code Playgroud)

但是 - 显然Subjects在Rx中是不好的做法,因为它们并不真正遵循功能性反应式编程.

以功能性反应式编程方式执行此操作的正确方法是什么?

reactive-programming rxjs

4
推荐指数
1
解决办法
2208
查看次数

RxJava - 如何获取列表的第一个元素并将其作为Observable返回

让我们来看看这个:

Observable<List<UserProfile>> findUser =service.getUserProfiles()
Run Code Online (Sandbox Code Playgroud)

如何对其进行转换,使其返回第一个元素作为Observable(而不是仅包含第一个元素的Observable列表).我试过first()和takeFirst(),但它仍然会返回一个列表.

reactive-programming rx-java

4
推荐指数
1
解决办法
5406
查看次数