AdoccaMemcache 0.1.4 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +1 -1
- data/lib/am_memcache.rb +64 -9
- metadata +2 -2
data/Rakefile
CHANGED
@@ -4,7 +4,7 @@ require 'rubygems'
|
|
4
4
|
require 'hoe'
|
5
5
|
require './lib/adocca_memcache.rb'
|
6
6
|
|
7
|
-
Hoe.new('AdoccaMemcache', '0.1.
|
7
|
+
Hoe.new('AdoccaMemcache', '0.1.5') do |p|
|
8
8
|
p.rubyforge_name = 'adocca-plugins'
|
9
9
|
p.author = 'adocca Entertainment AB'
|
10
10
|
p.summary = 'A client library to simplify using memcached with Ruby on Rails projects.'
|
data/lib/am_memcache.rb
CHANGED
@@ -43,6 +43,9 @@ module Adocca
|
|
43
43
|
# Default memcached server weight.
|
44
44
|
DEFAULT_WEIGHT = 1
|
45
45
|
|
46
|
+
# Default interval between checking server liveliness
|
47
|
+
VALIDATION_INTERVAL = 30
|
48
|
+
|
46
49
|
# The amount of time to wait for a response from a memcached server. If a
|
47
50
|
# response is not completed within this time, the connection to the server
|
48
51
|
# will be closed and an error will be raised.
|
@@ -69,6 +72,28 @@ module Adocca
|
|
69
72
|
end
|
70
73
|
@servers = []
|
71
74
|
@buckets = []
|
75
|
+
start_validation
|
76
|
+
end
|
77
|
+
|
78
|
+
def start_validation
|
79
|
+
Thread.new do
|
80
|
+
loop do
|
81
|
+
begin
|
82
|
+
@servers.each do |server|
|
83
|
+
server.check_liveliness if server.alive?
|
84
|
+
end
|
85
|
+
sleep VALIDATION_INTERVAL
|
86
|
+
rescue Exception => e
|
87
|
+
if defined?(RAILS_DEFAULT_LOGGER)
|
88
|
+
RAILS_DEFAULT_LOGGER.error("validation got #{e}")
|
89
|
+
RAILS_DEFAULT_LOGGER.error(PP.pp(e.backtrace, ""))
|
90
|
+
else
|
91
|
+
puts e
|
92
|
+
pp e.backtrace
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
72
97
|
end
|
73
98
|
|
74
99
|
# Return a string representation of the cache object.
|
@@ -129,7 +154,7 @@ module Adocca
|
|
129
154
|
sock.gets
|
130
155
|
sock.gets
|
131
156
|
rescue SystemCallError, IOError => err
|
132
|
-
server.
|
157
|
+
server.mark_dead
|
133
158
|
raise MemCacheError, err.message
|
134
159
|
end
|
135
160
|
end
|
@@ -246,7 +271,7 @@ module Adocca
|
|
246
271
|
sock.gets
|
247
272
|
sock.gets
|
248
273
|
rescue SystemCallError, IOError => err
|
249
|
-
server.
|
274
|
+
server.mark_dead
|
250
275
|
raise MemCacheError, err.message
|
251
276
|
end
|
252
277
|
|
@@ -269,7 +294,7 @@ module Adocca
|
|
269
294
|
sock.write "flush_all\r\n"
|
270
295
|
sock.gets
|
271
296
|
rescue SystemCallError, IOError => err
|
272
|
-
server.
|
297
|
+
server.mark_dead
|
273
298
|
raise MemCacheError, err.message
|
274
299
|
end
|
275
300
|
end
|
@@ -346,11 +371,15 @@ module Adocca
|
|
346
371
|
# The amount of time to wait to establish a connection with a
|
347
372
|
# memcached server. If a connection cannot be established within
|
348
373
|
# this time limit, the server will be marked as down.
|
349
|
-
CONNECT_TIMEOUT =
|
374
|
+
CONNECT_TIMEOUT = 1
|
350
375
|
|
351
376
|
# The amount of time to wait before attempting to re-establish a
|
352
377
|
# connection with a server that is marked dead.
|
353
|
-
RETRY_DELAY =
|
378
|
+
RETRY_DELAY = 10
|
379
|
+
|
380
|
+
# Default timeout for getting all the necessary info from the server when
|
381
|
+
# checking liveliness
|
382
|
+
LIVELINESS_TIMEOUT = 5
|
354
383
|
|
355
384
|
# The host the memcached server is running on.
|
356
385
|
attr_reader :host
|
@@ -378,6 +407,7 @@ module Adocca
|
|
378
407
|
|
379
408
|
@host = host
|
380
409
|
@port = port.to_i
|
410
|
+
@id = Integer("0x#{Digest::SHA1.hexdigest("#{host}:#{port}")}")
|
381
411
|
@weight = weight.to_i
|
382
412
|
|
383
413
|
@sock = nil
|
@@ -385,6 +415,34 @@ module Adocca
|
|
385
415
|
@status = "NOT CONNECTED"
|
386
416
|
end
|
387
417
|
|
418
|
+
#
|
419
|
+
# Will make sure the server works and mark_dead if not.
|
420
|
+
#
|
421
|
+
def check_liveliness
|
422
|
+
key = rand(1 << 32).to_s
|
423
|
+
value = rand(1 << 32).to_s
|
424
|
+
begin
|
425
|
+
timeout(LIVELINESS_TIMEOUT) do
|
426
|
+
socket.write("set am_memcache_check_liveliness_#{key} 0 #{LIVELINESS_TIMEOUT * 2} #{value.size}\r\n" + value + "\r\n")
|
427
|
+
raise 'bad response to set' unless @sock.gets == "STORED\r\n"
|
428
|
+
socket.write("get am_memcache_check_liveliness_#{key}\r\n")
|
429
|
+
v, cache_key, flags, bytes = @sock.gets.split(/ /)
|
430
|
+
raise 'bad response to get' unless @sock.read(bytes.to_i) == value
|
431
|
+
socket.gets
|
432
|
+
socket.gets
|
433
|
+
end
|
434
|
+
rescue Exception => e
|
435
|
+
mark_dead
|
436
|
+
if defined?(RAILS_DEFAULT_LOGGER)
|
437
|
+
RAILS_DEFAULT_LOGGER.error("liveliness check got #{e}")
|
438
|
+
RAILS_DEFAULT_LOGGER.error(PP.pp(e.backtrace, ""))
|
439
|
+
else
|
440
|
+
puts e
|
441
|
+
pp e.backtrace
|
442
|
+
end
|
443
|
+
end
|
444
|
+
end
|
445
|
+
|
388
446
|
# Return a string representation of the server object.
|
389
447
|
def inspect
|
390
448
|
sprintf("<MemCache::Server: %s:%d [%d] (%s)>",
|
@@ -500,7 +558,7 @@ module Adocca
|
|
500
558
|
sock.write "#{command}\r\n"
|
501
559
|
[sock.gets, sock, server]
|
502
560
|
rescue SystemCallError, IOError => err
|
503
|
-
server.
|
561
|
+
server.mark_dead
|
504
562
|
raise MemCacheError, err.message
|
505
563
|
end
|
506
564
|
end
|
@@ -554,9 +612,6 @@ module Adocca
|
|
554
612
|
|
555
613
|
# Pick a server to handle the request based on a hash of the key.
|
556
614
|
def get_server_for_key(key)
|
557
|
-
# Easy enough if there is only one server.
|
558
|
-
return @servers.first if @servers.length == 1
|
559
|
-
|
560
615
|
# Hash the value of the key to select the bucket.
|
561
616
|
hkey = Integer("0x#{key}")
|
562
617
|
|
metadata
CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.11
|
|
3
3
|
specification_version: 1
|
4
4
|
name: AdoccaMemcache
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.1.
|
7
|
-
date: 2007-
|
6
|
+
version: 0.1.5
|
7
|
+
date: 2007-02-12 00:00:00 +01:00
|
8
8
|
summary: A client library to simplify using memcached with Ruby on Rails projects.
|
9
9
|
require_paths:
|
10
10
|
- lib
|