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

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

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