From 0120e62250a1c45720e1676683b874a90e6a67dc Mon Sep 17 00:00:00 2001 From: UbitUmarov Date: Sun, 15 Mar 2026 05:42:26 +0000 Subject: [PATCH] use global shared httpclient on webrtc session --- .../os-webrtc-janus/Janus/JanusSession.cs | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/OpenSim/Addons/os-webrtc-janus/Janus/JanusSession.cs b/OpenSim/Addons/os-webrtc-janus/Janus/JanusSession.cs index 751d65fa84..f1f0bd4402 100644 --- a/OpenSim/Addons/os-webrtc-janus/Janus/JanusSession.cs +++ b/OpenSim/Addons/os-webrtc-janus/Janus/JanusSession.cs @@ -38,6 +38,7 @@ using OpenMetaverse.StructuredData; using log4net; using System.Threading; +using OpenSim.Framework; namespace osWebRtcVoice { @@ -66,7 +67,6 @@ namespace osWebRtcVoice public string PluginId { get; set; } private CancellationTokenSource _CancelTokenSource = new CancellationTokenSource(); - private HttpClient _HttpClient = new HttpClient(); public bool IsConnected { get; set; } @@ -90,11 +90,6 @@ namespace osWebRtcVoice { _ = DestroySession(); } - if (_HttpClient is not null) - { - _HttpClient.Dispose(); - _HttpClient = null; - } } /// @@ -287,10 +282,11 @@ namespace osWebRtcVoice string reqStr = pReq.ToJson(); + HttpClient httpClient = WebUtil.GetNewGlobalHttpClient(30000); HttpRequestMessage reqMsg = new(HttpMethod.Post, pURI); reqMsg.Content = new StringContent(reqStr, System.Text.Encoding.UTF8, MediaTypeNames.Application.Json); - reqMsg.Headers.Add("Accept", "application/json"); - HttpResponseMessage response = await _HttpClient.SendAsync(reqMsg, _CancelTokenSource.Token); + reqMsg.Headers.TryAddWithoutValidation("Accept", "application/json"); + HttpResponseMessage response = await httpClient.SendAsync(reqMsg, _CancelTokenSource.Token); if (response.IsSuccessStatusCode) { @@ -355,11 +351,12 @@ namespace osWebRtcVoice try { + HttpClient httpClient = WebUtil.GetNewGlobalHttpClient(30000); HttpRequestMessage reqMsg = new(HttpMethod.Post, pURI); string reqStr = pReq.ToJson(); reqMsg.Content = new StringContent(reqStr, System.Text.Encoding.UTF8, MediaTypeNames.Application.Json); - reqMsg.Headers.Add("Accept", "application/json"); - HttpResponseMessage response = await _HttpClient.SendAsync(reqMsg); + reqMsg.Headers.TryAddWithoutValidation("Accept", "application/json"); + HttpResponseMessage response = await httpClient.SendAsync(reqMsg); string respStr = await response.Content.ReadAsStringAsync(); ret = JanusMessageResp.FromJson(respStr); } @@ -443,7 +440,7 @@ namespace osWebRtcVoice /// /// /// - public async Task GetFromJanus(string pURI) + public async Task GetFromJanus(string pURI, int timeout = 30000) { if (!string.IsNullOrEmpty(_JanusAPIToken)) { @@ -454,12 +451,14 @@ namespace osWebRtcVoice try { // m_log.DebugFormat("{0} GetFromJanus: URI = \"{1}\"", LogHeader, pURI); + //HttpClient httpClient = WebUtil.GetNewGlobalHttpClient(timeout); + HttpClient httpClient = WebUtil.GetGlobalNoRedirHttpClient(timeout); HttpRequestMessage reqMsg = new HttpRequestMessage(HttpMethod.Get, pURI); - reqMsg.Headers.Add("Accept", "application/json"); + reqMsg.Headers.TryAddWithoutValidation("Accept", "application/json"); HttpResponseMessage response = null; try { - response = await _HttpClient.SendAsync(reqMsg, _CancelTokenSource.Token); + response = await httpClient.SendAsync(reqMsg, _CancelTokenSource.Token); if (response is not null && response.IsSuccessStatusCode) { string respStr = await response.Content.ReadAsStringAsync(); @@ -563,10 +562,10 @@ namespace osWebRtcVoice { try { - var resp = await GetFromJanus(SessionUri); + JanusMessageResp resp = await GetFromJanus(SessionUri, 60000); if (resp is not null) { - _ = Task.Run(() => + //_ = Task.Run(() => { EventResp eventResp = new EventResp(resp); switch (resp.ReturnCode) @@ -733,7 +732,8 @@ namespace osWebRtcVoice m_log.DebugFormat("{0} EventLongPoll: unknown response {1}", LogHeader, resp.ToString()); break; } - }); + } + // ); } else {