Category Archives: rails

Java Network Programming

I had to recently write a smallish TCP/IP server in Java. It has to be written in Java, because of yet another obsession of corporate world with Java. The API that I had to use was javish, and although people who wrote it, would claim that API can be used easily in any language, it was not so. Whats more, I had to use that API from rails, so you can understand my situation.

Well, So i googled and found the book for “Java Network Programming, Third Edition”.
Java Network Programming

What a crap. My main issues were:

  • I hate a book, which pretends that its examples are ready to run, but they don’t run, because they need tinkering. I am all for snippets, that demonstrate a thing or two. But when you are saying, ok this example is ready to run, with import and everything in place and It doesn’t run on actual machine, it just freaks me out.
  • Although the book has 776 pages, its surprisingly free from useful stuff. Seriously, when I compare this with book by Richard Stevens, this book looks like shit. Although author had more scope here on how to write REALLY scalable networking applications. He wasted 776 pages, talking nothing and perhaps reserved good stuff for “Advanced Network Programming with Java” ( Soon, after reading the book, I found that indeed there is a book on Advanced Network Programming )

Wierd problem with IE6,Content Encoding and Flash in Rails

Our flash guy, uses usual loadVars for loading external data in a flash movie and do some funky stuff like plotting of nice looking portfolio charts.
Portfolio Chart

But somehow, we saw some issues with loading of charts in IE6 running flash. A quick ethreal packet sniffing showed us, that although client is making
request with “Accept-Encoding: gzip,deflate”, its not able to decode the gzip response of web server. And parsing of gzipped response at flash obviously fails.

This is quite a corner case, I suppose, but I did this, to prevent Content-Encoding for that particular controller.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class OutputCompressionFilter
  def self.filter(controller)
    controller.response.headers['Content-Encoding'] = 'identity'
  end
end
 
class FlashController < ApplicationController
  no_pref true
  after_filter OutputCompressionFilter
  layout :set_layout
  include REXML
  def index
  end
end

New release of BackgrounDRb available now

Assuming nobody is reading this, I would quietly mention that, I released new version of BackgrounDRb plugin today.
Checkout full announcement here:

http://rubyforge.org/pipermail/backgroundrb-devel/2007-November/001043.html

Here is a sample worker:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class FooWorker < BackgrounDRb::MetaWorker
  set_worker_name :foo_worker
  attr_accessor :count
 
  def create
    puts "Starting Foo Worker"
    @count = 0
    add_periodic_timer(4) { increment_status}
  end
 
  def process_request p_data
    user_input = p_data[:data]
    result = self.send(user_input[:method],user_input[:data])
    send_response(p_data,result)
  end
 
  def increment_status
    puts "Registering status"
    register_status("stuff #{rand(10)}")
  end
 
  def foobar
    puts "Invoking foobar at #{Time.now}"
  end
 
  def add_values user_input
    p user_input
    return eval(user_input)
  end
end
 
=begin
  problems, with existing things.
=end

Beware of raising exceptions

I was reading on twitter experience of scaling rails and use of dtrace. Unfortunately for GNU/Linux users “dtrace” is just a unreality. But if you just concentrate on the results, then you will find that, a major source of bottleneck was use of:

  foo = @someobj.amazing rescue "not_amazing"

Above code is in vogue, because it takes care of a lot of things like:

  • if @someobj is nil, then foo defaults to “not_amazing”.
  • if @someobj doesn’t support “amazing” method, foo defaults to “not_amazing”.
  • And of course the obvious case.

But that code succinctness comes at a cost and you may end up with deep stacktraces. This was a major bottleneck for twitter team.
Above snippet can be written as:

foo = @someobject.blank? ? "not_amazing" : (@someobject.respont_do?(:amazing) ? @someobject.amazing : "amazing")

Not so succinct, but does its job.

Read the details http://blogs.sun.com/ahl/entry/dtrace_for_ruby_at_oscon .