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.

hub_worker.go 3.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package ioswitch2
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "gitlink.org.cn/cloudream/common/pkgs/types"
  7. "gitlink.org.cn/cloudream/common/utils/io2"
  8. "gitlink.org.cn/cloudream/common/utils/serder"
  9. stgglb "gitlink.org.cn/cloudream/jcs-pub/common/globals"
  10. "gitlink.org.cn/cloudream/jcs-pub/common/pkgs/ioswitch/exec"
  11. hubrpc "gitlink.org.cn/cloudream/jcs-pub/common/pkgs/rpc/hub"
  12. jcstypes "gitlink.org.cn/cloudream/jcs-pub/common/types"
  13. )
  14. var _ = serder.UseTypeUnionExternallyTagged(types.Ref(types.NewTypeUnion[exec.WorkerInfo](
  15. (*HubWorker)(nil),
  16. (*HttpHubWorker)(nil),
  17. )))
  18. type HubWorker struct {
  19. Hub jcstypes.Hub
  20. Address jcstypes.GRPCAddressInfo
  21. }
  22. func (w *HubWorker) Name() string {
  23. return fmt.Sprintf("%v", w.Hub.HubID)
  24. }
  25. func (w *HubWorker) NewClient() (exec.WorkerClient, error) {
  26. cli := stgglb.HubRPCPool.Get(stgglb.SelectGRPCAddress(&w.Hub, &w.Address))
  27. return &HubWorkerClient{workerName: w.Name(), hubID: w.Hub.HubID, cli: cli}, nil
  28. }
  29. func (w *HubWorker) String() string {
  30. return w.Hub.String()
  31. }
  32. func (w *HubWorker) Equals(worker exec.WorkerInfo) bool {
  33. aw, ok := worker.(*HubWorker)
  34. if !ok {
  35. return false
  36. }
  37. return w.Hub.HubID == aw.Hub.HubID
  38. }
  39. type HubWorkerClient struct {
  40. workerName string
  41. hubID jcstypes.HubID
  42. cli *hubrpc.Client
  43. }
  44. func (c *HubWorkerClient) ExecutePlan(ctx context.Context, plan exec.Plan) (exec.ExecutorResult, error) {
  45. resp, err := c.cli.ExecuteIOPlan(ctx, &hubrpc.ExecuteIOPlan{Plan: plan, WorkerName: c.workerName})
  46. if err != nil {
  47. return exec.ExecutorResult{}, err.ToError()
  48. }
  49. return resp.Result, nil
  50. }
  51. func (c *HubWorkerClient) SendStream(ctx context.Context, planID exec.PlanID, id exec.VarID, stream io.ReadCloser) error {
  52. _, err := c.cli.SendIOStream(ctx, &hubrpc.SendIOStream{
  53. PlanID: planID,
  54. VarID: id,
  55. Stream: io2.CounterCloser(stream, func(cnt int64, err error) {
  56. if stgglb.Stats.HubTransfer != nil {
  57. stgglb.Stats.HubTransfer.RecordOutput(c.hubID, cnt, err == nil || err == io.EOF)
  58. }
  59. }),
  60. })
  61. return err.ToError()
  62. }
  63. func (c *HubWorkerClient) SendVar(ctx context.Context, planID exec.PlanID, id exec.VarID, value exec.VarValue) error {
  64. _, err := c.cli.SendIOVar(ctx, &hubrpc.SendIOVar{
  65. PlanID: planID, VarID: id, Value: value,
  66. })
  67. return err.ToError()
  68. }
  69. func (c *HubWorkerClient) GetStream(ctx context.Context, planID exec.PlanID, streamID exec.VarID, signalID exec.VarID, signal exec.VarValue) (io.ReadCloser, error) {
  70. resp, err := c.cli.GetIOStream(ctx, &hubrpc.GetIOStream{
  71. PlanID: planID, VarID: streamID, SignalID: signalID, Signal: signal,
  72. })
  73. if err != nil {
  74. return nil, err.ToError()
  75. }
  76. return io2.CounterCloser(resp.Stream, func(cnt int64, err error) {
  77. if stgglb.Stats.HubTransfer != nil {
  78. stgglb.Stats.HubTransfer.RecordInput(c.hubID, cnt, err == nil || err == io.EOF)
  79. }
  80. }), nil
  81. }
  82. func (c *HubWorkerClient) GetVar(ctx context.Context, planID exec.PlanID, varID exec.VarID, signalID exec.VarID, signal exec.VarValue) (exec.VarValue, error) {
  83. resp, err := c.cli.GetIOVar(ctx, &hubrpc.GetIOVar{
  84. PlanID: planID, VarID: varID, SignalID: signalID, Signal: signal,
  85. })
  86. if err != nil {
  87. return nil, err.ToError()
  88. }
  89. return resp.Value, nil
  90. }
  91. func (c *HubWorkerClient) Close() error {
  92. c.cli.Release()
  93. return nil
  94. }

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