message.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. func NewMessageTransporter(sendCh chan msg.Message) MessageTransporter {
  33. return &transporterImpl{
  34. sendCh: sendCh,
  35. registry: make(map[string]map[string]chan msg.Message),
  36. }
  37. }
  38. type transporterImpl struct {
  39. sendCh chan msg.Message
  40. // First key is message type and second key is lane key.
  41. // Dispatch will dispatch message to related channel by its message type
  42. // and lane key.
  43. registry map[string]map[string]chan msg.Message
  44. mu sync.RWMutex
  45. }
  46. func (impl *transporterImpl) Send(m msg.Message) error {
  47. return errors.PanicToError(func() {
  48. impl.sendCh <- m
  49. })
  50. }
  51. func (impl *transporterImpl) Do(ctx context.Context, req msg.Message, laneKey, recvMsgType string) (msg.Message, error) {
  52. ch := make(chan msg.Message, 1)
  53. defer close(ch)
  54. unregisterFn := impl.registerMsgChan(ch, laneKey, recvMsgType)
  55. defer unregisterFn()
  56. if err := impl.Send(req); err != nil {
  57. return nil, err
  58. }
  59. select {
  60. case <-ctx.Done():
  61. return nil, ctx.Err()
  62. case resp := <-ch:
  63. return resp, nil
  64. }
  65. }
  66. func (impl *transporterImpl) DispatchWithType(m msg.Message, msgType, laneKey string) bool {
  67. var ch chan msg.Message
  68. impl.mu.RLock()
  69. byLaneKey, ok := impl.registry[msgType]
  70. if ok {
  71. ch = byLaneKey[laneKey]
  72. }
  73. impl.mu.RUnlock()
  74. if ch == nil {
  75. return false
  76. }
  77. if err := errors.PanicToError(func() {
  78. ch <- m
  79. }); err != nil {
  80. return false
  81. }
  82. return true
  83. }
  84. func (impl *transporterImpl) Dispatch(m msg.Message, laneKey string) bool {
  85. msgType := reflect.TypeOf(m).Elem().Name()
  86. return impl.DispatchWithType(m, msgType, laneKey)
  87. }
  88. func (impl *transporterImpl) registerMsgChan(recvCh chan msg.Message, laneKey string, msgType string) (unregister func()) {
  89. impl.mu.Lock()
  90. byLaneKey, ok := impl.registry[msgType]
  91. if !ok {
  92. byLaneKey = make(map[string]chan msg.Message)
  93. impl.registry[msgType] = byLaneKey
  94. }
  95. byLaneKey[laneKey] = recvCh
  96. impl.mu.Unlock()
  97. unregister = func() {
  98. impl.mu.Lock()
  99. delete(byLaneKey, laneKey)
  100. impl.mu.Unlock()
  101. }
  102. return
  103. }