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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package coordinator
  2. import (
  3. "gitlink.org.cn/cloudream/common/pkgs/mq"
  4. mymq "gitlink.org.cn/cloudream/storage-common/pkgs/mq"
  5. )
  6. // Service 协调端接口
  7. type Service interface {
  8. AgentService
  9. BucketService
  10. CacheService
  11. CommonService
  12. NodeService
  13. ObjectService
  14. PackageService
  15. StorageService
  16. }
  17. type Server struct {
  18. service Service
  19. rabbitSvr mq.RabbitMQServer
  20. OnError func(err error)
  21. }
  22. func NewServer(svc Service, cfg *mymq.Config) (*Server, error) {
  23. srv := &Server{
  24. service: svc,
  25. }
  26. rabbitSvr, err := mq.NewRabbitMQServer(
  27. cfg.MakeConnectingURL(),
  28. mymq.COORDINATOR_QUEUE_NAME,
  29. func(msg *mq.Message) (*mq.Message, error) {
  30. return msgDispatcher.Handle(srv.service, msg)
  31. },
  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) Serve() error {
  43. return s.rabbitSvr.Serve()
  44. }
  45. var msgDispatcher mq.MessageDispatcher = mq.NewMessageDispatcher()
  46. // Register 将Service中的一个接口函数作为指定类型消息的处理函数,同时会注册请求和响应的消息类型
  47. // TODO 需要约束:Service实现了TSvc接口
  48. func Register[TSvc any, TReq any, TResp any](svcFn func(svc TSvc, msg *TReq) (*TResp, *mq.CodeMessage)) any {
  49. mq.AddServiceFn(&msgDispatcher, svcFn)
  50. mq.RegisterMessage[TReq]()
  51. mq.RegisterMessage[TResp]()
  52. return nil
  53. }
  54. // RegisterNoReply 将Service中的一个*没有返回值的*接口函数作为指定类型消息的处理函数,同时会注册请求和响应的消息类型
  55. // TODO 需要约束:Service实现了TSvc接口
  56. func RegisterNoReply[TSvc any, TReq any](svcFn func(svc TSvc, msg *TReq)) any {
  57. mq.AddNoRespServiceFn(&msgDispatcher, svcFn)
  58. mq.RegisterMessage[TReq]()
  59. return nil
  60. }

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