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.

http_hub_worker.go 3.1 kB

1 year ago
1 year ago
1 year ago
1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package ioswitch2
  2. import (
  3. "context"
  4. "io"
  5. "strconv"
  6. "gitlink.org.cn/cloudream/common/utils/io2"
  7. stgglb "gitlink.org.cn/cloudream/jcs-pub/common/globals"
  8. "gitlink.org.cn/cloudream/jcs-pub/common/pkgs/ioswitch/exec"
  9. cortypes "gitlink.org.cn/cloudream/jcs-pub/coordinator/types"
  10. hubapi "gitlink.org.cn/cloudream/jcs-pub/hub/sdk/api"
  11. )
  12. type HttpHubWorker struct {
  13. Hub cortypes.Hub
  14. }
  15. func (w *HttpHubWorker) NewClient() (exec.WorkerClient, error) {
  16. addressInfo := w.Hub.Address.(*cortypes.HttpAddressInfo)
  17. baseUrl := "http://" + addressInfo.ExternalIP + ":" + strconv.Itoa(addressInfo.Port)
  18. config := hubapi.Config{
  19. URL: baseUrl,
  20. }
  21. pool := hubapi.NewPool(&config)
  22. cli, err := pool.Acquire()
  23. defer pool.Release(cli)
  24. if err != nil {
  25. return nil, err
  26. }
  27. return &HttpHubWorkerClient{hubID: w.Hub.HubID, cli: cli}, nil
  28. }
  29. func (w *HttpHubWorker) String() string {
  30. return w.Hub.String()
  31. }
  32. func (w *HttpHubWorker) Equals(worker exec.WorkerInfo) bool {
  33. aw, ok := worker.(*HttpHubWorker)
  34. if !ok {
  35. return false
  36. }
  37. return w.Hub.HubID == aw.Hub.HubID
  38. }
  39. type HttpHubWorkerClient struct {
  40. hubID cortypes.HubID
  41. cli *hubapi.Client
  42. }
  43. func (c *HttpHubWorkerClient) ExecutePlan(ctx context.Context, plan exec.Plan) (exec.ExecutorResult, error) {
  44. resp, err := c.cli.ExecuteIOPlan(hubapi.ExecuteIOPlanReq{
  45. Plan: plan,
  46. })
  47. if err != nil {
  48. return exec.ExecutorResult{}, err
  49. }
  50. return resp.Result, nil
  51. }
  52. func (c *HttpHubWorkerClient) SendStream(ctx context.Context, planID exec.PlanID, id exec.VarID, stream io.ReadCloser) error {
  53. return c.cli.SendStream(hubapi.SendStreamReq{
  54. SendStreamInfo: hubapi.SendStreamInfo{
  55. PlanID: planID,
  56. VarID: id,
  57. },
  58. Stream: io2.CounterCloser(stream, func(cnt int64, err error) {
  59. if stgglb.Stats.HubTransfer != nil {
  60. stgglb.Stats.HubTransfer.RecordOutput(c.hubID, cnt, err == nil || err == io.EOF)
  61. }
  62. }),
  63. })
  64. }
  65. func (c *HttpHubWorkerClient) SendVar(ctx context.Context, planID exec.PlanID, id exec.VarID, value exec.VarValue) error {
  66. return c.cli.SendVar(hubapi.SendVarReq{
  67. PlanID: planID,
  68. VarID: id,
  69. VarValue: value,
  70. })
  71. }
  72. func (c *HttpHubWorkerClient) GetStream(ctx context.Context, planID exec.PlanID, streamID exec.VarID, signalID exec.VarID, signal exec.VarValue) (io.ReadCloser, error) {
  73. str, err := c.cli.GetStream(hubapi.GetStreamReq{
  74. PlanID: planID,
  75. VarID: streamID,
  76. SignalID: signalID,
  77. Signal: signal,
  78. })
  79. if err != nil {
  80. return nil, err
  81. }
  82. return io2.CounterCloser(str, func(cnt int64, err error) {
  83. if stgglb.Stats.HubTransfer != nil {
  84. stgglb.Stats.HubTransfer.RecordInput(c.hubID, cnt, err == nil || err == io.EOF)
  85. }
  86. }), nil
  87. }
  88. func (c *HttpHubWorkerClient) GetVar(ctx context.Context, planID exec.PlanID, varID exec.VarID, signalID exec.VarID, signal exec.VarValue) (exec.VarValue, error) {
  89. resp, err := c.cli.GetVar(hubapi.GetVarReq{
  90. PlanID: planID,
  91. VarID: varID,
  92. SignalID: signalID,
  93. Signal: signal,
  94. })
  95. if err != nil {
  96. return nil, err
  97. }
  98. return resp.Value, err
  99. }
  100. func (c *HttpHubWorkerClient) Close() error {
  101. //stgglb.HubRPCPool.Release(c.cli)
  102. return nil
  103. }

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