Async Server and Fiber C [email protected] Me Github/Luikore Ruby-China/Luikore Motivation Challenges to Ruby Ruby Advantages Unicode Done Right

Total Page:16

File Type:pdf, Size:1020Kb

Async Server and Fiber C Zlui@Groupon.Com Me Github/Luikore Ruby-China/Luikore Motivation Challenges to Ruby Ruby Advantages Unicode Done Right Async Server And Fiber [email protected] Me github/luikore ruby-china/luikore Motivation Challenges To Ruby Ruby Advantages Unicode done right "".length Builtin continuation - Fiber And... Fast How To Build A High Performance Server How To Build An Async Server How To Build An Async Server Yet Providing Good API Study The Background The Bottle Neck Client Side Of View IO bound - network is slow CPU bound - complex page rendering Server load - wait for other requests to finish The Bottle Neck Server Side Of View IO bound - visiting database / oauth is slow CPU bound - context switch Scaling Increase App Throughput Reduce server side IO-waiting is the most effective way Create less threads, to avoid context switching Learn From Existing Solutions Chances To Ruby Limited Server Objects Many Crazy Clients Multiplexing Single Threaded One Client, One Process, Prefork unicorn -- multiplex with accept thin -- multiplex with load balancer Multithreaded One Client, One Thread, No Prefork puma, recommended for rails4 A simple way to implement is to use POSIX functions with callbacks: aio_read(), aio_write() Single Threaded And Event Loop One Client, One Event Context, Prefork nginx, redis, nodejs, twisted, eventmachine Thread Pool On Event Loop One Client, One Event Context, No Prefork erlang, golang, scala actors Hello World TCPSocket In 5 Minutes client TCPSocket.new host, port server TCPServer.new host, port receive socket.read socket.read_nonblock send socket.write socket.write_nonblock Hello World HTTP Server server = TCPServer.new 'localhost', 4000 loop do c = server.accept c << "HTTP/1.1 200 OK Content-Length: 12 Hello world!" c.close end Make It Fast Non­Blocking Our Loop loop do client = server.accept Thread.new do ... end end Threads Are Outdated Lets Use System Events Multiplexing Functions select IO.select r, w, err, timeout poll IO.poll fds, timeout System Events Linux For Example poll - iterate check all fds epoll - is more effective and provides more options Epoll In 5 Minutes sudo apt-get install manpages-dev man epoll Epoll In 5 Minutes register event: epoll_ctl() retrieve event: epoll() Cons Ruby does not ship with epoll API. You need to wrap it with C-ext. Loop With Epoll loop do event = epoll(timeout) if from_server?(event) connection = server.accept else connection = find_conn_by(event) end ... # resume processing end How To Make It Resumable? Eventmachine async web server clients are multiplexed to EM::Connection you define callbacks for readable events: def receive_data(data) "Resume Processing" connection.receive_data read_nonblock Make It Good How EM Send_data Works write to socket, if the socket can’t take any more, append the rest to a buffer chain when the socket becomes writable again (IO event), try consume the buffer chain Pitfalls Callbacks Are Not Sufficient avoid loss of chained buffers close_connection_after_writing avoid infinite recursion when registering events in callback: EM.next_tick{ ... } Resume Processing With Fiber Fiber was the “Thread” in 1.8 Like thread, but lighter, pausable, and no need to lock Example fiber = Fiber.new do i = 0 loop do Fiber.yield i += 1 end end fiber.resume #=> 1 fiber.resume #=> 2 Read With Fiber def read_with_fiber result = '' while buffer = read_nonblock result << buffer if nothing to read Fiber.yield :reading! end end result end Write With Fiber def write_with_fiber content loop do write_nonblock content content = unwritten part of content break if content.empty? Fiber.yield :writing! end end Recall Our Loop loop do event = epoll(timeout) if from_server?(event) connection = server.accept else connection = find_conn_by(event) end ... # resume processing end loop do event = epoll(timeout) if from_server?(event) connection = server.accept fiber = Fiber.new { ... read_with_fiber ... write_with_fiber } else fiber = find_fiber_by(event) end fiber.resume end Summary Network latency and C10k leads to async solutions. With Fiber it can be callback-free. In concept it is simple. In practice it is complex: protocols, IO exceptions, ... Product github.com/luikore/nyara faster than sinatra/expressjs in hello world benchmarks.
Recommended publications
  • A Sense of Time for Node.Js: Timeouts As a Cure for Event Handler Poisoning
    A Sense of Time for Node.js: Timeouts as a Cure for Event Handler Poisoning Anonymous Abstract—The software development community has begun to new Denial of Service attack that can be used against EDA- adopt the Event-Driven Architecture (EDA) to provide scalable based services. Our Event Handler Poisoning attack exploits web services. Though the Event-Driven Architecture can offer the most important limited resource in the EDA: the Event better scalability than the One Thread Per Client Architecture, Handlers themselves. its use exposes service providers to a Denial of Service attack that we call Event Handler Poisoning (EHP). The source of the EDA’s scalability is also its Achilles’ heel. Multiplexing unrelated work onto the same thread re- This work is the first to define EHP attacks. After examining EHP vulnerabilities in the popular Node.js EDA framework and duces overhead, but it also moves the burden of time sharing open-source npm modules, we explore various solutions to EHP- out of the thread library or operating system and into the safety. For a practical defense against EHP attacks, we propose application itself. Where OTPCA-based services can rely on Node.cure, which defends a large class of Node.js applications preemptive multitasking to ensure that resources are shared against all known EHP attacks by making timeouts a first-class fairly, using the EDA requires the service to enforce its own member of the JavaScript language and the Node.js framework. cooperative multitasking [89]. An EHP attack identifies a way to defeat the cooperative multitasking used by an EDA-based Our evaluation shows that Node.cure is effective, broadly applicable, and offers strong security guarantees.
    [Show full text]
  • Overview Guide Release 21B F45060-01
    Oracle Utilities Customer Care and Billing Cloud Service Overview Guide Release 21B F45060-01 August 2021 Oracle Utilities Customer Care and Billing Cloud Service Release 21B Overview Guide Copyright © 2012, 2021 Oracle and/or its affiliates. All rights reserved. This software and related documentation are provided under a license agreement containing restrictions on use and disclosure and are protected by intellectual property laws. Except as expressly permitted in your license agreement or allowed by law, you may not use, copy, reproduce, translate, broadcast, modify, license, transmit, distribute, exhibit, perform, publish, or display any part, in any form, or by any means. Reverse engineering, disassembly, or decompilation of this software, unless required by law for interoperability, is prohibited. The information contained herein is subject to change without notice and is not warranted to be error-free. If you find any errors, please report them to us in writing. If this is software or related documentation that is delivered to the U.S. Government or anyone licensing it on behalf of the U.S. Government, then the following notice is applicable: U.S. GOVERNMENT END USERS: Oracle programs (including any operating system, integrated software, any programs embedded, installed or activated on delivered hardware, and modifications of such programs) and Oracle computer documentation or other Oracle data delivered to or accessed by U.S. Government end users are "commercial computer software" or "commercial computer software documentation"
    [Show full text]
  • Industrial Ethernet Quick and Simple Explanation of Key Terms
    Industrial Ethernet Quick and simple explanation of key terms EN_Bro_Glossar_Netzwerktechnik_A6_Rev02.indd 1 26.05.14 15:26 Industrial Ethernet The use of industrial Ethernet communication offers many advantages and is becoming more common in the automation of production plants and machinery. Many new technical terms from the IT world are also being used in this context among industrial users. In this glossary, we have listed and explained the main techni- cal terms from the field of Industrial Ethernet – we make Ethernet easy. 2 PHOENIX CONTACT EN_Bro_Glossar_Netzwerktechnik_A6_Rev02.indd 2 26.05.14 15:26 Table of contents Glossary Pages 04 – 45 IEEE standards Pages 46 – 47 PHOENIX CONTACT 3 EN_Bro_Glossar_Netzwerktechnik_A6_Rev02.indd 3 26.05.14 15:26 Industrial Ethernet 10Base-T Standard for data transmission of 10 Mbps Ethernet using unshielded twisted pair cables (Category 3, 4 or 5). 100Base-FX Standard for data transmission of 100 Mbps Ethernet using fiber optic cables. 100Base-TX Standard for data transmission of 100 Mbps Ethernet using twisted pair cables (Category 5). Each connection is established via two wire pairs, one wire pair for “transmit data” and the other for “receive data”. 100Base-T Fast Ethernet; 100Base-T has been officially elevated to an IEEE standard as ITU 802.3u. This standard is essentially based on technologies for 10Base-T, the Ethernet version for twisted pair cables. There are several versions of 100Base-T, which differ with respect to the physical layer and therefore the transmission media: 100Base-TX, 100Base-T2, 100Base-T4, and 100Base-FX. With this method, the MAC level and therefore the conventional CSMA/CD access method are retained at a transmission speed of 100 Mbps.
    [Show full text]
  • Python Guide Documentation 0.0.1
    Python Guide Documentation 0.0.1 Kenneth Reitz 2015 11 07 Contents 1 3 1.1......................................................3 1.2 Python..................................................5 1.3 Mac OS XPython.............................................5 1.4 WindowsPython.............................................6 1.5 LinuxPython...............................................8 2 9 2.1......................................................9 2.2...................................................... 15 2.3...................................................... 24 2.4...................................................... 25 2.5...................................................... 27 2.6 Logging.................................................. 31 2.7...................................................... 34 2.8...................................................... 37 3 / 39 3.1...................................................... 39 3.2 Web................................................... 40 3.3 HTML.................................................. 47 3.4...................................................... 48 3.5 GUI.................................................... 49 3.6...................................................... 51 3.7...................................................... 52 3.8...................................................... 53 3.9...................................................... 58 3.10...................................................... 59 3.11...................................................... 62
    [Show full text]
  • Node.Js: Building for Scalability with Server-Side Javascript
    #141 CONTENTS INCLUDE: n What is Node? Node.js: Building for Scalability n Where does Node fit? n Installation n Quick Start with Server-Side JavaScript n Node Ecosystem n Node API Guide and more... By Todd Eichel Visit refcardz.com Consider a food vending WHAT IS NODE? truck on a city street or at a festival. A food truck In its simplest form, Node is a set of libraries for writing high- operating like a traditional performance, scalable network programs in JavaScript. Take a synchronous web server look at this application that will respond with the text “Hello would have a worker take world!” on every HTTP request: an order from the first customer in line, and then // require the HTTP module so we can create a server object the worker would go off to var http = require(‘http’); prepare the order while the customer waits at the window. Once Get More Refcardz! Refcardz! Get More // Create an HTTP server, passing a callback function to be the order is complete, the worker would return to the window, // executed on each request. The callback function will be give it to the customer, and take the next customer’s order. // passed two objects representing the incoming HTTP // request and our response. Contrast this with a food truck operating like an asynchronous var helloServer = http.createServer(function (req, res) { web server. The workers in this truck would take an order from // send back the response headers with an HTTP status the first customer in line, issue that customer an order number, // code of 200 and an HTTP header for the content type res.writeHead(200, {‘Content-Type’: ‘text/plain’}); and have the customer stand off to the side to wait while the order is prepared.
    [Show full text]
  • Vert.X in Action MEAP
    MEAP Edition Manning Early Access Program Vert.x in Action Version 1 Copyright 2018 Manning Publications For more information on this and other Manning titles go to www.manning.com ©Manning Publications Co. We welcome reader comments about anything in the manuscript - other than typos and other simple mistakes. These will be cleaned up during production of the book by copyeditors and proofreaders. https://forums.manning.com/forums/vertx-in-action welcome Thank you for purchasing the MEAP of Vert.x in Action. Asynchronous and reactive applications are an important topic in modern distributed systems, especially as the progressive shift to virtualized and containerized runtime environments emphasize the need for resource efficient, adaptable and dependable application designs. Asynchronous programming is key to maximizing hardware resource usage, as it allows dealing with more concurrent connections than with the traditional blocking I/O paradigms. Services need to cater for workloads that may drastically change throughout from one hour to the other, hence we need to design code that naturally supports horizontal scalability. Last but not least, failure is inevitable when we have services interacting with other services over the network. Embracing failure is key for designing dependable systems. Assemble asynchronous programming, horizontal scalability, resilience and you have what we call today reactive applications, which can also be summarized without marketing jargon as “scalable and dependable applications”. That being said there is no free lunch and the transition to writing asynchronous and reactive applications is difficult when you have a background in more traditional software stacks. Grokking asynchrony in itself is difficult, but the implications of scalability and resilience on the design of an application are anything but trivial.
    [Show full text]
  • Graphite Documentation Release 1.1.8
    Graphite Documentation Release 1.1.8 Chris Davis Apr 19, 2021 Contents 1 Overview 1 2 FAQ 3 3 Installing Graphite 7 4 The Carbon Daemons 35 5 Feeding In Your Data 39 6 Getting Your Data Into Graphite 41 7 Administering Carbon 43 8 Administering The Webapp 45 9 Using The Composer 47 10 The Render URL API 49 11 The Metrics API 71 12 Functions 73 13 The Dashboard User Interface 105 14 The Whisper Database 113 15 The Ceres Database 117 16 Alternative storage finders 121 17 Graphite Events 125 18 Graphite Tag Support 129 19 Graphite Terminology 137 20 Tools That Work With Graphite 139 i 21 Working on Graphite-web 145 22 Client APIs 147 23 Who is using Graphite? 149 24 Release Notes 151 25 Indices and tables 207 Python Module Index 209 Index 211 ii CHAPTER 1 Overview 1.1 What Graphite is and is not Graphite does two things: 1. Store numeric time-series data 2. Render graphs of this data on demand What Graphite does not do is collect data for you, however there are some tools out there that know how to send data to graphite. Even though it often requires a little code, sending data to Graphite is very simple. 1.2 About the project Graphite is an enterprise-scale monitoring tool that runs well on cheap hardware. It was originally designed and written by Chris Davis at Orbitz in 2006 as side project that ultimately grew to be a foundational monitoring tool. In 2008, Orbitz allowed Graphite to be released under the open source Apache 2.0 license.
    [Show full text]
  • @Let@Token Node.Js
    node.js [email protected] November 8, 2009 node.js in brief: I Server-side Javascript I Built on Google’s V8 I Evented, non-blocking I/O. Similar to EventMachine or Twisted. I CommonJS module system. I 8000 lines of C/C++, 2000 lines of Javascript, 14 contributors. I/O needs to be done differently. Many web applications have code like this: var result = db.query("select * fromT"); // use result What is the software doing while it queries the database? In many cases, just waiting for the response. I/O latency L1: 3 cycles L2: 14 cycles RAM: 250 cycles DISK: 41,000,000 cycles NETWORK: 240,000,000 cycles http://duartes.org/gustavo/blog/post/ what-your-computer-does-while-you-wait Better software can multitask. Other threads of execution can run while waiting. Is that the best that can be done? A look at Apache and NGINX. Apache vs NGINX concurrency × reqs/sec http://blog.webfaction.com/a-little-holiday-present Apache vs NGINX concurrency × memory http://blog.webfaction.com/a-little-holiday-present Apache vs NGINX The difference? Apache uses one thread per connection. NGINX doesn’t use threads. It uses an event loop. I Context switching is not free I Execution stacks take up memory For massive concurrency, cannot use an OS thread for each connection. Green threads or coroutines can improve the situation dramatically BUT there is still machinery involved to create the illusion of holding execution on I/O. Threaded concurrency is a leaky abstraction. Code like this var result = db.query("select.."); // use result either blocks the entire process or implies multiple execution stacks.
    [Show full text]
  • Red Hat Enterprise Linux 7 7.8 Release Notes
    Red Hat Enterprise Linux 7 7.8 Release Notes Release Notes for Red Hat Enterprise Linux 7.8 Last Updated: 2021-03-02 Red Hat Enterprise Linux 7 7.8 Release Notes Release Notes for Red Hat Enterprise Linux 7.8 Legal Notice Copyright © 2021 Red Hat, Inc. The text of and illustrations in this document are licensed by Red Hat under a Creative Commons Attribution–Share Alike 3.0 Unported license ("CC-BY-SA"). An explanation of CC-BY-SA is available at http://creativecommons.org/licenses/by-sa/3.0/ . In accordance with CC-BY-SA, if you distribute this document or an adaptation of it, you must provide the URL for the original version. Red Hat, as the licensor of this document, waives the right to enforce, and agrees not to assert, Section 4d of CC-BY-SA to the fullest extent permitted by applicable law. Red Hat, Red Hat Enterprise Linux, the Shadowman logo, the Red Hat logo, JBoss, OpenShift, Fedora, the Infinity logo, and RHCE are trademarks of Red Hat, Inc., registered in the United States and other countries. Linux ® is the registered trademark of Linus Torvalds in the United States and other countries. Java ® is a registered trademark of Oracle and/or its affiliates. XFS ® is a trademark of Silicon Graphics International Corp. or its subsidiaries in the United States and/or other countries. MySQL ® is a registered trademark of MySQL AB in the United States, the European Union and other countries. Node.js ® is an official trademark of Joyent. Red Hat is not formally related to or endorsed by the official Joyent Node.js open source or commercial project.
    [Show full text]
  • Darren Dormer's Resume — Represent
    Darren Dormer Software Engineer I'm a software engineer from Johannesburg, South Africa, currently living in Vienna, Austria. My passion is in back-end development and systems architecture; however I'm proficient in front-end design and development. In my spare time you’ll find me expanding my computer science knowledge, attempting photography, practicing martial arts and contributing to open- source projects. Experience Projects Software Engineer, Owner Diamond Codeherent Quotemaster CC Johannesburg, South Africa Diamond is an electronic document signing platform Sep 2016 - Present that allows users to digitally sign documents via a Codeherent is a software development company one-time-pin delivered to their cellphone via SMS. founded by myself and my business partner, Jonathan Jacobs. Codeherent specialises in Entropy developing software and most often uses Python, Quotemaster CC Clojure and Javascript. I have contributed to Entropy which is an immutable object store, with replication to S3. Software Developer Ciuvo Querryl Vienna, Austria Personal, open source Oct 2017 - Present A Quassel-IRC log web-based text search. This was written out of frustration of Quassel’s lack of Software Engineer log text-search. I wrote it in Python with Twisted and Fusion Dealership Systems Javascript with Backbone.js. Johannesburg, South Africa Feb 2015 - Jul 2017 clj-dynamo The Fusion system from Quotemaster CC spun off Personal, open source into it's own company and our technology stack An IRC bot written in Clojure that reports on Github started to shift into a microservice architecture and and Bitbucket bugs and pull requests. include more modern front-end javascript tools. BDM Software Developer Personal, open source Quotemaster CC Single-page donation web application written with Johannesburg, South Africa Python and Twisted for the backend and Javascript Apr 2012 - Feb 2015 and react for the front-end.
    [Show full text]
  • Towards Modernising Data Collection and Archive for the Tor Network
    Towards Modernising Data Collection and Archive for the Tor Network Iain R. Learmonth and Karsten Loesing {irl,karsten}@torproject.org The Tor Project Tor Tech Report 2018-12-001 December 19, 2018 Contents 1 Introduction 2 2 Core Requirements3 3 The relaydescs module5 4 The onionperf module 12 5 Frameworks Evaluated 13 6 Prototype Implementation 20 7 Next Steps 21 Abstract arXiv:1812.08429v1 [cs.OH] 20 Dec 2018 CollecTor is developed by Tor Project’s Metrics Team for the purpose of archiving data relating to the public Tor network and applications developed by Tor Project. This report distills the requirements for a prototype modernized replacement of the CollecTor service, and evaluates frameworks and libraries that are available to reduce code maintenance costs for the CollecTor service. This work was supported by Open Technology Fund under grant number 1002-2017-018. Support does not imply endorsement. With thanks to Nick Matthewson and Tim Wilson-Brown for their help in clarifying certain points in specifications and how they are implemented in tor, and thanks to Damian Johnson for his assistance in experimentation using the stem library. 1 1 Introduction The Tor anonymity system [3] protects Internet users from tracking, surveillance, and censorship. The Tor network is made up of thousands of volunteer-run relays—servers that are usually located in data centers—distributed across the world that enable users to make private connections to services on the Internet. Currently, the vast majority of connections to the Tor network are made using the Tor Browser. But a growing number of applications use the Tor network, and we expect that many more will do so in the future.
    [Show full text]
  • Dell EMC Powerstore Open Source License and Copyright Information
    Open Source License and Copyright Information Dell EMC PowerStore Open Source License and Copyright Information June 2021 Rev A04 Revisions Revisions Date Description May 2020 Initial release September 2020 Version updates for some licenses and addition of iwpmd component December 2020 Version updates for some licenses, and addition and deletion of other components January 2021 Version updates for some licenses June 2021 Version updates for some licenses, and addition and deletion of other components The information in this publication is provided “as is.” Dell Inc. makes no representations or warranties of any kind with respect to the information in this publication, and specifically disclaims implied warranties of merchantability or fitness for a particular purpose. Use, copying, and distribution of any software described in this publication requires an applicable software license. Copyright © 2020-2021 Dell Inc. or its subsidiaries. All Rights Reserved. Dell Technologies, Dell, EMC, Dell EMC and other trademarks are trademarks of Dell Inc. or its subsidiaries. Other trademarks may be trademarks of their respective owners. [6/1/2021] [Open Source License and Copyright Information] [Rev A04] 2 Dell EMC PowerStore: Open Source License and Copyright Information Table of contents Table of contents Revisions............................................................................................................................................................................. 2 Table of contents ...............................................................................................................................................................
    [Show full text]