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.

server.go 1.9 kB

2 years ago
1 year ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package coordinator
  2. import (
  3. "gitlink.org.cn/cloudream/common/pkgs/mq"
  4. "gitlink.org.cn/cloudream/common/utils/sync2"
  5. mymq "gitlink.org.cn/cloudream/storage/common/pkgs/mq"
  6. )
  7. // Service 协调端接口
  8. type Service interface {
  9. AgentService
  10. BucketService
  11. CacheService
  12. HubService
  13. ObjectService
  14. PackageService
  15. StorageService
  16. }
  17. type Server struct {
  18. service Service
  19. rabbitSvr mq.RabbitMQServer
  20. }
  21. func NewServer(svc Service, cfg *mymq.Config) (*Server, error) {
  22. srv := &Server{
  23. service: svc,
  24. }
  25. rabbitSvr, err := mq.NewRabbitMQServer(
  26. cfg.MakeConnectingURL(),
  27. mymq.COORDINATOR_QUEUE_NAME,
  28. func(msg *mq.Message) (*mq.Message, error) {
  29. return msgDispatcher.Handle(srv.service, msg)
  30. },
  31. cfg.Param,
  32. )
  33. if err != nil {
  34. return nil, err
  35. }
  36. srv.rabbitSvr = *rabbitSvr
  37. return srv, nil
  38. }
  39. func (s *Server) Stop() {
  40. s.rabbitSvr.Close()
  41. }
  42. func (s *Server) Start(cfg mymq.Config) *sync2.UnboundChannel[mq.RabbitMQServerEvent] {
  43. return s.rabbitSvr.Start()
  44. }
  45. func (s *Server) OnError(callback func(error)) {
  46. s.rabbitSvr.OnError = callback
  47. }
  48. var msgDispatcher mq.MessageDispatcher = mq.NewMessageDispatcher()
  49. // Register 将Service中的一个接口函数作为指定类型消息的处理函数,同时会注册请求和响应的消息类型
  50. // TODO 需要约束:Service实现了TSvc接口
  51. func Register[TReq mq.MessageBody, TResp mq.MessageBody](svcFn func(svc Service, msg TReq) (TResp, *mq.CodeMessage)) any {
  52. mq.AddServiceFn(&msgDispatcher, svcFn)
  53. mq.RegisterMessage[TReq]()
  54. mq.RegisterMessage[TResp]()
  55. return nil
  56. }
  57. // RegisterNoReply 将Service中的一个*没有返回值的*接口函数作为指定类型消息的处理函数,同时会注册请求和响应的消息类型
  58. // TODO 需要约束:Service实现了TSvc接口
  59. func RegisterNoReply[TReq mq.MessageBody](svcFn func(svc Service, msg TReq)) any {
  60. mq.AddNoRespServiceFn(&msgDispatcher, svcFn)
  61. mq.RegisterMessage[TReq]()
  62. return nil
  63. }

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