Giter VIP home page Giter VIP logo

cool.io's Introduction

Cool.io

If you are interested in Celluloid based IO framework, please check out Celluloid::IO

Cool.io is an event library for Ruby, built on the libev event library which provides a cross-platform interface to high performance system calls . This includes the epoll system call for Linux, the kqueue system call for BSDs and OS X, and the completion ports interface for Solaris.

Cool.io also binds asynchronous wrappers to Ruby's core socket classes so you can use them in conjunction with Cool.io to build asynchronous event-driven applications.

You can include Cool.io in your programs with:

require 'cool.io'

Anatomy

Cool.io builds on two core classes which bind to the libev API:

  • Cool.io::Loop - This class represents an event loop which uses underlying high performance system calls to wait for events.

  • Cool.io::Watcher - This is the base class for event observers. Once you attach an event observer to a loop and start running it, you will begin receiving callbacks to particlar methods when events occur.

Watchers

There are presently four types of watchers:

  • Cool.io::IOWatcher - This class waits for an IO object to become readable, writable, or both.

  • Cool.io::TimerWatcher - This class waits for a specified duration then fires an event. You can also configure it to fire an event at specified intervals.

  • Cool.io::StatWatcher - Monitors files or directories for changes

  • Cool.io::AsyncWatcher - Can be used to wake up a Cool.io::Loop running in a different thread. This allows each thread to run a separate Cool.io::Loop and for the different event loops to be able to signal each other.

Using Watchers

Watchers have five important methods:

  • attach(loop) - This binds a watcher to the specified event loop. If the watcher is already bound to a loop it will be detached first, then attached to the new one.

  • detach - This completely unbinds a watcher from an event loop.

  • disable - This stops the watcher from receiving events but does not unbind it from the loop. If you are trying to toggle a watcher on and off, it's best to use this method (and enable) as it performs better than completely removing the watcher from the event loop.

  • enable - This re-enables a watcher which has been disabled in the past. The watcher must still be bound to an event loop.

  • evloop - This returns the Cool.io::Loop object which the watcher is currently bound to.

Asynchronous Wrappers

Several classes which provide asynchronous event-driven wrappers for Ruby's core socket classes are also provided. Among these are:

  • Cool.io::TCPSocket - A buffered wrapper to core Ruby's Socket class for use with TCP sockets. You can asynchronously create outgoing TCP connections using its Cool.io::TCPSocket.connect method. Cool.io::TCPSocket provides write buffering to ensure that writing never blocks, and has asynchronous callbacks for several events, including when the connection is opened (or failed), when data is received, when the write buffer has been written out completely, and when the connection closes.

  • Cool.io::TCPServer - A wrapper for TCPServer which creates new instances of Cool.io::TCPSocket (or any subclass you wish to provide) whenever an incoming connection is received.

Example Program

Cool.io provides a Sinatra-like DSL for authoring event-driven programs:

require 'cool.io'

ADDR = '127.0.0.1'
PORT = 4321

cool.io.connection :echo_server_connection do
  on_connect do
    puts "#{remote_addr}:#{remote_port} connected"
  end

  on_close do
    puts "#{remote_addr}:#{remote_port} disconnected"
  end

  on_read do |data|
    write data
  end
end

puts "Echo server listening on #{ADDR}:#{PORT}"
cool.io.server ADDR, PORT, :echo_server_connection
cool.io.run

This creates a new connection class called :echo_server_connection and defines a set of callbacks for when various events occur.

We then create a new server on the given address and port. When this server receives new connections, it will create new instances of the given connection class for each connection.

Finally, we kick everything off with cool.io.run. Calling cool.io.run will block, listening for events on our server.

Using Cool.io subclasses directly

Below is an example of how to write an echo server using a subclass instead of the DSL:

require 'cool.io'
HOST = 'localhost'
PORT = 4321

class EchoServerConnection < Cool.io::TCPSocket
  def on_connect
    puts "#{remote_addr}:#{remote_port} connected"
  end

  def on_close
    puts "#{remote_addr}:#{remote_port} disconnected"
  end

  def on_read(data)
    write data
  end
end

server = Cool.io::TCPServer.new(HOST, PORT, EchoServerConnection)
server.attach(Cool.io::Loop.default)

puts "Echo server listening on #{HOST}:#{PORT}"
Cool.io::Loop.default.run

Here a new observer type (EchoServerConnection) is made by subclassing an existing one and adding new implementations to existing event handlers.

A new event loop is created, and a new Cool.io::TCPServer (whose base class is Cool.io::Watcher) is created and attached to the event loop.

Once this is done, the event loop is started with event_loop.run. This method will block until there are no active watchers for the loop or the loop is stopped explicitly with event_loop.stop.

cool.io's People

Contributors

authornari avatar dirtyice avatar godfat avatar hsbt avatar kou avatar luislavena avatar paddor avatar rdp avatar repeatedly avatar sonots avatar taichi avatar tarcieri avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

cool.io's Issues

[WIP] Support JRuby

そもそも分かる事が少ないので出来ることからノンビリやります。
ここは、自分が何やってるのか忘れない為のメモ置場とします。

現時点では設計の方針さえも不安定な感じなので、実装が固まってきたら英語でPR作ります。

ゴール

  • jruby上でspecを全部通す。
    • 通す為にコードを修正することもある。
  • fluentd をjrubyで動かす。
  • fluentd をoneJarにする。

作業メモ

  • Gradleによるビルド環境の構築
  • eclipseによる開発環境の構築
  • RakefileをJRuby対応
    • IS_JRUBY
    • ragelをjruby時は無視する
  • cool.ioのAPI構造の調査
    • cool.ioの利用者が直接呼び出すAPIとそうでないAPIを腑分けする。
  • iobufferをnetty-bufferベースで再実装する
  • io.rbをjavaで再実装する
    • on_readable及びon_writeableがイベントとして消える
    • より高速に動作するようになる
  • Loop#run_onceの戻り値をbooleanにしたい。
    • LinkedBlockingQueue#sizeは遅い
  • jruby用のインターフェーススケッチとなるコードの作成
  • テスト実行環境の整備
  • テストコードの整備
  • マイクロベンチマークコードの整備
  • Gradle依存をやめてRakeだけで開発する方法が無いか模索する。(優先度低)
  • デバッグ用のログをinfoレベルで出しているので、これを全てdebugレベルにする。
  • スレッドプールの扱いに何か問題があるようだ。再現性は低いがデッドロックしてるっぽい。
    • イベントバッファリングするようにしたらデッドロックが無くなったようだ。要検証。
  • specだけのPRを作る
  • jruby対応のPRを作る
  • 仕様変更を含む大掛かりなPRを作る
  • on_write_complete をrubyコードの修正なしに動くようにする
  • on_readを直接呼ぶように変更する
  • finalizer でdetachするように変更する。現状はdetach漏れがあるとNettyが暴走するので危ない。

設計方針メモ

  • 基本的には現行のコードを余り修正しない
  • Rubyのコードはruby/jrubyの両面対応用のコード以外は書かない
  • Cのコードは全面的にjavaで作り直す
  • スレッドプールの廃棄の為に既存には無い仕様の変更が必要になるかもしれない(要検証)
  • nio4r もしくは、Netty を使う、コードの状態をみつつコアAPIだけでやるかも検討する。
  • まずは、RxNettyベースで実装してRx部分が必要か検討する
    • Nettyだけでやりはじめた。
  • コードとテストコードが安定してきたら依存ライブラリを削る

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.