• Stars
    star
    694
  • Rank 62,595 (Top 2 %)
  • Language
    C
  • License
    MIT License
  • Created over 15 years ago
  • Updated 9 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Simple evented I/O for Ruby (but please check out Celluloid::IO instead)

Cool.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'
require 'cool.io/dsl'

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.

More Repositories

1

falcon

A high-performance web server for Ruby, supporting HTTP/1, HTTP/2 and TLS.
Ruby
2,427
star
2

async

An awesome asynchronous event-driven reactor for Ruby.
Ruby
1,937
star
3

nio4r

Cross-platform asynchronous I/O primitives for scalable network clients and servers.
C
954
star
4

rubydns

A DSL for building fun, high-performance DNS servers.
Ruby
704
star
5

timers

Pure Ruby timers collections suitable for use with event loops
Ruby
334
star
6

multipart-post

Adds multipart POST capability to net/http
Ruby
288
star
7

async-http

Ruby
280
star
8

localhost

Ruby
204
star
9

async-io

Concurrent wrappers for native Ruby IO & Sockets.
Ruby
199
star
10

lightio

LightIO is a userland implemented green thread library for ruby
Ruby
163
star
11

async-websocket

Asynchronous WebSocket client and server, supporting HTTP/1 and HTTP/2 for Ruby.
Ruby
143
star
12

utopia

A content-centric Ruby/Rack based web framework.
Ruby
136
star
13

cloudflare

An asynchronous Ruby wrapper for the CloudFlare V4 API.
Ruby
135
star
14

socketry

High-level wrappers for Ruby sockets with advanced thread-safe timeout support
Ruby
132
star
15

async-dns

An asynchronous DNS resolver and server.
Ruby
92
star
16

http-accept

Parse Accept and Accept-Language HTTP headers in Ruby.
Ruby
80
star
17

async-postgres

Ruby
78
star
18

async-redis

Ruby
76
star
19

async-await

Why wait? It's available today!
Ruby
68
star
20

async-container

Scalable multi-thread multi-process containers for Ruby.
Ruby
67
star
21

async-http-faraday

Ruby
67
star
22

async-rspec

Ruby
51
star
23

rackula

Generate a static site from any rack middleware.
Ruby
51
star
24

io-event

C
50
star
25

db

Event-driven database drivers for streaming queries.
Ruby
48
star
26

console

Ruby
47
star
27

live

JavaScript
46
star
28

roda-websockets

Asynchronous WebSockets plugin for Roda.
Ruby
44
star
29

process-metrics

Ruby
33
star
30

db-postgres

Ruby
32
star
31

async-rest

Ruby
29
star
32

async-pool

Provides support for connection pooling both singleplex and multiplex resources.
Ruby
23
star
33

async-job

Ruby
21
star
34

guard-falcon

Ruby
21
star
35

async-process

Ruby
20
star
36

async-actor

Ruby
18
star
37

protocol-http

Ruby
18
star
38

utopia-project

JavaScript
18
star
39

falcon-capybara

Ruby
18
star
40

benchmark-http

Ruby
17
star
41

rspec-memory

Ruby
15
star
42

falcon-rails-example

Ruby
15
star
43

async-examples

Ruby
14
star
44

traces

Ruby
14
star
45

cloudflare-dns-update

A Ruby script which can update CloudFlare periodically to provide dynamic DNS.
Ruby
14
star
46

thread-local

Ruby
13
star
47

async-sequel

Ruby
13
star
48

lively

JavaScript
13
star
49

fiber-local

Ruby
13
star
50

async-mysql

Ruby
12
star
51

falcon-benchmark

A work in progress synthetic benchmark comparing Falcon with other servers.
JavaScript
10
star
52

db-mariadb

Ruby
10
star
53

protocol-quic

C++
9
star
54

protocol-websocket

Provides a low-level implementation of the WebSocket protocol according to RFC6455.
Ruby
8
star
55

variant

Ruby
8
star
56

protocol-http2

Ruby
7
star
57

rack-conform

Ruby
7
star
58

async-limiter

Async limiter for ruby.
Ruby
7
star
59

metrics

Ruby
7
star
60

async-job-rails-example

Ruby
7
star
61

async-job-adapter-active_job

Ruby
6
star
62

async-webdriver

Ruby
6
star
63

async-worker

Ruby
5
star
64

memory

Ruby
5
star
65

async-http-cache

Ruby
5
star
66

protocol-http1

Ruby
5
star
67

db-active_record

Ruby
5
star
68

db-model

Ruby
4
star
69

async-debug

JavaScript
4
star
70

falcon-my_api

Ruby
3
star
71

protocol-hpack

Ruby
3
star
72

katacoda

Katacoda Tutorials
Shell
3
star
73

community

3
star
74

traces-backend-datadog

Ruby
3
star
75

async-bus

Ruby
3
star
76

live-js

JavaScript
3
star
77

migrate

Ruby
3
star
78

utopia-falcon-heroku

JavaScript
3
star
79

console-adapter-rails

Ruby
2
star
80

rails-falcon-heroku

Ruby
2
star
81

io-stream

Ruby
2
star
82

lively-falcon

Ruby
2
star
83

protocol-rack

Ruby
2
star
84

async-cable

Ruby
2
star
85

utopia-wiki

JavaScript
1
star
86

rspec-files

Ruby
1
star
87

console-adapter-sidekiq

Ruby
1
star
88

console-output-datadog

Ruby
1
star
89

sus-fixtures-openssl

Ruby
1
star
90

db-migrate

Ruby
1
star
91

metrics-backend-datadog

Ruby
1
star
92

traces-backend-open_telemetry

Ruby
1
star
93

async-service

Ruby
1
star
94

falcon-example-sinatra

Ruby
1
star
95

process-terminal

Ruby
1
star
96

async-slack

Ruby
1
star
97

xrb-rails

Ruby
1
star