Aetherance commented on issue #3166:
URL: https://github.com/apache/dubbo-go/issues/3166#issuecomment-3765205618

   router chain cache 设计思路
   
   1. router chain cache 存储结构
   
   > router 的路由功能是有条件的,需要通过一定的条件进行 routing
   
   因此不同的条件会产生不同的路由结果,用 key-val 结构可以将不同的条件hash到不同的路由结果,使router chain cache 
可以正确获取路由结果。
   
   2. router chain cache key 设计
   
   ```go
   type RouterCacheKey struct {
        // 服务标识
        ServiceKey string 
   
        // 方法标识
        MethodName string 
   
        // 路由元数据哈希
        // 将所有影响路由的动态参数(Identity, Tags, Attachments)压缩成一个 hash值
        RuleAttributeHash uint64 
   }
   ```
   
   这个key将会通过 buildCacheKey() 构建,然后作为存储路由结果 []Invokers 的键
   
   对于过于复杂的路由结果,可以不进行缓存,以免出现命中错误缓存等危险情况
   
   3. router chain cache interface 设计
   
   ```go
   type RouterCache interface {
        // Get 根据 Key 获取缓存的 Invoker 列表
        Get(key RouterCacheKey) ([]base.Invoker, bool)
   
        // Set 将路由计算结果存入缓存
        Set(key RouterCacheKey, invokers []base.Invoker)
   
        // Clear 清空当前服务下的所有路由缓存
        // 触发时机:
        // 1. SetInvokers
        // 2. Process
       // 3. ...
        Clear()
   }
   ```
   
   4. router chain cache struct 实现示例
   
   (由ai生成,不代表最终设计)
   
   ```go
   package chain
   
   import (
        "sync"
   )
   
   import (
        "dubbo.apache.org/dubbo-go/v3/protocol/base"
   )
   
   // DefaultRouterCache 是 RouterCache 接口的默认实现。
   // 它内置了读写锁以支持高并发下的缓存读写。
   type DefaultRouterCache struct {
        // 使用读写锁保护 map,因为路由场景下“读多写少”
        mutex sync.RWMutex
        
        // 核心存储结构
        // Key 是我们定义的 RouterCacheKey 结构体
        // Value 是过滤后的 Invoker 指针切片
        data map[RouterCacheKey][]base.Invoker
        
        // 可选:最大容量限制(简单的防内存溢出机制)
        maxCapacity int
   }
   
   // NewDefaultRouterCache 创建一个默认的缓存实例
   func NewDefaultRouterCache(capacity int) *DefaultRouterCache {
        if capacity <= 0 {
                capacity = 1000 // 默认给个 1000 个缓存位
        }
        return &DefaultRouterCache{
                data:        make(map[RouterCacheKey][]base.Invoker, capacity),
                maxCapacity: capacity,
        }
   }
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to