forked from elastic/apm-agent-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chan.go
70 lines (63 loc) · 1.99 KB
/
chan.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
package transporttest
import (
"context"
"github.com/elastic/apm-agent-go/model"
)
// ChannelTransport implements transport.Transport,
// sending payloads to the provided channels as
// request objects. Once a request object has been
// received, an error should be sent to its Result
// channel to unblock the tracer.
type ChannelTransport struct {
Transactions chan<- SendTransactionsRequest
Errors chan<- SendErrorsRequest
}
// SendTransactionsRequest is the type of values sent over the
// ChannelTransport.Transactions channel when its SendTransactions
// method is called.
type SendTransactionsRequest struct {
Payload *model.TransactionsPayload
Result chan<- error
}
// SendErrorsRequest is the type of values sent over the
// ChannelTransport.Errors channel when its SendErrors
// method is called.
type SendErrorsRequest struct {
Payload *model.ErrorsPayload
Result chan<- error
}
// SendTransactions sends a SendTransactionsRequest value over the
// c.Transactions channel with the given payload, and waits for a
// response on the error channel included in the request, or for
// the context to be canceled.
func (c *ChannelTransport) SendTransactions(ctx context.Context, payload *model.TransactionsPayload) error {
result := make(chan error, 1)
select {
case <-ctx.Done():
return ctx.Err()
case c.Transactions <- SendTransactionsRequest{payload, result}:
select {
case <-ctx.Done():
return ctx.Err()
case err := <-result:
return err
}
}
}
// SendErrors sends a SendErrorsRequest value over the c.Errors channel
// with the given payload, and waits for a response on the error channel
// included in the request, or for the context to be canceled.
func (c *ChannelTransport) SendErrors(ctx context.Context, payload *model.ErrorsPayload) error {
result := make(chan error, 1)
select {
case <-ctx.Done():
return ctx.Err()
case c.Errors <- SendErrorsRequest{payload, result}:
select {
case <-ctx.Done():
return ctx.Err()
case err := <-result:
return err
}
}
}