message.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Copyright 2023 The frp Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package transport
  15. import (
  16. "context"
  17. "reflect"
  18. "sync"
  19. "github.com/fatedier/golib/errors"
  20. "github.com/fatedier/frp/pkg/msg"
  21. )
  22. type MessageTransporter interface {
  23. Send(msg.Message) error
  24. // Recv(ctx context.Context, laneKey string, msgType string) (Message, error)
  25. // Do will first send msg, then recv msg with the same laneKey and specified msgType.
  26. Do(ctx context.Context, req msg.Message, laneKey, recvMsgType string) (msg.Message, error)
  27. // Dispatch will dispatch message to related channel registered in Do function by its message type and laneKey.
  28. Dispatch(m msg.Message, laneKey string) bool
  29. // Same with Dispatch but with specified message type.
  30. DispatchWithType(m msg.Message, msgType, laneKey string) bool
  31. }
  32. type MessageSender interface {
  33. Send(msg.Message) error
  34. }
  35. func NewMessageTransporter(sender MessageSender) MessageTransporter {
  36. return &transporterImpl{
  37. sender: sender,
  38. registry: make(map[string]map[string]chan msg.Message),
  39. }
  40. }
  41. type transporterImpl struct {
  42. sender MessageSender
  43. // First key is message type and second key is lane key.
  44. // Dispatch will dispatch message to related channel by its message type
  45. // and lane key.
  46. registry map[string]map[string]chan msg.Message
  47. mu sync.RWMutex
  48. }
  49. func (impl *transporterImpl) Send(m msg.Message) error {
  50. return impl.sender.Send(m)
  51. }
  52. func (impl *transporterImpl) Do(ctx context.Context, req msg.Message, laneKey, recvMsgType string) (msg.Message, error) {
  53. ch := make(chan msg.Message, 1)
  54. defer close(ch)
  55. unregisterFn := impl.registerMsgChan(ch, laneKey, recvMsgType)
  56. defer unregisterFn()
  57. if err := impl.Send(req); err != nil {
  58. return nil, err
  59. }
  60. select {
  61. case <-ctx.Done():
  62. return nil, ctx.Err()
  63. case resp := <-ch:
  64. return resp, nil
  65. }
  66. }
  67. func (impl *transporterImpl) DispatchWithType(m msg.Message, msgType, laneKey string) bool {
  68. var ch chan msg.Message
  69. impl.mu.RLock()
  70. byLaneKey, ok := impl.registry[msgType]
  71. if ok {
  72. ch = byLaneKey[laneKey]
  73. }
  74. impl.mu.RUnlock()
  75. if ch == nil {
  76. return false
  77. }
  78. if err := errors.PanicToError(func() {
  79. ch <- m
  80. }); err != nil {
  81. return false
  82. }
  83. return true
  84. }
  85. func (impl *transporterImpl) Dispatch(m msg.Message, laneKey string) bool {
  86. msgType := reflect.TypeOf(m).Elem().Name()
  87. return impl.DispatchWithType(m, msgType, laneKey)
  88. }
  89. func (impl *transporterImpl) registerMsgChan(recvCh chan msg.Message, laneKey string, msgType string) (unregister func()) {
  90. impl.mu.Lock()
  91. byLaneKey, ok := impl.registry[msgType]
  92. if !ok {
  93. byLaneKey = make(map[string]chan msg.Message)
  94. impl.registry[msgType] = byLaneKey
  95. }
  96. byLaneKey[laneKey] = recvCh
  97. impl.mu.Unlock()
  98. unregister = func() {
  99. impl.mu.Lock()
  100. delete(byLaneKey, laneKey)
  101. impl.mu.Unlock()
  102. }
  103. return
  104. }