| 1 | package ShardedKV::Storage::Rest; | ||
| 2 | |||
| 3 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use Moose; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use Hijk; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 6 | use URI; | ||
| 7 | use Socket; | ||
| 8 | |||
| 9 | our $VERSION = '0.7'; | ||
| 10 | |||
| 11 | with 'ShardedKV::Storage'; | ||
| 12 | |||
| 13 | has 'url' => ( | ||
| 14 | is => 'ro', | ||
| 15 | isa => 'Str', | ||
| 16 | required => 1 | ||
| 17 | ); | ||
| 18 | |||
| 19 | has 'basepath' => ( | ||
| 20 | is => 'ro', | ||
| 21 | isa => 'Str', | ||
| 22 | required => 0, | ||
| 23 | default => sub { my $self = shift; | ||
| 24 | my $uri = URI->new($self->url); | ||
| 25 | return $uri->path }, | ||
| 26 | ); | ||
| 27 | |||
| 28 | has 'connect_timeout' => ( | ||
| 29 | is => 'ro', | ||
| 30 | isa => 'Num', | ||
| 31 | required => 0, | ||
| 32 | default => 2, # seconds | ||
| 33 | ); | ||
| 34 | |||
| 35 | has 'read_timeout' => ( | ||
| 36 | is => 'ro', | ||
| 37 | isa => 'Num', | ||
| 38 | required => 0, | ||
| 39 | default => 1, # seconds | ||
| 40 | ); | ||
| 41 | |||
| 42 | sub _send_http_request { | ||
| 43 | my ($self, $type, $key, $body) = @_; | ||
| 44 | |||
| 45 | my $uri = URI->new($self->url . "/" . $key); | ||
| 46 | |||
| 47 | my $response; | ||
| 48 | |||
| 49 | eval { | ||
| 50 | $response = Hijk::request({ | ||
| 51 | socket_cache => {}, # don't cache connections | ||
| 52 | method => $type, | ||
| 53 | port => $uri->port, | ||
| 54 | path => $uri->path, | ||
| 55 | host => $uri->host, | ||
| 56 | read_timeout => $self->read_timeout, | ||
| 57 | connect_timeout => $self->connect_timeout, | ||
| 58 | $body ? ( | ||
| 59 | head => [ "Content-Type" => "application/octet-stream" ], | ||
| 60 | body => $body) | ||
| 61 | : (), | ||
| 62 | }); | ||
| 63 | 1; | ||
| 64 | } or do { | ||
| 65 | warn $@; | ||
| 66 | return; | ||
| 67 | }; | ||
| 68 | return $response; | ||
| 69 | } | ||
| 70 | |||
| 71 | sub get { | ||
| 72 | my ($self, $key) = @_; | ||
| 73 | |||
| 74 | my $response = $self->_send_http_request('GET', $key); | ||
| 75 | my $code = $response->{status}; | ||
| 76 | return unless defined $code; | ||
| 77 | |||
| 78 | if ($code >= 200 && $code < 300) { | ||
| 79 | return $response->{body}; | ||
| 80 | } | ||
| 81 | |||
| 82 | return undef; | ||
| 83 | } | ||
| 84 | |||
| 85 | sub set { | ||
| 86 | my ($self, $key, $value_ref) = @_; | ||
| 87 | |||
| 88 | return unless $value_ref; | ||
| 89 | |||
| 90 | my $response = $self->_send_http_request('PUT', $key, $value_ref); | ||
| 91 | my $code = $response->{status}; | ||
| 92 | return unless defined $code; | ||
| 93 | |||
| 94 | if ($code >= 200 && $code < 300) { | ||
| 95 | return 1; | ||
| 96 | } | ||
| 97 | return 0; | ||
| 98 | } | ||
| 99 | |||
| 100 | sub delete { | ||
| 101 | my ($self, $key) = @_; | ||
| 102 | |||
| 103 | my $response = $self->_send_http_request('DELETE', $key); | ||
| 104 | my $code = $response->{status}; | ||
| 105 | return unless defined $code; | ||
| 106 | |||
| 107 | if ($code >= 200 && $code < 300) { | ||
| 108 | return 1; | ||
| 109 | } | ||
| 110 | return 0; | ||
| 111 | } | ||
| 112 | |||
| 113 | sub reset_connection { | ||
| 114 | # noop -- hijk doesn't cache connections | ||
| 115 | } | ||
| 116 | |||
| 117 | no Moose; | ||
| 118 | |||
| 119 | __PACKAGE__->meta->make_immutable; | ||
| 120 | |||
| 121 | __END__ | ||
| 122 | |||
| 123 | =head1 NAME | ||
| 124 | |||
| 125 | ShardedKV::Storage::Rest - rest backend for ShardedKV | ||
| 126 | |||
| 127 | =head1 SYNOPSIS | ||
| 128 | |||
| 129 | use ShardedKV; | ||
| 130 | use ShardedKV::Storage::Rest; | ||
| 131 | ... create ShardedKV... | ||
| 132 | my $storage = ShardedKV::Storage::Rest->new( | ||
| 133 | url => 'http://localhost:679', | ||
| 134 | ); | ||
| 135 | ... put storage into ShardedKV... | ||
| 136 | |||
| 137 | # values are scalar references to strings | ||
| 138 | $skv->set("foo", 'bar'); | ||
| 139 | my $value_ref = $skv->get("foo"); | ||
| 140 | |||
| 141 | |||
| 142 | =head1 DESCRIPTION | ||
| 143 | |||
| 144 | A C<ShardedKV> storage backend that uses a remote http/rest storage. | ||
| 145 | |||
| 146 | Implements the C<ShardedKV::Storage> role. | ||
| 147 | |||
| 148 | =head1 PUBLIC ATTRIBUTES | ||
| 149 | |||
| 150 | =over 4 | ||
| 151 | |||
| 152 | =head2 url | ||
| 153 | |||
| 154 | A 'http://hostname:port[/basepath]' url string pointing at the http/rest server for this shard. | ||
| 155 | Required. | ||
| 156 | |||
| 157 | =head2 basepath | ||
| 158 | |||
| 159 | The base path part of the url provided at initialization time | ||
| 160 | Read Only | ||
| 161 | |||
| 162 | =back | ||
| 163 | |||
| 164 | =head1 SEE ALSO | ||
| 165 | |||
| 166 | L<ShardedKV> | ||
| 167 | L<ShardedKV::Storage> | ||
| 168 | |||
| 169 | =head1 AUTHORS | ||
| 170 | |||
| 171 | =over 4 | ||
| 172 | |||
| 173 | =item Andrea Guzzo <xant@cpan.org> | ||
| 174 | |||
| 175 | =back | ||
| 176 | |||
| 177 | =cut | ||