SlideShare uma empresa Scribd logo
1 de 31
Baixar para ler offline
Netty
List("Asynchronous", "data", "transfer")
      ::: List("practical", "aspects")




                            Victor Cherkassky
                    "vcherkassky@gmail.com"
What is Netty
         IO library



        IO
cat Files > Networking
What is Netty
                   IO library
                   asynchronous

    ChannelFuture future = channel.write(message)
future.addListener(WRITE_COMPLETE_LISTENER)
What is Netty
                          IO library
                          asynchronous
                          non-blocking
      /**
        * This method blocks until input data is available
        */
           InputStream.read()
What is Netty
         IO library
         asynchronous
         non-blocking
         multi-protocol

HTTP | WebSocket | Protobuf
         | Binary
     TCP | UDP
blocking vs non-blocking
              blocking (OIO)
Ipttemi =nwFlIpttem"nu.i";
 nuSra s  e ienuSra(iptbn)
itbt =i.ed) / crettra wisfrrsl o err
 n ye  sra(; / urn hed at o eut r ro




            non-blocking (NIO)
wie(re {
 hl tu)
  slco.eet) / rqetn eet fo mlil canl
   eetrslc(; / eusig vns rm utpe hnes
  Ieao i =slco.eetdes)ieao(;
   trtr t  eetrslceKy(.trtr)
  wie(thset) {
   hl i.aNx()
    SlcoKyky=(eetoKy i.et)
    eetre e   Slcine) tnx(;
    hnlKyky;
    adee(e)
    i.eoe)
    trmv(;
  }
}
blocking vs non-blocking
   Hardware blocking write
blocking vs non-blocking
Hardware non-blocking write: DMA
blocking vs non-blocking
                     OS
select()/poll() - traditional POSIX polling O(n)
epoll() - event-based polling O(1), Linux 2.5.44+
kqueue - FreeBSD, Mac OS X
I/O Completion Ports - Windows NT 3.5+, Solaris 10+
blocking vs non-blocking
                        Java
SelectableChannel.register(selector) - blocking operation,
registers an "interest" for IO events
Selector.select - non-blocking operation, checks for
occurred events
blocking vs non-blocking

            Java                            OS
                                     select() / poll() / kqueue
Channel.register & Selector.select   I/O Completion Ports
                                     epoll() - Java 1.6+
Thread model

    Tomcat
 1 request = 1 thread
fast for ~1000 clients
  too much memory
Thread model

    Node.js
all requests = 1 thread
     super scalable
limited error handling
Thread model

       Netty
many requests = 1 thread
    flexible model
Netty web Server
Netty web Client
Channel Pipeline
All terms together
Channel (socket read/write)
channel Buffer (byte array)
channel Event (message)
channel Pipeline (message bus)
channel Handler (message handler)
  Upstream Event (from socket)
  Downstream Event (to socket)
Creating an HTTP server
        Set up thread pools
   boss pool for handling incoming connections
   worker pool for processing IO

piaefnlEeuo bsPo =Eeuosnwahdhedol)
 rvt ia xctr osol  xctr.eCceTraPo(
;
piaefnlEeuo wrePo =Eeuosnwahdhedol
 rvt ia xctr okrol  xctr.eCceTraPo
(;
 )
Creating an HTTP server
    Provide ChannelFactory
piaefnlCanlatr canlatr =
 rvt ia hneFcoy hneFcoy
   nwNoevrokthneFcoybsPo,wrePo)
   e iSreSceCanlatr(osol okrol;
Creating an HTTP server
         Set up PipelineFactory
piaefnlCanlieieatr pplnFcoy=
 rvt ia hnePplnFcoy ieieatr
   nwCanlieieatr( {
   e hnePplnFcoy)
  @vrie
  Oerd
  pbi Canlieiegtieie)trw Ecpin{
  ulc hnePpln ePpln( hos xeto

      Canlieiep=Canl.ieie)
      hnePpln    hnesppln(;
      padat"tpevroe" nwHtSreCdc);
      .dLs(htSreCdc, e tpevroe()

      padat"ade" nwCsoRqetade(lCanl);
      .dLs(hnlr, e utmeusHnlralhnes)

      rtr p
      eun ;
  }
};
Creating an HTTP server
 Put all together with Bootstrap
piaefnlSreBosrpbosrp=nwSreBosrpca
 rvt ia evrotta otta e evrotta(h
neFcoy;
 nlatr)

 ...

bosrpstieieatr(ieieatr)
 otta.ePplnFcoypplnFcoy;

/ Otosfraprn canl-i acpscnetos
 / pin o aet hne   t cet oncin
bosrpstpin"oaAdes,nwIeSceAdespr);
 otta.eOto(lcldrs" e ntoktdrs(ot)
bosrpstpin"esAdes,tu)
 otta.eOto(ruedrs" re;

/ Otosfriscide -te poesI
 / pin o t hlrn hy rcs O
bosrpstpin"hl.cNDly,tu)
 otta.eOto(cidtpoea" re;
bosrpstpin"hl.eplv" fle;
 otta.eOto(cidkeAie, as)
Creating an HTTP server
    Writing a custom handler
casCsoHnlretnsSmlCanlptemade {
 ls utmade xed ipehneUsraHnlr
   ...
  @vrie
   Oerd
  pbi vi msaeeevdCanladeCnetcx Msae
   ulc od esgRcie(hneHnlrotx t, esgE
vn e trw Ecpin{
 et ) hos xeto
    HtRqetrq=(tpeus)egtesg(;
     tpeus e   HtRqet .eMsae)
    / pouersos adwiei t acanl
     / rdc epne n rt t o hne
    HtRsos rs=nwDfuttpepneHT__,O)
     tpepne e   e ealHtRsos(TP11 K;
    Canluueftr =Canl.rt(t.eCanl) rs;
     hneFtr uue   hneswiecxgthne(, e)
    ftr.dLsee(hneFtrLsee.LS)
     uueaditnrCanluueitnrCOE;
  }
}
Creating an HTTP server
Gather channels for future closing
piaefnlCanlru alhnes=nwDfuthneGop
 rvt ia hneGop lCanl    e ealCanlru(
)
    ...
/ aanisd orcso hnlr
 / gi nie u utm ade
casCsoHnlretnsSmlCanlptemade {
 ls utmade xed ipehneUsraHnlr
    ...
    @vrie
    Oerd
  pbi vi canlpnCanladeCnetcx Canltt
   ulc od hneOe(hneHnlrotx t, hneSae
Eete trw Ecpin{
 vn ) hos xeto
    Canlcanl=egthne(;
    hne hne   .eCanl)
    alhnesadcanl;
    lCanl.d(hne)
  }
}
Creating an HTTP server
        Release resources
         close gathered channels
         release external resources

alhnescoe)aatnnerpil(;
 lCanl.ls(.wiUitrutby)
bosrprlaexenleore(;
 otta.eesEtraRsucs)
TCPnodelay, TCPkeepalive
  Remember that bootstrap code?

 / Otosfriscide -te poesI
  / pin o t hlrn hy rcs O
 bosrpstpin"hl.cNDly,tu)
  otta.eOto(cidtpoea" re;
 bosrpstpin"hl.eplv" fle;
  otta.eOto(cidkeAie, as)




   What do these options mean?
TCP keepAlive

prolongs each TCP connection
makes NAT router happy
TCP noDelay
   Nagle's algorithm

glues up small TCP packets
tries to reduce bandwidth
increases latency
TCPnodelay, TCPkeepalive

    To use,
 or not to use?
Don't ask,
measure!
Thank you
Additional resources
    netty @ github
    introductory article

Mais conteúdo relacionado

Mais procurados

Connecting to the network
Connecting to the networkConnecting to the network
Connecting to the network
Mu Chun Wang
 
Streams are Awesome - (Node.js) TimesOpen Sep 2012
Streams are Awesome - (Node.js) TimesOpen Sep 2012 Streams are Awesome - (Node.js) TimesOpen Sep 2012
Streams are Awesome - (Node.js) TimesOpen Sep 2012
Tom Croucher
 
Twisted logic
Twisted logicTwisted logic
Twisted logic
ashfall
 
Iss letcure 7_8
Iss letcure 7_8Iss letcure 7_8
Iss letcure 7_8
Ali Habeeb
 
Chatting dengan beberapa pc laptop
Chatting dengan beberapa pc laptopChatting dengan beberapa pc laptop
Chatting dengan beberapa pc laptop
yayaria
 

Mais procurados (20)

Connecting to the network
Connecting to the networkConnecting to the network
Connecting to the network
 
Building scalable network applications with Netty (as presented on NLJUG JFal...
Building scalable network applications with Netty (as presented on NLJUG JFal...Building scalable network applications with Netty (as presented on NLJUG JFal...
Building scalable network applications with Netty (as presented on NLJUG JFal...
 
Developing High Performance Application with Aerospike & Go
Developing High Performance Application with Aerospike & GoDeveloping High Performance Application with Aerospike & Go
Developing High Performance Application with Aerospike & Go
 
无锁编程
无锁编程无锁编程
无锁编程
 
Socket.io v.0.8.3
Socket.io v.0.8.3Socket.io v.0.8.3
Socket.io v.0.8.3
 
Taking advantage of Prometheus relabeling
Taking advantage of Prometheus relabelingTaking advantage of Prometheus relabeling
Taking advantage of Prometheus relabeling
 
Python twisted
Python twistedPython twisted
Python twisted
 
Briefly Rust - Daniele Esposti - Codemotion Rome 2017
Briefly Rust - Daniele Esposti - Codemotion Rome 2017Briefly Rust - Daniele Esposti - Codemotion Rome 2017
Briefly Rust - Daniele Esposti - Codemotion Rome 2017
 
Pemrograman Jaringan
Pemrograman JaringanPemrograman Jaringan
Pemrograman Jaringan
 
Counter Wars (JEEConf 2016)
Counter Wars (JEEConf 2016)Counter Wars (JEEConf 2016)
Counter Wars (JEEConf 2016)
 
How to Leverage Go for Your Networking Needs
How to Leverage Go for Your Networking NeedsHow to Leverage Go for Your Networking Needs
How to Leverage Go for Your Networking Needs
 
Streams are Awesome - (Node.js) TimesOpen Sep 2012
Streams are Awesome - (Node.js) TimesOpen Sep 2012 Streams are Awesome - (Node.js) TimesOpen Sep 2012
Streams are Awesome - (Node.js) TimesOpen Sep 2012
 
Twisted logic
Twisted logicTwisted logic
Twisted logic
 
Async await in C++
Async await in C++Async await in C++
Async await in C++
 
Iss letcure 7_8
Iss letcure 7_8Iss letcure 7_8
Iss letcure 7_8
 
C#을 이용한 task 병렬화와 비동기 패턴
C#을 이용한 task 병렬화와 비동기 패턴C#을 이용한 task 병렬화와 비동기 패턴
C#을 이용한 task 병렬화와 비동기 패턴
 
Nessus scan report using microsoft patchs scan policy - Tareq Hanaysha
Nessus scan report using microsoft patchs scan policy - Tareq HanayshaNessus scan report using microsoft patchs scan policy - Tareq Hanaysha
Nessus scan report using microsoft patchs scan policy - Tareq Hanaysha
 
The future of async i/o in Python
The future of async i/o in PythonThe future of async i/o in Python
The future of async i/o in Python
 
Chatting dengan beberapa pc laptop
Chatting dengan beberapa pc laptopChatting dengan beberapa pc laptop
Chatting dengan beberapa pc laptop
 
Improving rpc bkp
Improving rpc bkpImproving rpc bkp
Improving rpc bkp
 

Destaque

Zero-Copy Event-Driven Servers with Netty
Zero-Copy Event-Driven Servers with NettyZero-Copy Event-Driven Servers with Netty
Zero-Copy Event-Driven Servers with Netty
Daniel Bimschas
 
Introduction of netty
Introduction of nettyIntroduction of netty
Introduction of netty
Bing Luo
 
Real time analytics with Netty, Storm, Kafka
Real time analytics with Netty, Storm, KafkaReal time analytics with Netty, Storm, Kafka
Real time analytics with Netty, Storm, Kafka
Trieu Nguyen
 
深入浅出Netty l.t
深入浅出Netty   l.t深入浅出Netty   l.t
深入浅出Netty l.t
oleone
 
Nettyらへん
NettyらへんNettyらへん
Nettyらへん
Go Tanaka
 
Netty 4-based RPC System Development
Netty 4-based RPC System DevelopmentNetty 4-based RPC System Development
Netty 4-based RPC System Development
Allan Huang
 
Xitrum internals
Xitrum internalsXitrum internals
Xitrum internals
Ngoc Dao
 

Destaque (20)

Netty Notes Part 3 - Channel Pipeline and EventLoops
Netty Notes Part 3 - Channel Pipeline and EventLoopsNetty Notes Part 3 - Channel Pipeline and EventLoops
Netty Notes Part 3 - Channel Pipeline and EventLoops
 
Netty - a pragmatic introduction
Netty - a pragmatic introductionNetty - a pragmatic introduction
Netty - a pragmatic introduction
 
Zero-Copy Event-Driven Servers with Netty
Zero-Copy Event-Driven Servers with NettyZero-Copy Event-Driven Servers with Netty
Zero-Copy Event-Driven Servers with Netty
 
Introduction of netty
Introduction of nettyIntroduction of netty
Introduction of netty
 
Notes on Netty baics
Notes on Netty baicsNotes on Netty baics
Notes on Netty baics
 
Real time analytics with Netty, Storm, Kafka
Real time analytics with Netty, Storm, KafkaReal time analytics with Netty, Storm, Kafka
Real time analytics with Netty, Storm, Kafka
 
Netty from the trenches
Netty from the trenchesNetty from the trenches
Netty from the trenches
 
深入浅出Netty l.t
深入浅出Netty   l.t深入浅出Netty   l.t
深入浅出Netty l.t
 
Netty
NettyNetty
Netty
 
Asynchronous, Event-driven Network Application Development with Netty
Asynchronous, Event-driven Network Application Development with NettyAsynchronous, Event-driven Network Application Development with Netty
Asynchronous, Event-driven Network Application Development with Netty
 
Nettyらへん
NettyらへんNettyらへん
Nettyらへん
 
Netty 4-based RPC System Development
Netty 4-based RPC System DevelopmentNetty 4-based RPC System Development
Netty 4-based RPC System Development
 
Building Netty Servers
Building Netty ServersBuilding Netty Servers
Building Netty Servers
 
Xitrum internals
Xitrum internalsXitrum internals
Xitrum internals
 
Everything you wanted to know about writing async, concurrent http apps in java
Everything you wanted to know about writing async, concurrent http apps in java Everything you wanted to know about writing async, concurrent http apps in java
Everything you wanted to know about writing async, concurrent http apps in java
 
Netty
NettyNetty
Netty
 
Formal Grammars
Formal GrammarsFormal Grammars
Formal Grammars
 
Asynchronous serial data transfer, UART and CAN controller
Asynchronous serial data transfer, UART and CAN controllerAsynchronous serial data transfer, UART and CAN controller
Asynchronous serial data transfer, UART and CAN controller
 
High performance network programming on the jvm oscon 2012
High performance network programming on the jvm   oscon 2012 High performance network programming on the jvm   oscon 2012
High performance network programming on the jvm oscon 2012
 
Scalability using Node.js
Scalability using Node.jsScalability using Node.js
Scalability using Node.js
 

Semelhante a Netty: asynchronous data transfer

Stackless Python In Eve
Stackless Python In EveStackless Python In Eve
Stackless Python In Eve
l xf
 
Trelles_QnormBOSC2009
Trelles_QnormBOSC2009Trelles_QnormBOSC2009
Trelles_QnormBOSC2009
bosc
 

Semelhante a Netty: asynchronous data transfer (20)

Server side JavaScript: going all the way
Server side JavaScript: going all the wayServer side JavaScript: going all the way
Server side JavaScript: going all the way
 
Stackless Python In Eve
Stackless Python In EveStackless Python In Eve
Stackless Python In Eve
 
Scaling Apache Storm - Strata + Hadoop World 2014
Scaling Apache Storm - Strata + Hadoop World 2014Scaling Apache Storm - Strata + Hadoop World 2014
Scaling Apache Storm - Strata + Hadoop World 2014
 
JS everywhere 2011
JS everywhere 2011JS everywhere 2011
JS everywhere 2011
 
Parallel Processing
Parallel ProcessingParallel Processing
Parallel Processing
 
Java7 New Features and Code Examples
Java7 New Features and Code ExamplesJava7 New Features and Code Examples
Java7 New Features and Code Examples
 
Non-blocking I/O, Event loops and node.js
Non-blocking I/O, Event loops and node.jsNon-blocking I/O, Event loops and node.js
Non-blocking I/O, Event loops and node.js
 
Ngrep commands
Ngrep commandsNgrep commands
Ngrep commands
 
Trelles_QnormBOSC2009
Trelles_QnormBOSC2009Trelles_QnormBOSC2009
Trelles_QnormBOSC2009
 
swift-nio のアーキテクチャーと RxHttpClient
swift-nio のアーキテクチャーと RxHttpClientswift-nio のアーキテクチャーと RxHttpClient
swift-nio のアーキテクチャーと RxHttpClient
 
Opendaylight app development
Opendaylight app developmentOpendaylight app development
Opendaylight app development
 
Scalaxb preso
Scalaxb presoScalaxb preso
Scalaxb preso
 
Storm Anatomy
Storm AnatomyStorm Anatomy
Storm Anatomy
 
ATS Internals
ATS InternalsATS Internals
ATS Internals
 
ATS Internals
ATS InternalsATS Internals
ATS Internals
 
Python for Linux System Administration
Python for Linux System AdministrationPython for Linux System Administration
Python for Linux System Administration
 
The Functional Web
The Functional WebThe Functional Web
The Functional Web
 
Introduction to clojure
Introduction to clojureIntroduction to clojure
Introduction to clojure
 
Leveraging Hadoop in your PostgreSQL Environment
Leveraging Hadoop in your PostgreSQL EnvironmentLeveraging Hadoop in your PostgreSQL Environment
Leveraging Hadoop in your PostgreSQL Environment
 
Reactive Extensions
Reactive ExtensionsReactive Extensions
Reactive Extensions
 

Último

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 

Último (20)

[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 

Netty: asynchronous data transfer

  • 1. Netty List("Asynchronous", "data", "transfer") ::: List("practical", "aspects") Victor Cherkassky "vcherkassky@gmail.com"
  • 2. What is Netty IO library IO cat Files > Networking
  • 3. What is Netty IO library asynchronous ChannelFuture future = channel.write(message) future.addListener(WRITE_COMPLETE_LISTENER)
  • 4. What is Netty IO library asynchronous non-blocking       /**         * This method blocks until input data is available         */ InputStream.read()
  • 5. What is Netty IO library asynchronous non-blocking multi-protocol HTTP | WebSocket | Protobuf | Binary TCP | UDP
  • 6. blocking vs non-blocking blocking (OIO) Ipttemi =nwFlIpttem"nu.i"; nuSra s e ienuSra(iptbn) itbt =i.ed) / crettra wisfrrsl o err n ye sra(; / urn hed at o eut r ro non-blocking (NIO) wie(re { hl tu) slco.eet) / rqetn eet fo mlil canl eetrslc(; / eusig vns rm utpe hnes Ieao i =slco.eetdes)ieao(; trtr t eetrslceKy(.trtr) wie(thset) { hl i.aNx() SlcoKyky=(eetoKy i.et) eetre e Slcine) tnx(; hnlKyky; adee(e) i.eoe) trmv(; } }
  • 7. blocking vs non-blocking Hardware blocking write
  • 8. blocking vs non-blocking Hardware non-blocking write: DMA
  • 9. blocking vs non-blocking OS select()/poll() - traditional POSIX polling O(n) epoll() - event-based polling O(1), Linux 2.5.44+ kqueue - FreeBSD, Mac OS X I/O Completion Ports - Windows NT 3.5+, Solaris 10+
  • 10. blocking vs non-blocking Java SelectableChannel.register(selector) - blocking operation, registers an "interest" for IO events Selector.select - non-blocking operation, checks for occurred events
  • 11. blocking vs non-blocking Java OS select() / poll() / kqueue Channel.register & Selector.select I/O Completion Ports epoll() - Java 1.6+
  • 12. Thread model Tomcat 1 request = 1 thread fast for ~1000 clients too much memory
  • 13. Thread model Node.js all requests = 1 thread super scalable limited error handling
  • 14. Thread model Netty many requests = 1 thread flexible model
  • 18. All terms together Channel (socket read/write) channel Buffer (byte array) channel Event (message) channel Pipeline (message bus) channel Handler (message handler) Upstream Event (from socket) Downstream Event (to socket)
  • 19. Creating an HTTP server Set up thread pools boss pool for handling incoming connections worker pool for processing IO piaefnlEeuo bsPo =Eeuosnwahdhedol) rvt ia xctr osol xctr.eCceTraPo( ; piaefnlEeuo wrePo =Eeuosnwahdhedol rvt ia xctr okrol xctr.eCceTraPo (; )
  • 20. Creating an HTTP server Provide ChannelFactory piaefnlCanlatr canlatr = rvt ia hneFcoy hneFcoy nwNoevrokthneFcoybsPo,wrePo) e iSreSceCanlatr(osol okrol;
  • 21. Creating an HTTP server Set up PipelineFactory piaefnlCanlieieatr pplnFcoy= rvt ia hnePplnFcoy ieieatr nwCanlieieatr( { e hnePplnFcoy) @vrie Oerd pbi Canlieiegtieie)trw Ecpin{ ulc hnePpln ePpln( hos xeto Canlieiep=Canl.ieie) hnePpln hnesppln(; padat"tpevroe" nwHtSreCdc); .dLs(htSreCdc, e tpevroe() padat"ade" nwCsoRqetade(lCanl); .dLs(hnlr, e utmeusHnlralhnes) rtr p eun ; } };
  • 22. Creating an HTTP server Put all together with Bootstrap piaefnlSreBosrpbosrp=nwSreBosrpca rvt ia evrotta otta e evrotta(h neFcoy; nlatr) ... bosrpstieieatr(ieieatr) otta.ePplnFcoypplnFcoy; / Otosfraprn canl-i acpscnetos / pin o aet hne t cet oncin bosrpstpin"oaAdes,nwIeSceAdespr); otta.eOto(lcldrs" e ntoktdrs(ot) bosrpstpin"esAdes,tu) otta.eOto(ruedrs" re; / Otosfriscide -te poesI / pin o t hlrn hy rcs O bosrpstpin"hl.cNDly,tu) otta.eOto(cidtpoea" re; bosrpstpin"hl.eplv" fle; otta.eOto(cidkeAie, as)
  • 23. Creating an HTTP server Writing a custom handler casCsoHnlretnsSmlCanlptemade { ls utmade xed ipehneUsraHnlr ... @vrie Oerd pbi vi msaeeevdCanladeCnetcx Msae ulc od esgRcie(hneHnlrotx t, esgE vn e trw Ecpin{ et ) hos xeto HtRqetrq=(tpeus)egtesg(; tpeus e HtRqet .eMsae) / pouersos adwiei t acanl / rdc epne n rt t o hne HtRsos rs=nwDfuttpepneHT__,O) tpepne e e ealHtRsos(TP11 K; Canluueftr =Canl.rt(t.eCanl) rs; hneFtr uue hneswiecxgthne(, e) ftr.dLsee(hneFtrLsee.LS) uueaditnrCanluueitnrCOE; } }
  • 24. Creating an HTTP server Gather channels for future closing piaefnlCanlru alhnes=nwDfuthneGop rvt ia hneGop lCanl e ealCanlru( ) ... / aanisd orcso hnlr / gi nie u utm ade casCsoHnlretnsSmlCanlptemade { ls utmade xed ipehneUsraHnlr ... @vrie Oerd pbi vi canlpnCanladeCnetcx Canltt ulc od hneOe(hneHnlrotx t, hneSae Eete trw Ecpin{ vn ) hos xeto Canlcanl=egthne(; hne hne .eCanl) alhnesadcanl; lCanl.d(hne) } }
  • 25. Creating an HTTP server Release resources close gathered channels release external resources alhnescoe)aatnnerpil(; lCanl.ls(.wiUitrutby) bosrprlaexenleore(; otta.eesEtraRsucs)
  • 26. TCPnodelay, TCPkeepalive Remember that bootstrap code? / Otosfriscide -te poesI / pin o t hlrn hy rcs O bosrpstpin"hl.cNDly,tu) otta.eOto(cidtpoea" re; bosrpstpin"hl.eplv" fle; otta.eOto(cidkeAie, as) What do these options mean?
  • 27. TCP keepAlive prolongs each TCP connection makes NAT router happy
  • 28. TCP noDelay Nagle's algorithm glues up small TCP packets tries to reduce bandwidth increases latency
  • 29. TCPnodelay, TCPkeepalive To use, or not to use?
  • 31. Thank you Additional resources netty @ github introductory article