Skip to content

Commit

Permalink
Use unique ID instead of fd in logging
Browse files Browse the repository at this point in the history
  • Loading branch information
icgood committed Sep 21, 2020
1 parent 9362344 commit 4d34868
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 22 deletions.
19 changes: 12 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,16 @@ after_success:
branches:
only:
- master
- /^\d+\.\d+.*$/ # version tags
deploy:
provider: pages
skip_cleanup: true
github_token: $GH_TOKEN
keep_history: true
on:
branch: master
local_dir: doc/build/html
- provider: pages:git
local_dir: doc/build/html
on:
tags: true
edge: true
- provider: pypi
distributions: sdist bdist_wheel
skip_existing: true
on:
tags: true
edge: true
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ This project attempts to simplify the complexity of the [IMAP protocol][1] into
a set of clean Python APIs that can be implemented by pluggable backends.
Everything runs in an [asyncio][2] event loop.

#### [API Documentation](http://icgood.github.io/pymap/)
#### [API Documentation](https://icgood.github.io/pymap/)

### Table of Contents

Expand Down
2 changes: 1 addition & 1 deletion pymap/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
primitives.
current_command: The currently executing
:class:`~pymap.parsing.command.Command`.
socket_info: :class:`~pymap.sockinfo.SocketInfo` about the currently
socket_info: :class:`~proxyprotocol.sock.SocketInfo` about the currently
connected client.
language_code: The language code string, e.g. ``en``.
connection_exit: The active :class:`~contextlib.AsyncExitStack` that will
Expand Down
20 changes: 11 additions & 9 deletions pymap/imap/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from base64 import b64encode, b64decode
from contextlib import closing, AsyncExitStack
from typing import TypeVar, Union, Optional, Iterable, List, Awaitable
from uuid import uuid4

from proxyprotocol import ProxyProtocolResult
from proxyprotocol.sock import SocketInfo
Expand Down Expand Up @@ -153,7 +154,8 @@ def _reset_streams(self, reader: StreamReader,
writer: StreamWriter) -> None:
self.reader = reader
self.writer = writer
socket_info.set(SocketInfo(writer, self.pp_result))
socket_info.set(SocketInfo(writer, self.pp_result,
unique_id=uuid4().bytes))

async def _read_proxy_protocol(self) -> None:
self.pp_result = await self.config.proxy_protocol.read(self.reader)
Expand All @@ -165,14 +167,14 @@ def close(self) -> None:
@classmethod
def _print(cls, log_format: str, output: Union[str, bytes]) -> None:
if _log.isEnabledFor(logging.DEBUG):
fd = socket_info.get().socket.fileno()
uid = socket_info.get().unique_id.hex()
if not isinstance(output, str):
output = str(output, 'utf-8', 'replace')
lines = cls._lines.split(output)
if not lines[-1]:
lines = lines[:-1]
for line in lines:
_log.debug(log_format, fd, line)
_log.debug(log_format, uid, line)

def _exec(self, future: Awaitable[_Ret]) -> Awaitable[_Ret]:
return subsystem.get().execute(future)
Expand All @@ -191,12 +193,12 @@ async def readline(self) -> memoryview:
buf += await self.reader.readexactly(literal_length)
buf += await self.reader.readline()
else:
self._print('%d -->| %s', buf)
self._print('%s -->| %s', buf)
return memoryview(buf)

async def read_continuation(self, literal_length: int) -> memoryview:
extra_literal = await self.reader.readexactly(literal_length)
self._print('%d -->| %s', extra_literal)
self._print('%s -->| %s', extra_literal)
extra_line = await self.readline()
extra = extra_literal + bytes(extra_line)
return memoryview(extra)
Expand Down Expand Up @@ -267,7 +269,7 @@ async def write_response(self, resp: Response) -> None:
except ConnectionError:
pass
else:
self._print('%d <--| %s', bytes(resp))
self._print('%s <--| %s', bytes(resp))

async def start_tls(self) -> None:
loop = asyncio.get_event_loop()
Expand All @@ -280,7 +282,7 @@ async def start_tls(self) -> None:
new_writer = StreamWriter(new_transport, new_protocol,
self.reader, loop)
self._reset_streams(self.reader, new_writer)
self._print('%d <->| %s', b'<TLS handshake>')
self._print('%s <->| %s', b'<TLS handshake>')

async def send_error_disconnect(self) -> None:
_, exc, _ = sys.exc_info()
Expand Down Expand Up @@ -345,7 +347,7 @@ async def run(self, state: ConnectionState) -> None:
"""
await self._read_proxy_protocol()
self._print('%d +++| %s', str(socket_info.get()))
self._print('%s +++| %s', str(socket_info.get()))
bad_commands = 0
try:
greeting = await self._exec(state.do_greeting())
Expand Down Expand Up @@ -415,4 +417,4 @@ async def run(self, state: ConnectionState) -> None:
finally:
await state.do_cleanup()
current_command.reset(prev_cmd)
self._print('%d ---| %s', b'<disconnected>')
self._print('%s ---| %s', b'<disconnected>')
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
license = f.read()

setup(name='pymap',
version='0.20.0',
version='0.20.1',
author='Ian Good',
author_email='icgood@gmail.com',
description='Lightweight, asynchronous IMAP serving in Python.',
Expand All @@ -50,7 +50,7 @@
packages=find_packages(),
install_requires=[
'pysasl >= 0.6.1',
'proxy-protocol >= 0.1.0',
'proxy-protocol >= 0.5.0',
'typing-extensions'],
extras_require={
'redis': ['aioredis >= 1.3.0', 'msgpack >= 1.0'],
Expand Down
7 changes: 5 additions & 2 deletions test/server/mocktransport.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import enum
import inspect
import re
import socket
import traceback
from collections import deque
from itertools import zip_longest
Expand All @@ -21,7 +22,7 @@ class _Socket:

def __init__(self, fd: int) -> None:
self.fd = fd
self.family = None
self.family = socket.AF_INET

def fileno(self):
return self.fd
Expand Down Expand Up @@ -195,7 +196,9 @@ def get_extra_info(self, name: str, default=None):
if name == 'socket':
return self.socket
elif name == 'peername':
return 'test'
return ('1.2.3.4', 1234)
elif name == 'sockname':
return ('5.6.7.8', 5678)

async def readline(self) -> bytes:
where, data, wait, set = self._pop_expected(_Type.READLINE)
Expand Down

0 comments on commit 4d34868

Please sign in to comment.