Skip to content

Commit 9360683

Browse files
committed
Add user agent
1 parent 8bcf754 commit 9360683

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

internal/tasks/controller-registry.go

+16-5
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,15 @@ import (
1616
"github.com/nextmn/srv6/internal/ctrl"
1717
)
1818

19+
const UserAgent = "go-github-nextmn-srv6"
20+
1921
// ControllerRegistry registers and unregisters into controller
2022
type ControllerRegistryTask struct {
2123
WithName
2224
WithState
2325
ControllerRegistry *ctrl.ControllerRegistry
2426
SetupRegistry app_api.Registry
27+
httpClient http.Client
2528
}
2629

2730
// Create a new ControllerRegistry
@@ -37,6 +40,7 @@ func NewControllerRegistryTask(name string, remoteControlURI string, backbone ne
3740
Resource: "",
3841
},
3942
SetupRegistry: setup_registry,
43+
httpClient: http.Client{},
4044
}
4145
}
4246

@@ -56,8 +60,14 @@ func (t *ControllerRegistryTask) RunInit(ctx context.Context) error {
5660
if err != nil {
5761
return err
5862
}
59-
// TODO: retry on timeout failure
60-
resp, err := http.Post(t.ControllerRegistry.RemoteControlURI+"/routers", "application/json", bytes.NewBuffer(json_data))
63+
// TODO: retry on timeout failure (use a new ctx)
64+
req, err := http.NewRequestWithContext(ctx, http.MethodPost, t.ControllerRegistry.RemoteControlURI+"/routers", bytes.NewBuffer(json_data))
65+
if err != nil {
66+
return err
67+
}
68+
req.Header.Add("User-Agent", UserAgent)
69+
req.Header.Set("Content-Type", "application/json; charset=UTF-8")
70+
resp, err := t.httpClient.Do(req)
6171
if err != nil {
6272
return err
6373
}
@@ -89,12 +99,13 @@ func (t *ControllerRegistryTask) RunExit() error {
8999
t.state = false
90100
return nil
91101
}
92-
req, err := http.NewRequest("DELETE", t.ControllerRegistry.RemoteControlURI+t.ControllerRegistry.Resource, nil)
102+
// no context since Background Context is already Done
103+
req, err := http.NewRequest(http.MethodDelete, t.ControllerRegistry.RemoteControlURI+t.ControllerRegistry.Resource, nil)
93104
if err != nil {
94105
return err
95106
}
96-
client := &http.Client{}
97-
resp, err := client.Do(req)
107+
req.Header.Add("User-Agent", UserAgent)
108+
resp, err := t.httpClient.Do(req)
98109
if err != nil {
99110
return err
100111
}

0 commit comments

Comments
 (0)