Reactive Programming with RxJava-Chapter5:Reactive from Top to Bottom(1)

Beating the C10k Problem

Traditional Thread-Based HTTP Servers

Single threaded server

Traditional applications are inherently limited to a couple thousand connections,even built on top of modern servlet containers

Nonblocking HTTP Server with Netty and RxNetty

Netty is entirely event-driven;we never block waiting for data to be sent ir received.Instead,raw bytes in the form of ByteBuf instances are pushed to our processing pipeline.Whenever a few bytes arrive to our application,Netty will notify our handler.Whenever we send few bytes,we get a ChannelFuture without blocking.

Netty uses just a handful of threads to process possibly thousands of connections.We do not keep any heavyweight data structures or threads per each connection.

Observable server with RxNetty

HttpServer
        .newServer(8080)
        .start((req,resp)) -> {
            String amountStr = req.getDecodedPath().substring(1);
            BigDecimal amount = new BigDecimal(amountStr);
            Observable<String> response = Observable
                    .just(amount)
                    .map(eur -> eur.multiply(RATE))
                    .map(usd ->
                            "{\"EUR\": " + amount +"," +
                            "\"USD\": " + isd + "}");
            return resp.writeString(response);
        })
        .awaitShutdown(); 

This implementation can easily withstand thousands of concurrent connections,and vertical scalability is limited only by the amount of traffic it must handle,not the number of more-or-less idle connections.

Reactive HTTP Servers Tour

Http Client Code

A TCP/IP connection is actually quite lightweight.The operating system must keep a socket descriptor for each open connection (around one kb) and that is pretty much it.When a packet (messages) arrives,the kernel dispatches it to the appropriate process,like JVM.One kb is quite a small memory footprint compared to the roughly one mb consumed by the stack of each thread blocked on a socket.

Nonblocking HTTP Client with RxNetty

RxNetty goes a bit futrher compared to other nonblocking HTTP clients and does not simply notify us when the entrie response arrives.Instead,we get a stream of ByteBuf messages,optionally followed by Observable completion when the server decides to drop the connection.

最后,安利一款自己写的基于MVP的Android开发框架
https://github.com/sxenon/Pure
欢迎大家拍砖,如果觉得好,麻烦多多star

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值