Skip to content

Commit

Permalink
Remove reproxying
Browse files Browse the repository at this point in the history
This is part of a series, but this simplifies some later things. This
removes "reproxying" from the codebase. Since we're recommending people
use Varnish, Cloudflare, or some other caching system, there's not a lot
of performance gain by reproxying. Particularly with how fast computers
are these days.
  • Loading branch information
zorkian committed Jun 27, 2016
1 parent d1324f4 commit c56dc03
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 111 deletions.
121 changes: 21 additions & 100 deletions cgi-bin/Apache/LiveJournal.pm
Expand Up @@ -1042,67 +1042,25 @@ sub userpic_trans
return OK;
}

sub userpic_content
{
my $apache_r = shift;
my $file = $apache_r->filename;

my $picid = $RQ{'picid'};
my $userid = $RQ{'pic-userid'}+0;

my ($data, $lastmod);

my $mime = "image/jpeg";
my $set_mime = sub {
my $data = shift;
if ($data =~ /^GIF/) { $mime = "image/gif"; }
elsif ($data =~ /^\x89PNG/) { $mime = "image/png"; }
};
my $size;

my $send_headers = sub {
$size = $_[0] if @_;
$size ||= 0;
$apache_r->content_type( $mime );
$apache_r->headers_out->{"Content-length"} = $size + 0;
$apache_r->headers_out->{"Cache-Control"} = "no-transform";
$apache_r->headers_out->{"Last-Modified"} = LJ::time_to_http($lastmod);
};
sub userpic_content {
my $apache_r = $_[0];

# Load the user object and pic and make sure the picture is viewable
my $u = LJ::load_userid($userid);
my $pic = LJ::Userpic->get( $u, $picid, { no_expunged => 1 } )
my $u = LJ::load_userid( $RQ{'pic-userid'} );
my $pic = LJ::Userpic->get( $u, $RQ{'picid'}, { no_expunged => 1 } )
or return NOT_FOUND;

# Read the mimetype from the pichash if dversion 7
$mime = $pic->mimetype;

### Handle reproxyable requests

# For dversion 7+ and mogilefs userpics, follow this path
if ( $pic->in_mogile ) {
my $key = $u->mogfs_userpic_key( $picid );
my $memkey = [$picid, "mogp.up.$picid"];
mogile_fetch( $apache_r, $key, $memkey, 'userpics', $send_headers );
return OK;
}

# else, get it from db.
unless ($data) {
$lastmod = $pic->pictime;

my $dbb = LJ::get_cluster_reader( $u );
return SERVER_ERROR unless $dbb;
$data = $dbb->selectrow_array( "SELECT imagedata FROM userpicblob2 WHERE " .
"userid=$userid AND picid=$picid" );
}

# Must have contents by now, or return 404
my $data = $pic->imagedata;
return NOT_FOUND unless $data;

$set_mime->($data);
$size = length($data);
$send_headers->();
$apache_r->print($data) unless $apache_r->header_only;
# Everything looks good, send it
$apache_r->content_type( $pic->mimetype );
$apache_r->headers_out->{"Content-length"} = length $data;
$apache_r->headers_out->{"Cache-Control"} = "no-transform";
$apache_r->headers_out->{"Last-Modified"} = LJ::time_to_http( $pic->pictime );
$apache_r->print( $data )
unless $apache_r->header_only;
return OK;
}

Expand Down Expand Up @@ -1143,9 +1101,8 @@ sub vgift_trans
return OK;
}

sub vgift_content
{
my $apache_r = shift;
sub vgift_content {
my $apache_r = $_[0];
my $picid = $RQ{picid};
my $picsize = $RQ{picsize};

Expand All @@ -1154,7 +1111,6 @@ sub vgift_content
return NOT_FOUND unless $mime;

my $size;

my $send_headers = sub {
$size = $_[0] if @_;
$size ||= 0;
Expand All @@ -1164,8 +1120,12 @@ sub vgift_content
};

my $key = $vg->img_mogkey( $picsize );
my $memkey = $vg->img_memkey( $picsize ); #[$picid, "mogp.vg.$picsize.$picid"];
mogile_fetch( $apache_r, $key, $memkey, 'vgifts', $send_headers );
my $data = LJ::mogclient()->get_file_data( $key );
return NOT_FOUND unless $data;

$send_headers->( length $$data );
$apache_r->print( $$data )
unless $apache_r->header_only;
return OK;
}

Expand Down Expand Up @@ -1454,45 +1414,6 @@ sub interface_content
return OK;
}

sub mogile_fetch {
my ( $apache_r, $key, $memkey, $class, $send_headers ) = @_;

if ( !$LJ::REPROXY_DISABLE{$class} &&
$apache_r->headers_in->{'X-Proxy-Capabilities'} &&
$apache_r->headers_in->{'X-Proxy-Capabilities'} =~ m{\breproxy-file\b}i ) {

my $zone = $apache_r->headers_in->{'X-MogileFS-Explicit-Zone'} || undef;
$memkey->[1] .= ".$zone" if $zone;

my $cache_for = $LJ::MOGILE_PATH_CACHE_TIMEOUT || 3600;

my $paths = LJ::MemCache::get( $memkey );
unless ( $paths ) {
# load and add to memcache
my @paths = LJ::mogclient()->get_paths( $key, { noverify => 1, zone => $zone } );
$paths = \@paths;
LJ::MemCache::add( $memkey, $paths, $cache_for ) if @paths;
}

if ( defined $paths->[0] && $paths->[0] =~ m/^https?:/ ) {
# reproxy url
$apache_r->headers_out->{'X-REPROXY-CACHE-FOR'} = "$cache_for; Last-Modified Content-Type";
$apache_r->headers_out->{'X-REPROXY-URL'} = join( ' ', @$paths );
} else {
# reproxy file
$apache_r->headers_out->{'X-REPROXY-FILE'} = $paths->[0];
}

$send_headers->();

} else { # no reproxy
my $data = LJ::mogclient()->get_file_data( $key );
return NOT_FOUND unless $data;
$send_headers->( length $$data );
$apache_r->print( $$data ) unless $apache_r->header_only;
}
}

package LJ::Protocol;
use Encode();

Expand Down
1 change: 0 additions & 1 deletion cgi-bin/DW/Controller/Media.pm
Expand Up @@ -159,7 +159,6 @@ sub media_handler {
unless $obj->visible_to( LJ::get_remote() );

# load the data for this object
# FIXME: support X-REPROXY headers here
my $dataref = LJ::mogclient()->get_file_data( $obj->mogkey );
return $error_out->( 500, 'Unexpected internal error locating file' )
unless defined $dataref && ref $dataref eq 'SCALAR';
Expand Down
4 changes: 0 additions & 4 deletions cgi-bin/LJ/ConfCheck/General.pm
Expand Up @@ -430,10 +430,6 @@ add_conf('$MEMCACHE_CB_CONNECT_FAIL',
des => "Callback when a connection to a memcached instance fails. Subref gets the IP address that was being connected to, but without the port number."
);

add_conf('%REPROXY_DISABLE',
des => "Set of file classes that shouldn't be internally redirected to mogstored nodes. Values are true, keys are one of 'userpics', or site-local file types like 'phoneposts' for ljcom. Seee also \%USERPIC_REPROXY_DISABLE"
);

add_conf('$SQUAT_URL',
des => "For anti-squatter checking in Apache/LiveJournal.pm."
);
Expand Down
5 changes: 0 additions & 5 deletions cgi-bin/LJ/Global/Defaults.pm
Expand Up @@ -195,10 +195,6 @@ no strict "vars";
$MOGILEFS_CONFIG{classes}->{vgifts} ||= 3;
$MOGILEFS_CONFIG{classes}->{media} ||= 3;

# Default to allow all reproxying.
%REPROXY_DISABLE = () unless %REPROXY_DISABLE;


# detect whether we are running on 32-bit architecture
my $arch = ( length(pack "L!", 0) == 4 ) ? 1 : 0;
if ( defined $ARCH32 ) {
Expand All @@ -208,7 +204,6 @@ no strict "vars";
$ARCH32 = $arch;
}


# setup default minimal style information
$MINIMAL_USERAGENT{$_} ||= 1 foreach qw(Links Lynx w BlackBerry WebTV); # w is for w3m
$MINIMAL_BML_SCHEME ||= 'lynx';
Expand Down
3 changes: 2 additions & 1 deletion cgi-bin/LJ/User/Icons.pm
Expand Up @@ -746,7 +746,8 @@ sub get_userpic_kw_map {
foreach my $keyword ( keys %{$picinfo->{kw}} ) {
my $picid = $picinfo->{kw}->{$keyword}->{picid};
$keywords->{$picid} = [] unless $keywords->{$picid};
push @{$keywords->{$picid}}, $keyword if ( defined( $keyword ) && $picid && $keyword !~ m/^pic\#(\d+)$/ );
push @{$keywords->{$picid}}, $keyword
if defined $keyword && $picid && $keyword !~ m/^pic\#(\d+)$/;
}

return $u->{picid_kw_map} = $keywords;
Expand Down

0 comments on commit c56dc03

Please sign in to comment.