Upgrade to Pro — share decks privately, control downloads, hide ads and more …

FINAGLE - AN INTRO TO RPC & ASYNC PROGRAMMING IN JVM

FINAGLE - AN INTRO TO RPC & ASYNC PROGRAMMING IN JVM

FINAGLE - AN INTRO TO RPC & ASYNC PROGRAMMING IN JVM

prassee

June 14, 2013
Tweet

More Decks by prassee

Other Decks in Technology

Transcript

  1. Finagle - An Intro to RPC & Async programming in

    JVM P r a s a n n a K u m a r . S p r a s a n n a . s a t h y a n a r a y a n a n @ c s s c o r p . c o m T w i t t e r @ p r a s o n s c a l a
  2. Agenda Why RPC & Async programming is Interesting ? Lets

    know about "Future" & "Promise " Finagle !!!! - Writing Protocol Handlers Code - Lets get in to Action Use Cases - Where could I use Finagle Q&A
  3. Why RPC & Async programming ? I know my infrastructure

    I wanna provide an platform Isolated computation with callable API's Don't call me let me call u !!! RPC + Async gives steroids for your server apps
  4. Lets know about Future The fundmental abstraction of Async Programming

    (Fingale as well :-) ) A Future is computation which has not yet performed, i.e it is supposed to happen !!!
  5. Finagle Writing Protocol Handlers Client and Server are just s

    e r v i c e Define Server / Client in terms of s e r v i c e
  6. Exposing a Service - (Server Side) / / i m

    p l e m e n t a s e r v i c e v a l s e r v i c e = n e w S e r v i c e [ H t t p R e q u e s t , H t t p R e s p o n s e ] { d e f a p p l y ( r e q : H t t p R e q u e s t ) = { F u t u r e ( n e w D e f a u l t H t t p R e s p o n s e ( H t t p V e r s i o n . H T T P _ 1 _ 1 , H t t p R e s p o n s e S t a t u s . O K ) ) } } / / c o n f i g u r e t h e s e r v i c e S e r v e r B u i l d e r ( ) . c o d e c ( H t t p ( ) ) . b i n d T o ( n e w I n e t S o c k e t A d d r e s s ( 1 0 0 0 0 ) ) . n a m e ( " c h u t t i - s e r v e r " ) . b u i l d ( s e r v i c e )
  7. Consuming with Client v a l c l i e

    n t = C l i e n t B u i l d e r ( ) . c o d e c ( H t t p ( ) ) . h o s t s ( " 1 0 . 1 0 . 2 7 . 1 4 : 1 0 0 0 0 " ) . h o s t C o n n e c t i o n L i m i t ( 1 ) . b u i l d ( ) v a l r e q = n e w D e f a u l t H t t p R e q u e s t ( H t t p V e r s i o n . H T T P _ 1 _ 1 , H t t p M e t h o d . G E T , " / " ) v a l f u t = c l i e n t ( r e q ) f u t o n S u c c e s s ( m s g = > { p r i n t l n ( m s g ) } )
  8. Putting it all o b j e c t C

    h u t t i S e r v e r { p r i v a t e l a z y v a l g l o b a l R e s p o n s e = ( r e q : H t t p R e q u e s t ) = > { v a l r e s p = n e w D e f a u l t H t t p R e s p o n s e ( H t t p V e r s i o n . H T T P _ 1 _ 1 , H t t p R e s p o n s e S t a t u s . O K ) v a l h t m l R e s = " w e l c o m e f i n a g l e s e s s i o n ! ! ! ! " . g e t B y t e s ( ) r e s p . s e t C o n t e n t ( C h a n n e l B u f f e r s . c o p i e d B u f f e r ( h t m l R e s ) ) F u t u r e . v a l u e ( r e s p ) } d e f s t a r t ( ) : U n i t = { / / i m p l e m e n t a s e r v i c e v a l s e r v i c e = n e w S e r v i c e [ H t t p R e q u e s t , H t t p R e s p o n s e ] { d e f a p p l y ( r e q : H t t p R e q u e s t ) = { r e q . g e t U r i ( ) m a t c h { c a s e " / " = > g l o b a l R e s p o n s e ( r e q ) c a s e _ = > F u t u r e ( n e w D e f a u l t H t t p R e s p o n s e ( H t t p V e r s i o n . H T T P _ 1 _ 1 , H t t p R e s p o n s e S t a t u s . N O T _ F O U N D ) ) } } } v a l f i l = n e w C h u t t i S e r v e r F i l t e r [ H t t p R e q u e s t , H t t p R e s p o n s e ] ( D u r a t i o n . f r o m M i l l i s e c o n d s ( 1 0 0 ) , n e w J a v a T i m e r ) v a l s v c = f i l a n d T h e n f i l a n d T h e n s e r v i c e / / c o n f i g u r e t h e s e r v i c e S e r v e r B u i l d e r ( ) . c o d e c ( H t t p ( ) ) . b i n d T o ( n e w I n e t S o c k e t A d d r e s s ( 1 0 0 0 0 ) ) . n a m e ( " l o c a l h o s t " ) . b u i l d ( s v c ) } } / / F i l t e r d e f i n i t i o n c l a s s C h u t t i S e r v e r F i l t e r [ H t t p R e q u e s t , H t t p R e s p o n s e ] ( t i m e o u t : D u r a t i o n , t i m e r : T i m e r ) e x t e n d s F i l t e r [ H t t p R e q u e s t , H t t p R e s p o n s e , H t t p R e q u e s t , H t t p R e s p o n s e ]
  9. { d e f a p p l y (

    r e q u e s t : H t t p R e q u e s t , s e r v i c e : S e r v i c e [ H t t p R e q u e s t , H t t p R e s p o n s e ] ) : F u t u r e [ H t t p R e s p o n s e ] = { p r i n t l n ( " r e q p a s s e d t h r u " ) s e r v i c e ( r e q u e s t ) } }
  10. Putting it all (Contd.) o b j e c t

    C h u t t i C l i e n t { d e f m a i n ( a r g s : A r r a y [ S t r i n g ] ) { v a l c l i e n t = C l i e n t B u i l d e r ( ) . c o d e c ( H t t p ( ) ) . h o s t s ( " 1 0 . 1 0 . 2 7 . 1 4 : 1 0 0 0 0 " ) . h o s t C o n n e c t i o n L i m i t ( 1 ) . b u i l d ( ) v a l r e q = n e w D e f a u l t H t t p R e q u e s t ( H t t p V e r s i o n . H T T P _ 1 _ 1 , H t t p M e t h o d . G E T , " / " ) v a l f u t = c l i e n t ( r e q ) f u t o n S u c c e s s ( m s g = > { p r i n t l n ( m s g ) } ) } }
  11. Q&A