better session handeling
* improved session handeling Due to the with statement in the session creation process, every requests to the api created a new session. This patch decreases the enormous TCP overhead, by creating a single global session to reuse.
This commit is contained in:
parent
95616508ce
commit
c8d88a537d
|
@ -20,7 +20,10 @@ class EjabberdMetrics:
|
||||||
self.url = url
|
self.url = url
|
||||||
self._cmd = self._rpc
|
self._cmd = self._rpc
|
||||||
else:
|
else:
|
||||||
|
import requests
|
||||||
|
|
||||||
self._url = url
|
self._url = url
|
||||||
|
self.session = requests.Session()
|
||||||
self._cmd = self._rest
|
self._cmd = self._rest
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
@ -43,15 +46,19 @@ class EjabberdMetrics:
|
||||||
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def _rest(self, command: str, data):
|
def _rest(self, command: str, data) -> dict:
|
||||||
import requests
|
# add authentication header to the session obj
|
||||||
|
if self.session.auth is None:
|
||||||
|
self.session.auth = self._auth
|
||||||
|
|
||||||
with requests.Session() as s:
|
# post
|
||||||
r = s.post(f'{self._url}/{command}', auth=self._auth, json=data)
|
r = self.session.post('/'.join([self._url, command]), json=data)
|
||||||
|
|
||||||
if r.status_code == 200:
|
# proceed if response is ok
|
||||||
|
if r.ok:
|
||||||
return r.json()
|
return r.json()
|
||||||
return{}
|
|
||||||
|
return {}
|
||||||
|
|
||||||
def _rpc(self, command: str, data):
|
def _rpc(self, command: str, data):
|
||||||
from xmlrpc import client
|
from xmlrpc import client
|
||||||
|
|
Loading…
Reference in New Issue