Skip to content

Commit

Permalink
Enable port/connections env vars.
Browse files Browse the repository at this point in the history
Also clean up logging and variable handling a bit.
  • Loading branch information
ktlim committed Mar 13, 2024
1 parent 471ae5e commit d439781
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 21 deletions.
15 changes: 6 additions & 9 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ convention = "numpy"
add-ignore = ["D107", "D105", "D102", "D100", "D200", "D205", "D400", "D104"]

[tool.ruff]
line-length = 110
target-version = "py311"

[tool.ruff.lint]
ignore = [
"N802",
"N803",
Expand All @@ -34,23 +38,16 @@ ignore = [
"D205",
"D400",
]
line-length = 110
select = [
"E", # pycodestyle
"F", # pycodestyle
"N", # pep8-naming
"W", # pycodestyle
"D", # pydocstyle
]
target-version = "py311"
# Commented out to suppress "unused noqa" in jenkins which has older ruff not
# generating E721.
#extend-select = [
# "RUF100", # Warn about unused noqa
#]

[tool.ruff.pycodestyle]
[tool.ruff.lint.pycodestyle]
max-doc-length = 79

[tool.ruff.pydocstyle]
[tool.ruff.lint.pydocstyle]
convention = "numpy"
28 changes: 18 additions & 10 deletions python/s3daemon/s3daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,26 @@
import aiobotocore.session
import botocore

port = int(os.environ.get("PORT", 15555))

endpoint_url = os.environ["S3_ENDPOINT_URL"]
access_key = os.environ["AWS_ACCESS_KEY_ID"]
secret_key = os.environ["AWS_SECRET_ACCESS_KEY"]
max_connections = os.environ.get("MAX_CONNECTIONS", 25)

config = botocore.config.Config(
max_pool_connections=25,
max_pool_connections=max_connections,
tcp_keepalive=True,
s3=dict(
payload_signing_enabled=False,
addressing_style="path",
),
)

PORT = 15555
endpoint_url = os.environ["S3_ENDPOINT_URL"]
access_key = os.environ["AWS_ACCESS_KEY_ID"]
secret_key = os.environ["AWS_SECRET_ACCESS_KEY"]

pylog_longLogFmt = "{levelname} {asctime} {name} - {message}"
if "S3DAEMON_LOG" in os.environ:
logging.basicConfig(filename=os.environ["S3DAEMON_LOG"], format=pylog_longLogFmt, style="{")
log_file = os.environ.get("S3DAEMON_LOG")
if log_file is not None:
logging.basicConfig(filename=log_file, format=pylog_longLogFmt, style="{")
else:
logging.basicConfig(format=pylog_longLogFmt, style="{")
log = logging.getLogger(__name__)
Expand All @@ -66,16 +69,21 @@ async def handle_client(client, reader, writer):
start = time.time()
# ignore the alias
_, bucket, key = dest.split("/", maxsplit=2)
result = "Success"
with open(filename, "rb") as f:
try:
await client.put_object(Body=f, Bucket=bucket, Key=key)
writer.write(b"Success")
except Exception as e:
writer.write(bytes(repr(e), "UTF-8"))
log.info("%f %f sec", start, time.time() - start)
result = f"Exception {e}"
log.info("%f %f sec: %s", start, time.time() - start, result)


async def main():
"""Run the daemon server."""
global access_key, secret_key, endpoint_url, config, port

"""Start the server."""
session = aiobotocore.session.get_session()
async with session.create_client(
Expand All @@ -89,7 +97,7 @@ async def main():
async def client_cb(reader, writer):
await handle_client(client, reader, writer)

server = await asyncio.start_server(client_cb, "localhost", PORT)
server = await asyncio.start_server(client_cb, "localhost", port)
log.info("Starting server")
async with server:
await server.serve_forever()
Expand Down
6 changes: 4 additions & 2 deletions python/s3daemon/send.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.

import os
import socket
import sys

PORT = 15555
port = int(os.environ.get("PORT", 15555))


def send(filename, dest):
Expand All @@ -36,8 +37,9 @@ def send(filename, dest):
Destination in ``{alias}/{bucket}/{key}`` format.
The key may contain slashes.
"""
global port
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
sock.connect(("localhost", PORT))
sock.connect(("localhost", port))
sock.sendall(bytes(f"{filename} {dest}\n", "UTF-8"))
received = str(sock.recv(4096), "utf-8")
sock.close()
Expand Down

0 comments on commit d439781

Please sign in to comment.