stream {
  upstream dynamic_server0 {
    server 127.0.0.1:58080;
  }
  upstream dynamic_server1 {
    server 127.0.0.1:58081;
  }

  mruby_stream_init_code '
    p "ngx_mruby: STREAM: mruby_stream_init_code"
    Userdata.new.new_upstream = "127.0.0.1:58081"
  ';

  mruby_stream_init_worker_code '
    p "ngx_mruby: STREAM: mruby_stream_init_worker_code"
  ';

  mruby_stream_exit_worker_code '
    p "ngx_mruby: STREAM: mruby_stream_exit_worker_code"
  ';

  server {
      listen 12346;
      mruby_stream_code '
        c = Nginx::Stream::Connection.new "dynamic_server0"
        # get from mruby_stream_init
        c.upstream_server = Userdata.new.new_upstream
        Nginx::Stream.log Nginx::Stream::LOG_NOTICE, "mruby_stream"
      ';
      proxy_pass dynamic_server0;
  }

  # test for dynamic tcp load balancer
  # upstream changed from 127.0.0.1:58081 to 127.0.0.1:58080 in mruby
  server {
      listen 12345;
      # cached only
      mruby_stream build/nginx/html/stream_lb.rb;
      proxy_pass dynamic_server1;
  }
  server {
      listen 12347;
      mruby_stream_code '
        if Nginx::Stream::Connection.remote_ip == "127.0.0.1"
          current_status = Nginx::Stream::Connection.stream_status
          Nginx::Stream::Connection.stream_status = Nginx::Stream::ABORT
          Nginx::Stream.log Nginx::Stream::LOG_NOTICE, "current status=#{(current_status == Nginx::Stream::DECLINED) ? "NGX_DECLINED" : current_status} but deny from #{Nginx::Stream::Connection.remote_ip} return NGX_ABORT"
        end
      ';
      proxy_pass dynamic_server1;
  }
}

