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

1 year ago
1 year ago
1 year ago
1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package ioswitch2
  2. import (
  3. "context"
  4. "io"
  5. "strconv"
  6. "gitlink.org.cn/cloudream/common/pkgs/ioswitch/exec"
  7. "gitlink.org.cn/cloudream/common/sdks/storage/cdsapi"
  8. "gitlink.org.cn/cloudream/common/utils/io2"
  9. stgglb "gitlink.org.cn/cloudream/jcs-pub/common/globals"
  10. cortypes "gitlink.org.cn/cloudream/jcs-pub/coordinator/types"
  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 := cdsapi.Config{
  19. URL: baseUrl,
  20. }
  21. pool := cdsapi.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 *cdsapi.Client
  42. }
  43. func (c *HttpHubWorkerClient) ExecutePlan(ctx context.Context, plan exec.Plan) error {
  44. return c.cli.ExecuteIOPlan(cdsapi.ExecuteIOPlanReq{
  45. Plan: plan,
  46. })
  47. }
  48. func (c *HttpHubWorkerClient) SendStream(ctx context.Context, planID exec.PlanID, id exec.VarID, stream io.ReadCloser) error {
  49. return c.cli.SendStream(cdsapi.SendStreamReq{
  50. SendStreamInfo: cdsapi.SendStreamInfo{
  51. PlanID: planID,
  52. VarID: id,
  53. },
  54. Stream: io2.CounterCloser(stream, func(cnt int64, err error) {
  55. if stgglb.Stats.HubTransfer != nil {
  56. stgglb.Stats.HubTransfer.RecordOutput(c.hubID, cnt, err == nil || err == io.EOF)
  57. }
  58. }),
  59. })
  60. }
  61. func (c *HttpHubWorkerClient) SendVar(ctx context.Context, planID exec.PlanID, id exec.VarID, value exec.VarValue) error {
  62. return c.cli.SendVar(cdsapi.SendVarReq{
  63. PlanID: planID,
  64. VarID: id,
  65. VarValue: value,
  66. })
  67. }
  68. func (c *HttpHubWorkerClient) GetStream(ctx context.Context, planID exec.PlanID, streamID exec.VarID, signalID exec.VarID, signal exec.VarValue) (io.ReadCloser, error) {
  69. str, err := c.cli.GetStream(cdsapi.GetStreamReq{
  70. PlanID: planID,
  71. VarID: streamID,
  72. SignalID: signalID,
  73. Signal: signal,
  74. })
  75. if err != nil {
  76. return nil, err
  77. }
  78. return io2.CounterCloser(str, func(cnt int64, err error) {
  79. if stgglb.Stats.HubTransfer != nil {
  80. stgglb.Stats.HubTransfer.RecordInput(c.hubID, cnt, err == nil || err == io.EOF)
  81. }
  82. }), nil
  83. }
  84. func (c *HttpHubWorkerClient) GetVar(ctx context.Context, planID exec.PlanID, varID exec.VarID, signalID exec.VarID, signal exec.VarValue) (exec.VarValue, error) {
  85. resp, err := c.cli.GetVar(cdsapi.GetVarReq{
  86. PlanID: planID,
  87. VarID: varID,
  88. SignalID: signalID,
  89. Signal: signal,
  90. })
  91. if err != nil {
  92. return nil, err
  93. }
  94. return resp.Value, err
  95. }
  96. func (c *HttpHubWorkerClient) Close() error {
  97. //stgglb.HubRPCPool.Release(c.cli)
  98. return nil
  99. }

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