| 1 | package IPC::Transit::Remote; | ||
| 2 | $IPC::Transit::Remote::VERSION = '1.171860'; | ||
| 3 | use strict;use warnings; | ||
| 4 | use IPC::Transit; | ||
| 5 | use Data::Dumper; | ||
| 6 | use Sys::Hostname; | ||
| 7 | use HTTP::Async; | ||
| 8 | use HTTP::Request; | ||
| 9 | |||
| 10 | our $debug = 0; | ||
| 11 | |||
| 12 | my $all_possible_mes = eval { | ||
| 13 | $ENV{PATH}="/usr/sbin:/sbin:$ENV{PATH}" unless $ENV{PATH} =~ m|/sbin|; | ||
| 14 | my $hostname = hostname; | ||
| 15 | my $ret = {}; | ||
| 16 | foreach (`ifconfig -a` =~ /inet (\S+) /g) { | ||
| 17 | $ret->{$_} = 1; | ||
| 18 | my $rev = IPC::Transit::cached_dns($_); | ||
| 19 | $ret->{$rev} = 1 if $rev; | ||
| 20 | } | ||
| 21 | $ret->{'127.0.0.1'} = 1; | ||
| 22 | $ret->{localhost} = 1; | ||
| 23 | $ret->{$hostname} = 1; | ||
| 24 | if($ENV{TRANSIT_EXTRA_MES}) { | ||
| 25 | foreach my $me (split ':', $ENV{TRANSIT_EXTRA_MES}) { | ||
| 26 | $ret->{$me} = 1; | ||
| 27 | } | ||
| 28 | } | ||
| 29 | return $ret; | ||
| 30 | }; | ||
| 31 | print STDERR 'IPC::Transit::Remote: $all_possible_mes= '; | ||
| 32 | print STDERR Data::Dumper::Dumper $all_possible_mes; | ||
| 33 | |||
| 34 | { | ||
| 35 | my $ts; | ||
| 36 | sub default_info_callback { | ||
| 37 | my $async = shift; | ||
| 38 | my $destinations = shift; | ||
| 39 | $ts = 0 unless defined $ts; | ||
| 40 | my $time = time; | ||
| 41 | return if $time - 1 < $ts; | ||
| 42 | $ts = $time; | ||
| 43 | my $to_send_count = $async->to_send_count; | ||
| 44 | my $to_return_count = $async->to_return_count; | ||
| 45 | my $in_progress_count = $async->in_progress_count; | ||
| 46 | my $total_count = $async->total_count; | ||
| 47 | my $info = $async->info; | ||
| 48 | #print STDERR "\$to_send_count=$to_send_count \$to_return_count=$to_return_count \$in_progress_count=$in_progress_count \$total_count=$total_count\n\$info=$info\n"; | ||
| 49 | print STDERR "$info\n" if $debug;; | ||
| 50 | while(my($key, $value) = each %{$destinations}) { | ||
| 51 | print STDERR "Dest: $key=" . (scalar @{$value->{messages}}) . "\n" if $debug; | ||
| 52 | } | ||
| 53 | print STDERR Data::Dumper::Dumper $destinations if $debug; | ||
| 54 | } | ||
| 55 | } | ||
| 56 | |||
| 57 | our $config = { | ||
| 58 | port => 9816, | ||
| 59 | path => '/sendMessage', | ||
| 60 | protocol => 'http', | ||
| 61 | remote_timeout => 5, | ||
| 62 | url_callback => undef, | ||
| 63 | proxy_callback => undef, | ||
| 64 | info_callback => \&default_info_callback, | ||
| 65 | transit_sending_host => hostname, | ||
| 66 | handle_interval_interval => 1, | ||
| 67 | max_return_message_bytes => 10000000, | ||
| 68 | http_async_constructor_args => { | ||
| 69 | ssl_options => { SSL_verify_mode => 'SSL_VERIFY_NONE' }, | ||
| 70 | }, | ||
| 71 | is_me => sub { | ||
| 72 | my $dest = shift; | ||
| 73 | return undef unless $dest; | ||
| 74 | return $all_possible_mes->{$dest}; | ||
| 75 | }, | ||
| 76 | }; | ||
| 77 | |||
| 78 | sub handle_raw_messages { | ||
| 79 | my $ref = shift; | ||
| 80 | #the actual incoming data is in $ref->{serialized_wire_data} | ||
| 81 | #we do it indirectly because that might be very big, and we don't want | ||
| 82 | #to copy it around | ||
| 83 | my $message_ct = 0; | ||
| 84 | my $total_length = length $ref->{serialized_wire_data}; | ||
| 85 | while(my $new = get_next_raw_message($ref)) { | ||
| 86 | $message_ct++; | ||
| 87 | my $send_to_qname = 'transitd'; | ||
| 88 | if($config->{is_me}->($new->{wire_headers}->{d})) { | ||
| 89 | $send_to_qname = $new->{wire_headers}->{q}; | ||
| 90 | } | ||
| 91 | my $to_queue = IPC::Transit::Internal::_initialize_queue(qname => $send_to_qname); | ||
| 92 | my $ret = $to_queue->snd(1,$new->{serialized_wire_data}, IPC::Transit::Internal::_get_flags('nonblock')); | ||
| 93 | #what to do if this fails? Good question. | ||
| 94 | #Maybe we should fail the whole operation, if we're called over http. | ||
| 95 | #Maybe we can call this in blocking mode, but then we have to be | ||
| 96 | #careful about timing out so we don't fill up with http processes. | ||
| 97 | #XXX consider | ||
| 98 | |||
| 99 | #Also we need to think about implementing arge message support again | ||
| 100 | #on this side. | ||
| 101 | } | ||
| 102 | return ($message_ct, $total_length) | ||
| 103 | } | ||
| 104 | |||
| 105 | sub get_proxy_send { | ||
| 106 | my $transit_sending_host = shift; | ||
| 107 | |||
| 108 | #common case: we have no messages to proxy send, so we return | ||
| 109 | #nothing | ||
| 110 | return '' unless IPC::Transit::queue_exists($transit_sending_host); | ||
| 111 | |||
| 112 | my $return_messages = ''; | ||
| 113 | |||
| 114 | my $from_queue = IPC::Transit::Internal::_initialize_queue(qname => $transit_sending_host); | ||
| 115 | my $flags = IPC::Transit::Internal::_get_flags('nowait'); | ||
| 116 | while(1) { | ||
| 117 | $from_queue->rcv(my $serialized_wire_data, 102400000, 0, $flags); | ||
| 118 | last unless $serialized_wire_data; | ||
| 119 | my $ref = { | ||
| 120 | serialized_wire_data => $serialized_wire_data | ||
| 121 | }; | ||
| 122 | my ($header_length, $wire_headers) = IPC::Transit::_parse_wire_header($ref); | ||
| 123 | IPC::Transit::sync_serialized_wire_data($wire_headers, $ref); | ||
| 124 | $return_messages .= $ref->{serialized_wire_data}; | ||
| 125 | last if length $return_messages > $config->{max_return_message_bytes}; | ||
| 126 | } | ||
| 127 | return $return_messages; | ||
| 128 | } | ||
| 129 | |||
| 130 | sub get_next_raw_message { | ||
| 131 | my $ref = shift; | ||
| 132 | return undef unless $ref->{serialized_wire_data}; | ||
| 133 | my $new = { | ||
| 134 | header_length => 0, | ||
| 135 | wire_headers => {}, | ||
| 136 | serialized_wire_data => 'abc', | ||
| 137 | }; | ||
| 138 | ($new->{header_length}, $new->{wire_headers}) = IPC::Transit::_parse_wire_header($ref); | ||
| 139 | return undef unless $new->{wire_headers}; | ||
| 140 | $new->{serialized_wire_data} = substr( | ||
| 141 | $ref->{serialized_wire_data}, | ||
| 142 | 0, | ||
| 143 | $new->{header_length} + length($new->{header_length}) + 1 + $new->{wire_headers}->{l}, | ||
| 144 | '' | ||
| 145 | ); | ||
| 146 | #print STDERR Data::Dumper::Dumper $new; | ||
| 147 | return $new; | ||
| 148 | } | ||
| 149 | |||
| 150 | sub _post_proxy { | ||
| 151 | my $dest = shift; | ||
| 152 | my $messages = shift; | ||
| 153 | #I believe all we need to do here is put them on a qname = $dest | ||
| 154 | #messages is an array REF of raw messages, serialized_wire_data | ||
| 155 | #if we return false here, then the messages will stay in the parent | ||
| 156 | #array and be tried again later | ||
| 157 | my $to_queue = IPC::Transit::Internal::_initialize_queue(qname => $dest); | ||
| 158 | foreach my $serialized_wire_data (@{$messages}) { | ||
| 159 | $to_queue->snd(1,$serialized_wire_data, IPC::Transit::Internal::_get_flags('nonblock')); | ||
| 160 | } | ||
| 161 | #for now, let's never fail | ||
| 162 | return 1; | ||
| 163 | } | ||
| 164 | |||
| 165 | |||
| 166 | { | ||
| 167 | my $destinations; | ||
| 168 | my $async; | ||
| 169 | my $ct; | ||
| 170 | sub _setup_async { | ||
| 171 | if(not $async) { | ||
| 172 | $ct = 0; | ||
| 173 | $async = HTTP::Async->new( | ||
| 174 | %{$config->{http_async_constructor_args}} | ||
| 175 | ); | ||
| 176 | $async->timeout($config->{remote_timeout}); | ||
| 177 | } | ||
| 178 | $ct++; | ||
| 179 | if($ct > 500000) { | ||
| 180 | print STDERR "IPC::Transit::Remote::_setup_async: resetting HTTP::Async object after 500000 uses\n"; | ||
| 181 | undef $async; | ||
| 182 | $ct = 1; | ||
| 183 | } | ||
| 184 | } | ||
| 185 | sub get_destinations { | ||
| 186 | return $destinations; | ||
| 187 | } | ||
| 188 | sub _init_dest { | ||
| 189 | my $dest = shift; | ||
| 190 | _setup_async(); | ||
| 191 | $destinations = {} unless $destinations; | ||
| 192 | $destinations->{$dest} = { | ||
| 193 | messages => [], | ||
| 194 | total_send_ct => 0, | ||
| 195 | total_byte_sent_ct => 0, | ||
| 196 | codes => {}, | ||
| 197 | } unless $destinations->{$dest}; | ||
| 198 | } | ||
| 199 | sub _set_code_stats { | ||
| 200 | my $dest = shift; | ||
| 201 | my $response = shift; | ||
| 202 | _setup_async(); | ||
| 203 | my $code = $response->code; | ||
| 204 | _init_dest($dest); | ||
| 205 | $destinations->{$dest}->{codes}->{$code} = { | ||
| 206 | ct => 0, | ||
| 207 | last_status_line => '', | ||
| 208 | } unless $destinations->{$dest}->{codes}->{$code}; | ||
| 209 | $destinations->{$dest}->{codes}->{$code}->{ct}++; | ||
| 210 | $destinations->{$dest}->{codes}->{$code}->{last_status_line} = $response->status_line; | ||
| 211 | } | ||
| 212 | sub add_to_destination { | ||
| 213 | my ($dest, $ref) = @_; | ||
| 214 | _setup_async(); | ||
| 215 | #print STDERR "remote-transitd: Adding message to destination $dest\n"; | ||
| 216 | $config->{info_callback}->($async, $destinations) | ||
| 217 | if $config->{info_callback}; | ||
| 218 | _init_dest($dest); | ||
| 219 | push @{$destinations->{$dest}->{messages}}, $ref->{serialized_wire_data}; | ||
| 220 | } | ||
| 221 | { | ||
| 222 | my $ts; | ||
| 223 | sub handle_interval { | ||
| 224 | _setup_async(); | ||
| 225 | $config->{info_callback}->($async, $destinations) | ||
| 226 | if $config->{info_callback}; | ||
| 227 | if($async->not_empty) { | ||
| 228 | while(my $response = $async->next_response) { | ||
| 229 | #$response->content contains all of the messages returned by | ||
| 230 | #proxy, if any | ||
| 231 | my $dest = $response->{_request}->header('TRANSIT_DESTINATION_HOST'); | ||
| 232 | _set_code_stats($dest, $response); | ||
| 233 | if(not $response->is_success) { | ||
| 234 | next; | ||
| 235 | } | ||
| 236 | my $ref = { | ||
| 237 | serialized_wire_data => $response->content | ||
| 238 | }; | ||
| 239 | #for now, handle the old (and grossly invalid) messages right away | ||
| 240 | if( $ref->{serialized_wire_data} and | ||
| 241 | $ref->{serialized_wire_data} !~ /^\d+/) { | ||
| 242 | print STDERR 'IPC::Transit::Remote::handle_interval: received a grossly mis-formatted message, these are the first 40 characters: ' . substr($ref->{serialized_wire_data}, 0, 40) . "\n"; | ||
| 243 | next; | ||
| 244 | } | ||
| 245 | eval { | ||
| 246 | my ($message_ct, $total_length) = IPC::Transit::Remote::handle_raw_messages($ref); | ||
| 247 | }; | ||
| 248 | if($@) { | ||
| 249 | if($ref->{serialized_wire_data}) { | ||
| 250 | print STDERR "IPC::Transit::Remote::handle_interval failed: $@: $ref->{serialized_wire_data}\n"; | ||
| 251 | } else { | ||
| 252 | print STDERR "IPC::Transit::Remote::handle_interval failed: $@: <undef>\n"; | ||
| 253 | } | ||
| 254 | } | ||
| 255 | } | ||
| 256 | } | ||
| 257 | my $time = time; | ||
| 258 | $ts = $time unless defined $ts; | ||
| 259 | return if $time - $config->{handle_interval_interval} < $ts; | ||
| 260 | $ts = $time; | ||
| 261 | foreach my $dest (keys %{$destinations}) { | ||
| 262 | my $message_ct = scalar @{$destinations->{$dest}->{messages}}; | ||
| 263 | next unless $message_ct; | ||
| 264 | if($config->{proxy_callback} and $config->{proxy_callback}->($dest)) { | ||
| 265 | if(_post_proxy($dest, $destinations->{$dest}->{messages})) { | ||
| 266 | $destinations->{$dest}->{messages} = []; | ||
| 267 | } | ||
| 268 | next; | ||
| 269 | } | ||
| 270 | my $all_messages = join '', @{$destinations->{$dest}->{messages}}; | ||
| 271 | my $url = "$config->{protocol}://$dest:$config->{port}$config->{path}"; | ||
| 272 | $url = $config->{url_callback}->($dest) | ||
| 273 | if $config->{url_callback}; | ||
| 274 | $destinations->{$dest}->{messages} = []; | ||
| 275 | $async->add( | ||
| 276 | HTTP::Request->new( | ||
| 277 | POST => $url, | ||
| 278 | [ 'TRANSIT_SENDING_HOST' => $config->{transit_sending_host}, | ||
| 279 | 'TRANSIT_DESTINATION_HOST' => $dest, | ||
| 280 | ], | ||
| 281 | $all_messages | ||
| 282 | ) | ||
| 283 | ); | ||
| 284 | $destinations->{$dest}->{total_send_ct} += $message_ct; | ||
| 285 | $destinations->{$dest}->{total_byte_sent_ct} += length $all_messages; | ||
| 286 | #we need logic or something here (and elsewhere) to handle the case | ||
| 287 | #where the request fails. | ||
| 288 | #this means that while there are pending requests, we should probably | ||
| 289 | #not do additional requests, so that if the request fails, we will | ||
| 290 | #be able to put the data back. | ||
| 291 | #or better yet, don't take the data from the array until the request | ||
| 292 | #comes back successfully. | ||
| 293 | } | ||
| 294 | } | ||
| 295 | } | ||
| 296 | } | ||
| 297 | |||
| 298 | 1; |