-
Notifications
You must be signed in to change notification settings - Fork 62
/
httpclient.go
98 lines (76 loc) · 2.5 KB
/
httpclient.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package client
import (
"context"
"github.com/open-telemetry/opamp-go/client/internal"
"github.com/open-telemetry/opamp-go/client/types"
sharedinternal "github.com/open-telemetry/opamp-go/internal"
"github.com/open-telemetry/opamp-go/protobufs"
)
// httpClient is an OpAMP Client implementation for plain HTTP transport.
// See specification: https://github.com/open-telemetry/opamp-spec/blob/main/specification.md#plain-http-transport
type httpClient struct {
common internal.ClientCommon
opAMPServerURL string
// The sender performs HTTP request/response loop.
sender *internal.HTTPSender
}
func NewHTTP(logger types.Logger) *httpClient {
if logger == nil {
logger = &sharedinternal.NopLogger{}
}
sender := internal.NewHTTPSender(logger)
w := &httpClient{
common: internal.NewClientCommon(logger, sender),
sender: sender,
}
return w
}
func (c *httpClient) Start(ctx context.Context, settings types.StartSettings) error {
if err := c.common.PrepareStart(ctx, settings); err != nil {
return err
}
c.opAMPServerURL = settings.OpAMPServerURL
// Prepare Server connection settings.
c.sender.SetRequestHeader(settings.Header)
// Prepare the first message to send.
err := c.common.PrepareFirstMessage(ctx)
if err != nil {
return err
}
c.sender.ScheduleSend()
c.common.StartConnectAndRun(c.runUntilStopped)
return nil
}
func (c *httpClient) Stop(ctx context.Context) error {
return c.common.Stop(ctx)
}
func (c *httpClient) AgentDescription() *protobufs.AgentDescription {
return c.common.AgentDescription()
}
func (c *httpClient) SetAgentDescription(descr *protobufs.AgentDescription) error {
return c.common.SetAgentDescription(descr)
}
func (c *httpClient) SetHealth(health *protobufs.AgentHealth) error {
return c.common.SetHealth(health)
}
func (c *httpClient) UpdateEffectiveConfig(ctx context.Context) error {
return c.common.UpdateEffectiveConfig(ctx)
}
func (c *httpClient) SetRemoteConfigStatus(status *protobufs.RemoteConfigStatus) error {
return c.common.SetRemoteConfigStatus(status)
}
func (c *httpClient) SetPackageStatuses(statuses *protobufs.PackageStatuses) error {
return c.common.SetPackageStatuses(statuses)
}
func (c *httpClient) runUntilStopped(ctx context.Context) {
// Start the HTTP sender. This will make request/responses with retries for
// failures and will wait with configured polling interval if there is nothing
// to send.
c.sender.Run(
ctx,
c.opAMPServerURL,
c.common.Callbacks,
&c.common.ClientSyncedState,
c.common.PackagesStateProvider,
)
}