~ubuntu-branches/ubuntu/trusty/swift/trusty-updates

« back to all changes in this revision

Viewing changes to swift/common/utils.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2013-01-28 09:40:30 UTC
  • mfrom: (1.2.16)
  • Revision ID: package-import@ubuntu.com-20130128094030-aetz57x2qz9ye2d4
Tags: 1.7.6-0ubuntu1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
624
624
    """
625
625
 
626
626
    def format(self, record):
 
627
        if not hasattr(record, 'server'):
 
628
            # Catch log messages that were not initiated by swift
 
629
            # (for example, the keystone auth middleware)
 
630
            record.server = record.name
627
631
        msg = logging.Formatter.format(self, record)
628
 
        if (record.txn_id and record.levelno != logging.INFO and
 
632
        if (hasattr(record, 'txn_id') and record.txn_id and
 
633
                record.levelno != logging.INFO and
629
634
                record.txn_id not in msg):
630
635
            msg = "%s (txn: %s)" % (msg, record.txn_id)
631
 
        if (record.client_ip and record.levelno != logging.INFO and
 
636
        if (hasattr(record, 'client_ip') and record.client_ip and
 
637
                record.levelno != logging.INFO and
632
638
                record.client_ip not in msg):
633
639
            msg = "%s (client_ip: %s)" % (msg, record.client_ip)
634
640
        return msg
690
696
            handler = SysLogHandler(address=log_address, facility=facility)
691
697
        except socket.error, e:
692
698
            # Either /dev/log isn't a UNIX socket or it does not exist at all
693
 
            if e.errno not in [errno.ENOTSOCK,  errno.ENOENT]:
 
699
            if e.errno not in [errno.ENOTSOCK, errno.ENOENT]:
694
700
                raise e
695
701
            handler = SysLogHandler(facility=facility)
696
702
    handler.setFormatter(formatter)
745
751
    return adapted_logger
746
752
 
747
753
 
 
754
def get_hub():
 
755
    """
 
756
    Checks whether poll is available and falls back
 
757
    on select if it isn't.
 
758
 
 
759
    Note about epoll:
 
760
 
 
761
    Review: https://review.openstack.org/#/c/18806/
 
762
 
 
763
    There was a problem where once out of every 30 quadrillion
 
764
    connections, a coroutine wouldn't wake up when the client
 
765
    closed its end. Epoll was not reporting the event or it was
 
766
    getting swallowed somewhere. Then when that file descriptor
 
767
    was re-used, eventlet would freak right out because it still
 
768
    thought it was waiting for activity from it in some other coro.
 
769
    """
 
770
    try:
 
771
        import select
 
772
        if hasattr(select, "poll"):
 
773
            return "poll"
 
774
        return "selects"
 
775
    except ImportError:
 
776
        return None
 
777
 
 
778
 
748
779
def drop_privileges(user):
749
780
    """
750
781
    Sets the userid/groupid of the current process, get session leader, etc.
756
787
        os.setgroups([])
757
788
    os.setgid(user[3])
758
789
    os.setuid(user[2])
 
790
    os.environ['HOME'] = user[5]
759
791
    try:
760
792
        os.setsid()
761
793
    except OSError:
1518
1550
            return itertools.chain([chunk], iterable)
1519
1551
        except StopIteration:
1520
1552
            return []
 
1553
 
 
1554
 
 
1555
class InputProxy(object):
 
1556
    """
 
1557
    File-like object that counts bytes read.
 
1558
    To be swapped in for wsgi.input for accounting purposes.
 
1559
    """
 
1560
    def __init__(self, wsgi_input):
 
1561
        """
 
1562
        :param wsgi_input: file-like object to wrap the functionality of
 
1563
        """
 
1564
        self.wsgi_input = wsgi_input
 
1565
        self.bytes_received = 0
 
1566
        self.client_disconnect = False
 
1567
 
 
1568
    def read(self, *args, **kwargs):
 
1569
        """
 
1570
        Pass read request to the underlying file-like object and
 
1571
        add bytes read to total.
 
1572
        """
 
1573
        try:
 
1574
            chunk = self.wsgi_input.read(*args, **kwargs)
 
1575
        except Exception:
 
1576
            self.client_disconnect = True
 
1577
            raise
 
1578
        self.bytes_received += len(chunk)
 
1579
        return chunk
 
1580
 
 
1581
    def readline(self, *args, **kwargs):
 
1582
        """
 
1583
        Pass readline request to the underlying file-like object and
 
1584
        add bytes read to total.
 
1585
        """
 
1586
        try:
 
1587
            line = self.wsgi_input.readline(*args, **kwargs)
 
1588
        except Exception:
 
1589
            self.client_disconnect = True
 
1590
            raise
 
1591
        self.bytes_received += len(line)
 
1592
        return line