Skip to content
This repository was archived by the owner on Feb 7, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion panini/middleware/error.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ async def send_any(self, subject: str, message, send_func, *args, **kwargs):

async def listen_any(self, msg, callback):
try:
response = await callback(msg)
if asyncio.iscoroutinefunction(callback):
response = await callback(msg)
else:
response = callback(msg)
return response
except self.error as e:
if asyncio.iscoroutinefunction(self.callback):
Expand Down
11 changes: 9 additions & 2 deletions panini/middleware/prometheus_monitoring.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import asyncio
import time

from prometheus_client import CollectorRegistry, Histogram, Counter, push_to_gateway
Expand Down Expand Up @@ -104,15 +105,21 @@ async def listen_any(self, msg, callback):

if "status" in self.labels:
try:
response = await callback(msg)
if asyncio.iscoroutinefunction(callback):
response = await callback(msg)
else:
response = callback(msg)
except Exception:
labels["status"] = "failure"
self.monitor_listen(start_time, labels)
raise
else:
labels["status"] = "success"
else:
response = await callback(msg)
if asyncio.iscoroutinefunction(callback):
response = await callback(msg)
else:
response = callback(msg)

self.monitor_listen(start_time, labels)

Expand Down
6 changes: 5 additions & 1 deletion panini/middleware/reader_emulator_middleware.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import asyncio
import uuid

from panini.app import get_app
Expand Down Expand Up @@ -38,7 +39,10 @@ def __init__(self, *args, **kwargs):

async def listen_any(self, message: Msg, callback):
message.subject = message.subject[len(self._prefix) + 1 :]
response = await callback(message)
if asyncio.iscoroutinefunction(callback):
response = await callback(message)
else:
response = callback(message)
return response

async def send_any(self, subject: str, message, send_func, *args, **kwargs):
Expand Down
11 changes: 9 additions & 2 deletions panini/middleware/writer_emulator_middleware.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import asyncio
import json
import os
import threading
Expand Down Expand Up @@ -116,11 +117,17 @@ async def listen_publish(self, msg, callback):
}
)

await callback(msg)
if asyncio.iscoroutinefunction(callback):
await callback(msg)
else:
callback(msg)

async def listen_request(self, msg, callback):

response = await callback(msg)
if asyncio.iscoroutinefunction(callback):
response = await callback(msg)
else:
response = callback(msg)

self._writer.add(
{
Expand Down