Browse Source

a few changes on HGinventoryBroker p+lus cosmetics

UbitUmarov 3 years ago
parent
commit
97870cc1b9

+ 4 - 4
OpenSim/Framework/RestClient.cs

@@ -93,7 +93,7 @@ namespace OpenSim.Framework
         /// <summary>
         /// MemoryStream representing the resulting resource
         /// </summary>
-        private Stream _resource;
+        private MemoryStream _resource;
 
         /// <summary>
         /// WebRequest object, held as a member variable
@@ -313,7 +313,7 @@ namespace OpenSim.Framework
         /// <summary>
         /// Perform a synchronous request
         /// </summary>
-        public Stream Request()
+        public MemoryStream Request()
         {
             return Request(null);
         }
@@ -321,7 +321,7 @@ namespace OpenSim.Framework
         /// <summary>
         /// Perform a synchronous request
         /// </summary>
-        public Stream Request(IServiceAuth auth)
+        public MemoryStream Request(IServiceAuth auth)
         {
             lock (_lock)
             {
@@ -471,7 +471,7 @@ namespace OpenSim.Framework
             try
             {
                 // Perform the operation; if sucessful set the result
-                Stream s = Request(null);
+                MemoryStream s = Request(null);
                 ar.SetAsCompleted(s, false);
             }
             catch (Exception e)

+ 1 - 6
OpenSim/Framework/WebUtil.cs

@@ -1324,15 +1324,12 @@ namespace OpenSim.Framework
                 {
                     if (hwr != null)
                     {
-                        if (hwr.StatusCode == HttpStatusCode.NotFound)
-                            return deserial;
-
                         if (hwr.StatusCode == HttpStatusCode.Unauthorized)
                         {
                             m_log.ErrorFormat("[SynchronousRestObjectRequester]: Web request {0} requires authentication",
                                 requestUrl);
                         }
-                        else
+                        else if(hwr.StatusCode != HttpStatusCode.NotFound)
                         {
                             m_log.WarnFormat("[SynchronousRestObjectRequester]: Web request {0} returned error: {1}",
                                 requestUrl, hwr.StatusCode);
@@ -1342,8 +1339,6 @@ namespace OpenSim.Framework
                         m_log.ErrorFormat(
                             "[SynchronousRestObjectRequester]: WebException for {0} {1} {2} {3}",
                             verb, requestUrl, typeof(TResponse).ToString(), e.Message);
-
-                    return deserial;
                 }
             }
             catch (System.InvalidOperationException)

+ 33 - 67
OpenSim/Region/CoreModules/ServiceConnectorsOut/Inventory/HGInventoryBroker.cs

@@ -30,6 +30,7 @@ using Mono.Addins;
 using Nini.Config;
 using System;
 using System.Collections.Generic;
+using System.Collections.Concurrent;
 using System.Reflection;
 using OpenSim.Framework;
 
@@ -45,26 +46,26 @@ namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.Inventory
     [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "HGInventoryBroker")]
     public class HGInventoryBroker : ISharedRegionModule, IInventoryService
     {
-        private static readonly ILog m_log =
-                LogManager.GetLogger(
+        private static readonly ILog m_log = LogManager.GetLogger(
                 MethodBase.GetCurrentMethod().DeclaringType);
 
         private static bool m_Enabled = false;
 
-        private static IInventoryService m_LocalGridInventoryService;
-        private Dictionary<string, IInventoryService> m_connectors = new Dictionary<string, IInventoryService>();
+        private const int CONNECTORS_CACHE_EXPIRE = 60000; // 1 minute
 
-        // A cache of userIDs --> ServiceURLs, for HGBroker only
-        protected Dictionary<UUID, string> m_InventoryURLs = new Dictionary<UUID,string>();
+        private readonly List<Scene> m_Scenes = new List<Scene>();
 
-        private List<Scene> m_Scenes = new List<Scene>();
+        private IInventoryService m_LocalGridInventoryService;
+        private readonly ExpiringCacheOS<string, IInventoryService> m_connectors = new ExpiringCacheOS<string, IInventoryService>();
 
-        private InventoryCache m_Cache = new InventoryCache();
+        // A cache of userIDs --> ServiceURLs, for HGBroker only
+        protected readonly ConcurrentDictionary<UUID, string> m_InventoryURLs = new ConcurrentDictionary<UUID, string>();
+        private readonly InventoryCache m_Cache = new InventoryCache();
 
         /// <summary>
         /// Used to serialize inventory requests.
         /// </summary>
-        private object m_Lock = new object();
+        private readonly object m_Lock = new object();
 
         protected IUserManagement m_UserManagement;
         protected IUserManagement UserManagementModule
@@ -112,9 +113,7 @@ namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.Inventory
 
                     string localDll = inventoryConfig.GetString("LocalGridInventoryService",
                             String.Empty);
-                    //string HGDll = inventoryConfig.GetString("HypergridInventoryService",
-                    //        String.Empty);
-
+ 
                     if (localDll == String.Empty)
                     {
                         m_log.Error("[HG INVENTORY CONNECTOR]: No LocalGridInventoryService named in section InventoryService");
@@ -176,9 +175,8 @@ namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.Inventory
 
                     ((LocalInventoryServicesConnector)m_LocalGridInventoryService).Scene = scene;
                 }
-
-                scene.EventManager.OnClientClosed += OnClientClosed;
             }
+            scene.EventManager.OnClientClosed += OnClientClosed;
         }
 
         public void RemoveRegion(Scene scene)
@@ -187,6 +185,7 @@ namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.Inventory
                 return;
 
             m_Scenes.Remove(scene);
+            scene.EventManager.OnClientClosed -= OnClientClosed;
         }
 
         public void RegionLoaded(Scene scene)
@@ -202,21 +201,17 @@ namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.Inventory
 
         void OnClientClosed(UUID clientID, Scene scene)
         {
-            ScenePresence sp = null;
             foreach (Scene s in m_Scenes)
             {
-                s.TryGetScenePresence(clientID, out sp);
-                if ((sp != null) && !sp.IsChildAgent && (s != scene))
+                if(s.TryGetScenePresence(clientID, out ScenePresence sp) && !sp.IsChildAgent && sp.ControllingClient != null && sp.ControllingClient.IsActive)
                 {
-                    m_log.DebugFormat("[INVENTORY CACHE]: OnClientClosed in {0}, but user {1} still in sim. Keeping inventoryURL in cache",
-                            scene.RegionInfo.RegionName, clientID);
-                        return;
+                    //m_log.DebugFormat("[HG INVENTORY CACHE]: OnClientClosed in {0}, but user {1} still in sim. Keeping inventoryURL in cache",
+                    //        scene.RegionInfo.RegionName, clientID);
+                    return;
                 }
             }
 
-            if (m_InventoryURLs.ContainsKey(clientID)) // if it's in cache
-                    DropInventoryServiceURL(clientID);
-
+            m_InventoryURLs.TryRemove(clientID, out string dummy);
             m_Cache.RemoveAll(clientID);
         }
 
@@ -225,7 +220,7 @@ namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.Inventory
         /// and sticks it in the cache
         /// </summary>
         /// <param name="userID"></param>
-        private void CacheInventoryServiceURL(UUID userID)
+        private string CacheInventoryServiceURL(UUID userID)
         {
             if (UserManagementModule != null && !UserManagementModule.IsLocalGridUser(userID))
             {
@@ -239,9 +234,9 @@ namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.Inventory
                     {
                         AgentCircuitData aCircuit = scene.AuthenticateHandler.GetAgentCircuitData(sp.ControllingClient.CircuitCode);
                         if (aCircuit == null)
-                            return;
+                            return null;
                         if (aCircuit.ServiceURLs == null)
-                            return;
+                            return null;
 
                         if (aCircuit.ServiceURLs.ContainsKey("InventoryServerURI"))
                         {
@@ -249,10 +244,9 @@ namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.Inventory
                             if (inventoryURL != null && inventoryURL != string.Empty)
                             {
                                 inventoryURL = inventoryURL.Trim(new char[] { '/' });
-                                lock (m_InventoryURLs)
-                                    m_InventoryURLs[userID] = inventoryURL;
+                                m_InventoryURLs[userID] = inventoryURL;
                                 m_log.DebugFormat("[HG INVENTORY CONNECTOR]: Added {0} to the cache of inventory URLs", inventoryURL);
-                                return;
+                                return inventoryURL;
                             }
                         }
 //                        else
@@ -268,46 +262,23 @@ namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.Inventory
                     if (!string.IsNullOrEmpty(inventoryURL))
                     {
                         inventoryURL = inventoryURL.Trim(new char[] { '/' });
-                        lock (m_InventoryURLs)
-                            m_InventoryURLs[userID] = inventoryURL;
+                        m_InventoryURLs[userID] = inventoryURL;
                         m_log.DebugFormat("[HG INVENTORY CONNECTOR]: Added {0} to the cache of inventory URLs", inventoryURL);
+                        return inventoryURL;
                     }
                 }
             }
-        }
-
-        private void DropInventoryServiceURL(UUID userID)
-        {
-            lock (m_InventoryURLs)
-            {
-                if (m_InventoryURLs.ContainsKey(userID))
-                {
-                    string url = m_InventoryURLs[userID];
-                    m_InventoryURLs.Remove(userID);
-                    m_log.DebugFormat("[HG INVENTORY CONNECTOR]: Removed {0} from the cache of inventory URLs", url);
-                }
-            }
+            return null;
         }
 
         public string GetInventoryServiceURL(UUID userID)
         {
-            lock (m_InventoryURLs)
-            {
-                if (m_InventoryURLs.ContainsKey(userID))
-                    return m_InventoryURLs[userID];
-            }
-
-            CacheInventoryServiceURL(userID);
-
-            lock (m_InventoryURLs)
-            {
-                if (m_InventoryURLs.ContainsKey(userID))
-                  return m_InventoryURLs[userID];
-            }
-
-            return null; //it means that the methods should forward to local grid's inventory
+            if (m_InventoryURLs.TryGetValue(userID, out string value))
+                return value;
 
+             return CacheInventoryServiceURL(userID);
         }
+
         #endregion
 
         #region IInventoryService
@@ -685,22 +656,17 @@ namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.Inventory
             IInventoryService connector = null;
             lock (m_connectors)
             {
-                if (m_connectors.ContainsKey(url))
-                {
-                    connector = m_connectors[url];
-                }
-                else
+                if (!m_connectors.TryGetValue(url, out connector))
                 {
                     // Still not as flexible as I would like this to be,
                     // but good enough for now
                     RemoteXInventoryServicesConnector rxisc = new RemoteXInventoryServicesConnector(url);
                     rxisc.Scene = m_Scenes[0];
                     connector = rxisc;
-
-                    m_connectors.Add(url, connector);
                 }
+                if (connector != null)
+                    m_connectors.AddOrUpdate(url, connector, CONNECTORS_CACHE_EXPIRE);
             }
-
             return connector;
         }
     }