| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package rpc |
| 17 |
|
| 18 |
import ( |
| 19 |
"context" |
| 20 |
"fmt" |
| 21 |
"sync" |
| 22 |
"time" |
| 23 |
|
| 24 |
"github.com/eclipse/paho.golang/autopaho" |
| 25 |
"github.com/eclipse/paho.golang/paho" |
| 26 |
) |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
type Handler struct { |
| 31 |
sync.Mutex |
| 32 |
cm *autopaho.ConnectionManager |
| 33 |
correlData map[string]chan *paho.Publish |
| 34 |
responseTopic string |
| 35 |
} |
| 36 |
|
| 37 |
type HandlerOpts struct { |
| 38 |
Conn *autopaho.ConnectionManager |
| 39 |
Router paho.Router |
| 40 |
ResponseTopicFmt string |
| 41 |
ClientID string |
| 42 |
} |
| 43 |
|
| 44 |
func NewHandler(ctx context.Context, opts HandlerOpts) (*Handler, error) { |
| 45 |
h := &Handler{ |
| 46 |
cm: opts.Conn, |
| 47 |
correlData: make(map[string]chan *paho.Publish), |
| 48 |
} |
| 49 |
|
| 50 |
h.responseTopic = fmt.Sprintf(opts.ResponseTopicFmt, opts.ClientID) |
| 51 |
|
| 52 |
opts.Router.RegisterHandler(h.responseTopic, h.responseHandler) |
| 53 |
|
| 54 |
_, err := opts.Conn.Subscribe(ctx, &paho.Subscribe{ |
| 55 |
Subscriptions: []paho.SubscribeOptions{ |
| 56 |
{Topic: h.responseTopic, QoS: 1}, |
| 57 |
}, |
| 58 |
}) |
| 59 |
if err != nil { |
| 60 |
return nil, err |
| 61 |
} |
| 62 |
|
| 63 |
return h, nil |
| 64 |
} |
| 65 |
|
| 66 |
func (h *Handler) addCorrelID(cID string, r chan *paho.Publish) { |
| 67 |
h.Lock() |
| 68 |
defer h.Unlock() |
| 69 |
|
| 70 |
h.correlData[cID] = r |
| 71 |
} |
| 72 |
|
| 73 |
func (h *Handler) getCorrelIDChan(cID string) chan *paho.Publish { |
| 74 |
h.Lock() |
| 75 |
defer h.Unlock() |
| 76 |
|
| 77 |
rChan := h.correlData[cID] |
| 78 |
delete(h.correlData, cID) |
| 79 |
|
| 80 |
return rChan |
| 81 |
} |
| 82 |
|
| 83 |
func (h *Handler) Request(ctx context.Context, pb *paho.Publish) (resp *paho.Publish, err error) { |
| 84 |
cID := fmt.Sprintf("%d", time.Now().UnixNano()) |
| 85 |
rChan := make(chan *paho.Publish) |
| 86 |
|
| 87 |
h.addCorrelID(cID, rChan) |
| 88 |
|
| 89 |
if pb.Properties == nil { |
| 90 |
pb.Properties = &paho.PublishProperties{} |
| 91 |
} |
| 92 |
|
| 93 |
pb.Properties.CorrelationData = []byte(cID) |
| 94 |
pb.Properties.ResponseTopic = h.responseTopic |
| 95 |
pb.Retain = false |
| 96 |
|
| 97 |
_, err = h.cm.Publish(ctx, pb) |
| 98 |
if err != nil { |
| 99 |
return nil, err |
| 100 |
} |
| 101 |
|
| 102 |
select { |
| 103 |
case <-ctx.Done(): |
| 104 |
_ = h.getCorrelIDChan(string(pb.Properties.CorrelationData)) |
| 105 |
return nil, fmt.Errorf("context ended") |
| 106 |
case resp = <-rChan: |
| 107 |
return resp, nil |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
func (h *Handler) responseHandler(pb *paho.Publish) { |
| 112 |
if pb.Properties == nil || pb.Properties.CorrelationData == nil { |
| 113 |
return |
| 114 |
} |
| 115 |
|
| 116 |
rChan := h.getCorrelIDChan(string(pb.Properties.CorrelationData)) |
| 117 |
if rChan == nil { |
| 118 |
return |
| 119 |
} |
| 120 |
|
| 121 |
select { |
| 122 |
case rChan <- pb: |
| 123 |
return |
| 124 |
default: |
| 125 |
return |
| 126 |
} |
| 127 |
} |