memcache imlementation of D7 locking

yhager - October 6, 2009 - 05:39
Project:Memcache API and Integration
Version:6.x-1.x-dev
Component:Code
Category:feature request
Priority:normal
Assigned:Unassigned
Status:needs work
Description

I've written this for version 5 (don't ask...) - but the code is version agnostic anyway.

#239798: Provide an interface for add would have been useful here...

AttachmentSize
memcache-lock.inc_.txt3.69 KB

#1

chx - December 3, 2009 - 15:04

You have a race window between get and set. You must use add to begin with. That's atomic.

#2

yhager - December 3, 2009 - 21:51

but I only set if I have the lock, and I assume those two lines of code would take less than a full second.

#3

chx - December 6, 2009 - 19:10

A full second! Some get thirty page loads a second...

#4

yhager - December 6, 2009 - 20:07
Status:needs review» needs work

You mean I got that condition backwards. Good catch!

How about if I change it to:

<?php
     
// renew if we have at least 1 second more. This is required to ensure atomic renew.
     
$success = ($result->lock_expire > $now + 1) && (bool)$mc->set($name, $lock, FALSE, $timeout);
?>

#5

chx - December 6, 2009 - 20:27

set is not atomic. add is. Edit: sorry I mean, set is not what you want. Two processes can run set and succeed. But only one add can succeed.

 
 

Drupal is a registered trademark of Dries Buytaert.