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 2.6 kB

1 year ago
1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package ioswitch2
  2. import (
  3. "context"
  4. "io"
  5. "gitlink.org.cn/cloudream/common/pkgs/ioswitch/exec"
  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. hubrpc "gitlink.org.cn/cloudream/jcs-pub/common/pkgs/grpc/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, err := stgglb.HubRPCPool.Acquire(stgglb.SelectGRPCAddress(w.Hub, w.Address))
  23. if err != nil {
  24. return nil, err
  25. }
  26. return &HubWorkerClient{hubID: w.Hub.HubID, cli: cli}, nil
  27. }
  28. func (w *HubWorker) String() string {
  29. return w.Hub.String()
  30. }
  31. func (w *HubWorker) Equals(worker exec.WorkerInfo) bool {
  32. aw, ok := worker.(*HubWorker)
  33. if !ok {
  34. return false
  35. }
  36. return w.Hub.HubID == aw.Hub.HubID
  37. }
  38. type HubWorkerClient struct {
  39. hubID cortypes.HubID
  40. cli *hubrpc.PoolClient
  41. }
  42. func (c *HubWorkerClient) ExecutePlan(ctx context.Context, plan exec.Plan) error {
  43. return c.cli.ExecuteIOPlan(ctx, plan)
  44. }
  45. func (c *HubWorkerClient) SendStream(ctx context.Context, planID exec.PlanID, id exec.VarID, stream io.ReadCloser) error {
  46. return c.cli.SendStream(ctx, planID, id, io2.CounterCloser(stream, func(cnt int64, err error) {
  47. if stgglb.Stats.HubTransfer != nil {
  48. stgglb.Stats.HubTransfer.RecordOutput(c.hubID, cnt, err == nil || err == io.EOF)
  49. }
  50. }))
  51. }
  52. func (c *HubWorkerClient) SendVar(ctx context.Context, planID exec.PlanID, id exec.VarID, value exec.VarValue) error {
  53. return c.cli.SendVar(ctx, planID, id, value)
  54. }
  55. func (c *HubWorkerClient) GetStream(ctx context.Context, planID exec.PlanID, streamID exec.VarID, signalID exec.VarID, signal exec.VarValue) (io.ReadCloser, error) {
  56. str, err := c.cli.GetStream(ctx, planID, streamID, signalID, signal)
  57. if err != nil {
  58. return nil, err
  59. }
  60. return io2.CounterCloser(str, func(cnt int64, err error) {
  61. if stgglb.Stats.HubTransfer != nil {
  62. stgglb.Stats.HubTransfer.RecordInput(c.hubID, cnt, err == nil || err == io.EOF)
  63. }
  64. }), nil
  65. }
  66. func (c *HubWorkerClient) GetVar(ctx context.Context, planID exec.PlanID, varID exec.VarID, signalID exec.VarID, signal exec.VarValue) (exec.VarValue, error) {
  67. return c.cli.GetVar(ctx, planID, varID, signalID, signal)
  68. }
  69. func (c *HubWorkerClient) Close() error {
  70. stgglb.HubRPCPool.Release(c.cli)
  71. return nil
  72. }

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