示例#1
0
文件: lock.py 项目: AHAMED750/reddit
    def acquire(self):
        start = datetime.now()

        self.nonce = (reddit_host, reddit_pid, simple_traceback(limit=7))

        # if this thread already has this lock, move on
        if self.key in self.locks:
            self.have_lock = True
            return

        timer = self.stats.get_timer("lock_wait")
        timer.start()

        # try and fetch the lock, looping until it's available
        lock = None
        while not lock:
            # catch all exceptions here because we can't trust the memcached
            # protocol. The add for the lock may have actually succeeded.
            try:
                lock = self.cache.add(self.key, self.nonce, time = self.time)
            except MemcachedError as e:
                if self.cache.get(self.key) == self.nonce:
                    g.log.error(
                        'Memcached add succeeded, but threw an exception for key %r %s',
                        self.key, e)
                    break

            if not lock:
                if (datetime.now() - start).seconds > self.timeout:
                    if self.verbose:
                        info = self.cache.get(self.key)
                        if info:
                            info = "%s %s\n%s" % info
                        else:
                            info = "(nonexistent)"
                        msg = ("\nSome jerk is hogging %s:\n%s" %
                                         (self.key, info))
                        msg += "^^^ that was the stack trace of the lock hog, not me."
                    else:
                        msg = "Timed out waiting for %s" % self.key
                    raise TimeoutExpired(msg)
                else:
                    # this should prevent unnecessary spam on highly contended locks.
                    sleep(random.uniform(0.1, 1))

        timer.stop(subname=self.group)

        self.owns_lock = True
        self.have_lock = True

        # tell this thread we have this lock so we can avoid deadlocks
        # of requests for the same lock in the same thread
        self.locks.add(self.key)
示例#2
0
def add_request_info(select):
    def sanitize(txt):
        return "".join(x if x.isalnum() else "." for x in filters._force_utf8(txt))

    tb = simple_traceback(limit=12)
    try:
        if hasattr(request, "path") and hasattr(request, "ip") and hasattr(request, "user_agent"):
            comment = "/*\n%s\n%s\n%s\n*/" % (tb or "", sanitize(request.fullpath), sanitize(request.ip))
            return select.prefix_with(comment)
    except UnicodeDecodeError:
        pass

    return select
示例#3
0
def add_request_info(select):
    def sanitize(txt):
        return _spaces.sub(' ', txt).replace("/", "|").replace("-", "_").replace(';', "").replace("*", "").replace(r"/", "")

    tb = simple_traceback(limit=12)
    try:
        if (hasattr(request, 'path') and
            hasattr(request, 'ip') and
            hasattr(request, 'user_agent')):
            comment = '/*\n%s\n%s\n%s\n*/' % (
                tb or "", 
                filters._force_utf8(sanitize(request.fullpath)),
                sanitize(request.ip))
            return select.prefix_with(comment)
    except UnicodeDecodeError:
        pass

    return select
示例#4
0
def add_request_info(select):
    def sanitize(txt):
        return "".join(x if x.isalnum() else "."
                       for x in filters._force_utf8(txt))

    tb = simple_traceback(limit=12)
    try:
        if (hasattr(request, 'path') and
            hasattr(request, 'ip') and
            hasattr(request, 'user_agent')):
            comment = '/*\n%s\n%s\n%s\n*/' % (
                tb or "", 
                sanitize(request.fullpath),
                sanitize(request.ip))
            return select.prefix_with(comment)
    except UnicodeDecodeError:
        pass

    return select
示例#5
0
文件: lock.py 项目: Anenome/reddit
    def __enter__(self):
        start = datetime.now()

        my_info = (reddit_host, reddit_pid, simple_traceback())

        #if this thread already has this lock, move on
        if self.key in self.locks:
            return

        timer = self.stats.get_timer("lock_wait")
        timer.start()

        #try and fetch the lock, looping until it's available
        while not self.cache.add(self.key, my_info, time = self.time):
            if (datetime.now() - start).seconds > self.timeout:
                if self.verbose:
                    info = self.cache.get(self.key)
                    if info:
                        info = "%s %s\n%s" % info
                    else:
                        info = "(nonexistent)"
                    msg = ("\nSome jerk is hogging %s:\n%s" %
                                         (self.key, info))
                    msg += "^^^ that was the stack trace of the lock hog, not me."
                else:
                    msg = "Timed out waiting for %s" % self.key
                raise TimeoutExpired(msg)

            sleep(.01)

        timer.stop(subname=self.group)

        #tell this thread we have this lock so we can avoid deadlocks
        #of requests for the same lock in the same thread
        self.locks.add(self.key)
        self.have_lock = True
示例#6
0
    def acquire(self):
        start = datetime.now()

        my_info = (reddit_host, reddit_pid, simple_traceback(limit=7))

        #if this thread already has this lock, move on
        if self.key in self.locks:
            return

        timer = self.stats.get_timer("lock_wait")
        timer.start()

        #try and fetch the lock, looping until it's available
        while not self.cache.add(self.key, my_info, time = self.time):
            if (datetime.now() - start).seconds > self.timeout:
                if self.verbose:
                    info = self.cache.get(self.key)
                    if info:
                        info = "%s %s\n%s" % info
                    else:
                        info = "(nonexistent)"
                    msg = ("\nSome jerk is hogging %s:\n%s" %
                                         (self.key, info))
                    msg += "^^^ that was the stack trace of the lock hog, not me."
                else:
                    msg = "Timed out waiting for %s" % self.key
                raise TimeoutExpired(msg)

            sleep(.01)

        timer.stop(subname=self.group)

        #tell this thread we have this lock so we can avoid deadlocks
        #of requests for the same lock in the same thread
        self.locks.add(self.key)
        self.have_lock = True