You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

cache.go 4.3 kB

2 years ago
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package mq
  2. import (
  3. "time"
  4. "github.com/samber/lo"
  5. "gitlink.org.cn/cloudream/common/consts/errorcode"
  6. "gitlink.org.cn/cloudream/common/pkgs/logger"
  7. "gitlink.org.cn/cloudream/common/pkgs/mq"
  8. mytask "gitlink.org.cn/cloudream/storage/agent/internal/task"
  9. stgglb "gitlink.org.cn/cloudream/storage/common/globals"
  10. agtmq "gitlink.org.cn/cloudream/storage/common/pkgs/mq/agent"
  11. )
  12. // CheckCache 检查IPFS缓存
  13. // 参数 msg: 包含检查缓存请求信息的结构体
  14. // 返回值: 检查缓存响应结构体和错误信息
  15. func (svc *Service) CheckCache(msg *agtmq.CheckCache) (*agtmq.CheckCacheResp, *mq.CodeMessage) {
  16. ipfsCli, err := stgglb.IPFSPool.Acquire() // 尝试从IPFS池获取客户端
  17. if err != nil {
  18. logger.Warnf("new ipfs client: %s", err.Error())
  19. return nil, mq.Failed(errorcode.OperationFailed, "new ipfs client failed")
  20. }
  21. defer ipfsCli.Close() // 确保IPFS客户端被正确关闭
  22. files, err := ipfsCli.GetPinnedFiles() // 获取IPFS上被固定的文件列表
  23. if err != nil {
  24. logger.Warnf("get pinned files from ipfs failed, err: %s", err.Error())
  25. return nil, mq.Failed(errorcode.OperationFailed, "get pinned files from ipfs failed")
  26. }
  27. return mq.ReplyOK(agtmq.NewCheckCacheResp(lo.Keys(files))) // 返回文件列表的键
  28. }
  29. // CacheGC 执行缓存垃圾回收
  30. // 参数 msg: 包含垃圾回收请求信息的结构体
  31. // 返回值: 垃圾回收响应结构体和错误信息
  32. func (svc *Service) CacheGC(msg *agtmq.CacheGC) (*agtmq.CacheGCResp, *mq.CodeMessage) {
  33. ipfsCli, err := stgglb.IPFSPool.Acquire() // 尝试从IPFS池获取客户端
  34. if err != nil {
  35. logger.Warnf("new ipfs client: %s", err.Error())
  36. return nil, mq.Failed(errorcode.OperationFailed, "new ipfs client failed")
  37. }
  38. defer ipfsCli.Close() // 确保IPFS客户端被正确关闭
  39. files, err := ipfsCli.GetPinnedFiles() // 获取IPFS上被固定的文件列表
  40. if err != nil {
  41. logger.Warnf("get pinned files from ipfs failed, err: %s", err.Error())
  42. return nil, mq.Failed(errorcode.OperationFailed, "get pinned files from ipfs failed")
  43. }
  44. // 根据请求对比当前被固定的文件,将未记录到元数据的文件取消固定
  45. shouldPinnedFiles := lo.SliceToMap(msg.PinnedFileHashes, func(hash string) (string, bool) { return hash, true })
  46. for hash := range files {
  47. if !shouldPinnedFiles[hash] {
  48. ipfsCli.Unpin(hash) // 取消固定文件
  49. logger.WithField("FileHash", hash).Debugf("unpinned by gc")
  50. }
  51. }
  52. return mq.ReplyOK(agtmq.RespCacheGC()) // 返回垃圾回收完成的响应
  53. }
  54. // StartCacheMovePackage 开始缓存移动包
  55. // 参数 msg: 包含启动缓存移动请求信息的结构体
  56. // 返回值: 启动缓存移动响应结构体和错误信息
  57. func (svc *Service) StartCacheMovePackage(msg *agtmq.StartCacheMovePackage) (*agtmq.StartCacheMovePackageResp, *mq.CodeMessage) {
  58. tsk := svc.taskManager.StartNew(mytask.NewCacheMovePackage(msg.UserID, msg.PackageID)) // 启动新的缓存移动任务
  59. return mq.ReplyOK(agtmq.NewStartCacheMovePackageResp(tsk.ID())) // 返回任务ID
  60. }
  61. // WaitCacheMovePackage 等待缓存移动包完成
  62. // 参数 msg: 包含等待缓存移动请求信息的结构体
  63. // 返回值: 等待缓存移动响应结构体和错误信息
  64. func (svc *Service) WaitCacheMovePackage(msg *agtmq.WaitCacheMovePackage) (*agtmq.WaitCacheMovePackageResp, *mq.CodeMessage) {
  65. tsk := svc.taskManager.FindByID(msg.TaskID) // 根据任务ID查找任务
  66. if tsk == nil {
  67. return nil, mq.Failed(errorcode.TaskNotFound, "task not found") // 如果任务不存在,返回错误
  68. }
  69. if msg.WaitTimeoutMs == 0 {
  70. tsk.Wait() // 等待任务完成
  71. errMsg := ""
  72. if tsk.Error() != nil {
  73. errMsg = tsk.Error().Error() // 获取任务错误信息
  74. }
  75. return mq.ReplyOK(agtmq.NewWaitCacheMovePackageResp(true, errMsg)) // 返回任务完成状态和错误信息
  76. } else {
  77. if tsk.WaitTimeout(time.Duration(msg.WaitTimeoutMs) * time.Millisecond) { // 设置等待超时
  78. errMsg := ""
  79. if tsk.Error() != nil {
  80. errMsg = tsk.Error().Error() // 获取任务错误信息
  81. }
  82. return mq.ReplyOK(agtmq.NewWaitCacheMovePackageResp(true, errMsg)) // 返回任务完成状态和错误信息
  83. }
  84. return mq.ReplyOK(agtmq.NewWaitCacheMovePackageResp(false, "")) // 返回等待超时状态
  85. }
  86. }

本项目旨在将云际存储公共基础设施化,使个人及企业可低门槛使用高效的云际存储服务(安装开箱即用云际存储客户端即可,无需关注其他组件的部署),同时支持用户灵活便捷定制云际存储的功能细节。