forked from porthos-rpc/porthos-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspec_shipper.go
83 lines (66 loc) · 1.82 KB
/
spec_shipper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package porthos
import (
"encoding/json"
"time"
"github.com/porthos-rpc/porthos-go/log"
"github.com/streadway/amqp"
)
const specsQueueName = "porthos.specs"
type specEntry struct {
Service string `json:"service"`
Specs map[string]Spec `json:"specs"`
}
// SpecShipperExtension logs incoming requests and outgoing responses.
type SpecShipperExtension struct {
b *Broker
}
// ServerListening takes all registered method specs and ships to the broker.
func (s *SpecShipperExtension) ServerListening(srv Server) {
ch, err := s.b.openChannel()
if err != nil {
log.Error("Error opening channel for the spec shipper.")
return
}
defer ch.Close()
_, err = ch.QueueDeclare(
specsQueueName, // name
true, // durable
false, // delete when usused
false, // exclusive
false, // noWait
nil, // arguments
)
if err != nil {
log.Error("Error declaring the specs queue", err)
return
}
payload, err := json.Marshal(specEntry{
Service: srv.GetServiceName(),
Specs: srv.GetSpecs(),
})
if err != nil {
log.Error("Error creating specs payload", err)
return
}
err = ch.Publish(
"",
specsQueueName,
false,
false,
amqp.Publishing{
ContentType: "application/json",
Body: payload,
})
if err != nil {
log.Error("Error publishing specs to the broker", err)
}
}
// IncomingRequest this is not implemented in this extension.
func (s *SpecShipperExtension) IncomingRequest(req Request) {}
// OutgoingResponse this is not implemented in this extension.
func (s *SpecShipperExtension) OutgoingResponse(req Request, res Response, resTime time.Duration, statusCode int32) {
}
// NewSpecShipperExtension creates a new extension that ship method specs to the broker.
func NewSpecShipperExtension(b *Broker) Extension {
return &SpecShipperExtension{b}
}