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.

grpc.go 3.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package ops
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "gitlink.org.cn/cloudream/common/pkgs/future"
  7. "gitlink.org.cn/cloudream/common/pkgs/logger"
  8. cdssdk "gitlink.org.cn/cloudream/common/sdks/storage"
  9. "gitlink.org.cn/cloudream/common/utils/io2"
  10. stgglb "gitlink.org.cn/cloudream/storage/common/globals"
  11. "gitlink.org.cn/cloudream/storage/common/pkgs/ioswitch"
  12. )
  13. type SendStream struct {
  14. Input *ioswitch.StreamVar `json:"input"`
  15. Send *ioswitch.StreamVar `json:"send"`
  16. Node cdssdk.Node `json:"node"`
  17. }
  18. func (o *SendStream) Execute(ctx context.Context, sw *ioswitch.Switch) error {
  19. err := sw.BindVars(ctx, o.Input)
  20. if err != nil {
  21. return err
  22. }
  23. defer o.Input.Stream.Close()
  24. agtCli, err := stgglb.AgentRPCPool.Acquire(stgglb.SelectGRPCAddress(&o.Node))
  25. if err != nil {
  26. return fmt.Errorf("new agent rpc client: %w", err)
  27. }
  28. defer stgglb.AgentRPCPool.Release(agtCli)
  29. logger.Debugf("sending stream %v as %v to node %v", o.Input.ID, o.Send.ID, o.Node)
  30. // 发送后流的ID不同
  31. err = agtCli.SendStream(ctx, sw.Plan().ID, o.Send.ID, o.Input.Stream)
  32. if err != nil {
  33. return fmt.Errorf("sending stream: %w", err)
  34. }
  35. return nil
  36. }
  37. type GetStream struct {
  38. Signal *ioswitch.SignalVar `json:"signal"`
  39. Target *ioswitch.StreamVar `json:"target"`
  40. Output *ioswitch.StreamVar `json:"output"`
  41. Node cdssdk.Node `json:"node"`
  42. }
  43. func (o *GetStream) Execute(ctx context.Context, sw *ioswitch.Switch) error {
  44. agtCli, err := stgglb.AgentRPCPool.Acquire(stgglb.SelectGRPCAddress(&o.Node))
  45. if err != nil {
  46. return fmt.Errorf("new agent rpc client: %w", err)
  47. }
  48. defer stgglb.AgentRPCPool.Release(agtCli)
  49. logger.Debugf("getting stream %v as %v from node %v", o.Target.ID, o.Output.ID, o.Node)
  50. str, err := agtCli.GetStream(sw.Plan().ID, o.Target.ID, o.Signal)
  51. if err != nil {
  52. return fmt.Errorf("getting stream: %w", err)
  53. }
  54. fut := future.NewSetVoid()
  55. // 获取后送到本地的流ID是不同的
  56. o.Output.Stream = io2.AfterReadClosedOnce(str, func(closer io.ReadCloser) {
  57. fut.SetVoid()
  58. })
  59. sw.PutVars(o.Output)
  60. return fut.Wait(ctx)
  61. }
  62. type SendVar struct {
  63. Input ioswitch.Var `json:"input"`
  64. Send ioswitch.Var `json:"send"`
  65. Node cdssdk.Node `json:"node"`
  66. }
  67. func (o *SendVar) Execute(ctx context.Context, sw *ioswitch.Switch) error {
  68. err := sw.BindVars(ctx, o.Input)
  69. if err != nil {
  70. return err
  71. }
  72. agtCli, err := stgglb.AgentRPCPool.Acquire(stgglb.SelectGRPCAddress(&o.Node))
  73. if err != nil {
  74. return fmt.Errorf("new agent rpc client: %w", err)
  75. }
  76. defer stgglb.AgentRPCPool.Release(agtCli)
  77. logger.Debugf("sending var %v as %v to node %v", o.Input.GetID(), o.Send.GetID(), o.Node)
  78. ioswitch.AssignVar(o.Input, o.Send)
  79. err = agtCli.SendVar(ctx, sw.Plan().ID, o.Send)
  80. if err != nil {
  81. return fmt.Errorf("sending var: %w", err)
  82. }
  83. return nil
  84. }
  85. type GetVar struct {
  86. Signal *ioswitch.SignalVar `json:"signal"`
  87. Target ioswitch.Var `json:"target"`
  88. Output ioswitch.Var `json:"output"`
  89. Node cdssdk.Node `json:"node"`
  90. }
  91. func (o *GetVar) Execute(ctx context.Context, sw *ioswitch.Switch) error {
  92. agtCli, err := stgglb.AgentRPCPool.Acquire(stgglb.SelectGRPCAddress(&o.Node))
  93. if err != nil {
  94. return fmt.Errorf("new agent rpc client: %w", err)
  95. }
  96. defer stgglb.AgentRPCPool.Release(agtCli)
  97. logger.Debugf("getting var %v as %v from node %v", o.Target.GetID(), o.Output.GetID(), o.Node)
  98. v2, err := agtCli.GetVar(ctx, sw.Plan().ID, o.Target, o.Signal)
  99. if err != nil {
  100. return fmt.Errorf("getting var: %w", err)
  101. }
  102. ioswitch.AssignVar(v2, o.Output)
  103. sw.PutVars(o.Output)
  104. return nil
  105. }
  106. func init() {
  107. OpUnion.AddT((*SendStream)(nil))
  108. OpUnion.AddT((*GetStream)(nil))
  109. OpUnion.AddT((*SendVar)(nil))
  110. OpUnion.AddT((*GetVar)(nil))
  111. }

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