@@ -16,12 +16,15 @@ import (
16
16
"github.com/nextmn/srv6/internal/ctrl"
17
17
)
18
18
19
+ const UserAgent = "go-github-nextmn-srv6"
20
+
19
21
// ControllerRegistry registers and unregisters into controller
20
22
type ControllerRegistryTask struct {
21
23
WithName
22
24
WithState
23
25
ControllerRegistry * ctrl.ControllerRegistry
24
26
SetupRegistry app_api.Registry
27
+ httpClient http.Client
25
28
}
26
29
27
30
// Create a new ControllerRegistry
@@ -37,6 +40,7 @@ func NewControllerRegistryTask(name string, remoteControlURI string, backbone ne
37
40
Resource : "" ,
38
41
},
39
42
SetupRegistry : setup_registry ,
43
+ httpClient : http.Client {},
40
44
}
41
45
}
42
46
@@ -56,8 +60,14 @@ func (t *ControllerRegistryTask) RunInit(ctx context.Context) error {
56
60
if err != nil {
57
61
return err
58
62
}
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 )
61
71
if err != nil {
62
72
return err
63
73
}
@@ -89,12 +99,13 @@ func (t *ControllerRegistryTask) RunExit() error {
89
99
t .state = false
90
100
return nil
91
101
}
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 )
93
104
if err != nil {
94
105
return err
95
106
}
96
- client := & http. Client {}
97
- resp , err := client .Do (req )
107
+ req . Header . Add ( "User-Agent" , UserAgent )
108
+ resp , err := t . httpClient .Do (req )
98
109
if err != nil {
99
110
return err
100
111
}
0 commit comments