| CODENOTIFIER | HelpYou are not signed inSign in |
Project: Revactor
Revision: 129
Author: tarcieri
Date: 15 May 2008 15:12:27
Changes:Spruce up examples
Files:| ... | ...@@ -0,0 +1,95 @@ | |
| 1 | require 'zlib' | |
| 2 | require 'stringio' | |
| 3 | ||
| 4 | require 'rubygems' | |
| 5 | require 'revactor' | |
| 6 | ||
| 7 | # A concurrent HTTP fetcher, implemented using a central dispatcher which | |
| 8 | # scatters requests to a worker pool. | |
| 9 | # | |
| 10 | # The HttpFetcher class is callback-driven and intended for subclassing. | |
| 11 | # When a request completes successfully, the on_success callback is called. | |
| 12 | # An on_failure callback represents non-200 HTTP responses, and on_error | |
| 13 | # delivers any exceptions which occured during the fetch. | |
| 14 | class HttpFetcher | |
| 15 | def initialize(nworkers = 8) | |
| 16 | @_nworkers = nworkers | |
| 17 | @_workers, @_queue = [], [] | |
| 18 | nworkers.times { @_workers << Worker.spawn(Actor.current) } | |
| 19 | end | |
| 20 | ||
| 21 | def get(url, *args) | |
| 22 | if @_workers.empty? | |
| 23 | @_queue << T[url, args] | |
| 24 | else | |
| 25 | @_workers.shift << T[:fetch, url, args] | |
| 26 | end | |
| 27 | end | |
| 28 | ||
| 29 | def run | |
| 30 | while true | |
| 31 | Actor.receive do |filter| | |
| 32 | filter.when(T[:ready]) do |_, worker| | |
| 33 | if @_queue.empty? | |
| 34 | @_workers << worker | |
| 35 | on_empty if @_workers.size == @_nworkers | |
| 36 | else | |
| 37 | worker << T[:fetch, *@_queue.shift] | |
| 38 | end | |
| 39 | end | |
| 40 | ||
| 41 | filter.when(T[:fetched]) { |_, url, document, args| on_success url, document, *args } | |
| 42 | filter.when(T[:failed]) { |_, url, status, args| on_failure url, status, *args } | |
| 43 | filter.when(T[:error]) { |_, url, ex, args| on_error url, ex, *args } | |
| 44 | end | |
| 45 | end | |
| 46 | end | |
| 47 | ||
| 48 | def on_success(url, document, *args); end | |
| 49 | def on_failure(url, status, *args); end | |
| 50 | def on_error(url, ex, *args); end | |
| 51 | def on_empty; exit; end | |
| 52 | ||
| 53 | class Worker | |
| 54 | extend Actorize | |
| 55 | ||
| 56 | def initialize(fetcher) | |
| 57 | @fetcher = fetcher | |
| 58 | loop { wait_for_request } | |
| 59 | end | |
| 60 | ||
| 61 | def wait_for_request | |
| 62 | Actor.receive do |filter| | |
| 63 | filter.when(T[:fetch]) do |_, url, args| | |
| 64 | begin | |
| 65 | fetch url, args | |
| 66 | rescue => ex | |
| 67 | @fetcher << T[:error, url, ex, args] | |
| 68 | end | |
| 69 | ||
| 70 | # FIXME this should be unnecessary, but the HTTP client "leaks" messages | |
| 71 | Actor.current.mailbox.clear | |
| 72 | @fetcher << T[:ready, Actor.current] | |
| 73 | end | |
| 74 | end | |
| 75 | end | |
| 76 | ||
| 77 | def fetch(url, args) | |
| 78 | Actor::HttpClient.get(url, :head => {'Accept-Encoding' => 'gzip'}) do |response| | |
| 79 | if response.status == 200 | |
| 80 | @fetcher << T[:fetched, url, decode_body(response), args] | |
| 81 | else | |
| 82 | @fetcher << T[:failed, url, response.status, args] | |
| 83 | end | |
| 84 | end | |
| 85 | end | |
| 86 | ||
| 87 | def decode_body(response) | |
| 88 | if response.content_encoding == 'gzip' | |
| 89 | Zlib::GzipReader.new(StringIO.new(response.body)).read | |
| 90 | else | |
| 91 | response.body | |
| 92 | end | |
| 93 | end | |
| 94 | end | |
| 95 | end | |
| 0 | 96 | \ No newline at end of file |
| ... | ...@@ -1,3 +1,10 @@ | |
| 1 | # An Actor ring example | |
| 2 | # | |
| 3 | # Here we construct a ring of interconnected Actors which each know the | |
| 4 | # next Actor to send messages to. Any message sent from the parent Actor | |
| 5 | # is delivered around the ring and back to the parent. | |
| 6 | ||
| 7 | require 'rubygems' | |
| 1 | 8 | require 'revactor' |
| 2 | 9 | |
| 3 | 10 | NCHILDREN = 5 |
| ... | ...@@ -1,3 +1,13 @@ | |
| 1 | # An example of trapping exit messages | |
| 2 | # | |
| 3 | # Here we create a new Actor which raises an unhandled exception | |
| 4 | # whenever it receives the :die message. | |
| 5 | # | |
| 6 | # The parent Actor is linked to this one, and is set to trap exits | |
| 7 | # When the child raises the unhandled exception, the exit message | |
| 8 | # is delivered back to the parent. | |
| 9 | ||
| 10 | require 'rubygems' | |
| 1 | 11 | require 'revactor' |
| 2 | 12 | |
| 3 | 13 | actor = Actor.spawn_link do |