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.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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) NewClient() (exec.WorkerClient, error) {
  23. cli := stgglb.HubRPCPool.Get(stgglb.SelectGRPCAddress(&w.Hub, &w.Address))
  24. return &HubWorkerClient{hubID: w.Hub.HubID, cli: cli}, nil
  25. }
  26. func (w *HubWorker) String() string {
  27. return w.Hub.String()
  28. }
  29. func (w *HubWorker) Equals(worker exec.WorkerInfo) bool {
  30. aw, ok := worker.(*HubWorker)
  31. if !ok {
  32. return false
  33. }
  34. return w.Hub.HubID == aw.Hub.HubID
  35. }
  36. type HubWorkerClient struct {
  37. hubID jcstypes.HubID
  38. cli *hubrpc.Client
  39. }
  40. func (c *HubWorkerClient) ExecutePlan(ctx context.Context, plan exec.Plan) (exec.ExecutorResult, error) {
  41. resp, err := c.cli.ExecuteIOPlan(ctx, &hubrpc.ExecuteIOPlan{Plan: plan, WorkerName: fmt.Sprintf("%v", c.hubID)})
  42. if err != nil {
  43. return exec.ExecutorResult{}, err.ToError()
  44. }
  45. return resp.Result, nil
  46. }
  47. func (c *HubWorkerClient) SendStream(ctx context.Context, planID exec.PlanID, id exec.VarID, stream io.ReadCloser) error {
  48. _, err := c.cli.SendIOStream(ctx, &hubrpc.SendIOStream{
  49. PlanID: planID,
  50. VarID: id,
  51. Stream: io2.CounterCloser(stream, func(cnt int64, err error) {
  52. if stgglb.Stats.HubTransfer != nil {
  53. stgglb.Stats.HubTransfer.RecordOutput(c.hubID, cnt, err == nil || err == io.EOF)
  54. }
  55. }),
  56. })
  57. return err.ToError()
  58. }
  59. func (c *HubWorkerClient) SendVar(ctx context.Context, planID exec.PlanID, id exec.VarID, value exec.VarValue) error {
  60. _, err := c.cli.SendIOVar(ctx, &hubrpc.SendIOVar{
  61. PlanID: planID, VarID: id, Value: value,
  62. })
  63. return err.ToError()
  64. }
  65. func (c *HubWorkerClient) GetStream(ctx context.Context, planID exec.PlanID, streamID exec.VarID, signalID exec.VarID, signal exec.VarValue) (io.ReadCloser, error) {
  66. resp, err := c.cli.GetIOStream(ctx, &hubrpc.GetIOStream{
  67. PlanID: planID, VarID: streamID, SignalID: signalID, Signal: signal,
  68. })
  69. if err != nil {
  70. return nil, err.ToError()
  71. }
  72. return io2.CounterCloser(resp.Stream, func(cnt int64, err error) {
  73. if stgglb.Stats.HubTransfer != nil {
  74. stgglb.Stats.HubTransfer.RecordInput(c.hubID, cnt, err == nil || err == io.EOF)
  75. }
  76. }), nil
  77. }
  78. func (c *HubWorkerClient) GetVar(ctx context.Context, planID exec.PlanID, varID exec.VarID, signalID exec.VarID, signal exec.VarValue) (exec.VarValue, error) {
  79. resp, err := c.cli.GetIOVar(ctx, &hubrpc.GetIOVar{
  80. PlanID: planID, VarID: varID, SignalID: signalID, Signal: signal,
  81. })
  82. if err != nil {
  83. return nil, err.ToError()
  84. }
  85. return resp.Value, nil
  86. }
  87. func (c *HubWorkerClient) Close() error {
  88. c.cli.Release()
  89. return nil
  90. }

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