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

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

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