@@ -26,7 +26,12 @@ import (
26
26
ops "github.com/firecracker-microvm/firecracker-go-sdk/client/operations"
27
27
)
28
28
29
- const firecrackerRequestTimeout = 500 * time .Millisecond
29
+ const (
30
+ // env name to make firecracker request timeout configurable
31
+ firecrackerRequestTimeoutEnv = "FIRECRACKER_GO_SDK_REQUEST_TIMEOUT_MILLISECONDS"
32
+
33
+ defaultFirecrackerRequestTimeout = 500
34
+ )
30
35
31
36
// newFirecrackerClient creates a FirecrackerClient
32
37
func newFirecrackerClient (socketPath string , logger * logrus.Entry , debug bool ) * client.Firecracker {
@@ -51,13 +56,18 @@ func WithOpsClient(opsClient ops.ClientIface) ClientOpt {
51
56
52
57
// Client is a client for interacting with the Firecracker API
53
58
type Client struct {
54
- client * client.Firecracker
59
+ client * client.Firecracker
60
+ firecrackerRequestTimeout int
61
+ firecrackerInitTimeout int
55
62
}
56
63
57
64
// NewClient creates a Client
58
65
func NewClient (socketPath string , logger * logrus.Entry , debug bool , opts ... ClientOpt ) * Client {
59
66
httpClient := newFirecrackerClient (socketPath , logger , debug )
60
67
c := & Client {client : httpClient }
68
+ c .firecrackerRequestTimeout = envValueOrDefaultInt (firecrackerRequestTimeoutEnv , defaultFirecrackerRequestTimeout )
69
+ c .firecrackerInitTimeout = envValueOrDefaultInt (firecrackerInitTimeoutEnv , defaultFirecrackerInitTimeoutSeconds )
70
+
61
71
for _ , opt := range opts {
62
72
opt (c )
63
73
}
@@ -72,7 +82,7 @@ type PutLoggerOpt func(*ops.PutLoggerParams)
72
82
// PutLogger is a wrapper for the swagger generated client to make calling of
73
83
// the API easier.
74
84
func (f * Client ) PutLogger (ctx context.Context , logger * models.Logger , opts ... PutLoggerOpt ) (* ops.PutLoggerNoContent , error ) {
75
- timeout , cancel := context .WithTimeout (ctx , firecrackerRequestTimeout )
85
+ timeout , cancel := context .WithTimeout (ctx , time . Duration ( f . firecrackerRequestTimeout ) * time . Millisecond )
76
86
defer cancel ()
77
87
78
88
loggerParams := ops .NewPutLoggerParamsWithContext (timeout )
@@ -91,7 +101,7 @@ type PutMachineConfigurationOpt func(*ops.PutMachineConfigurationParams)
91
101
// PutMachineConfiguration is a wrapper for the swagger generated client to
92
102
// make calling of the API easier.
93
103
func (f * Client ) PutMachineConfiguration (ctx context.Context , cfg * models.MachineConfiguration , opts ... PutMachineConfigurationOpt ) (* ops.PutMachineConfigurationNoContent , error ) {
94
- timeout , cancel := context .WithTimeout (ctx , firecrackerRequestTimeout )
104
+ timeout , cancel := context .WithTimeout (ctx , time . Duration ( f . firecrackerRequestTimeout ) * time . Millisecond )
95
105
defer cancel ()
96
106
97
107
mc := ops .NewPutMachineConfigurationParamsWithContext (timeout )
@@ -110,7 +120,7 @@ type PutGuestBootSourceOpt func(*ops.PutGuestBootSourceParams)
110
120
// PutGuestBootSource is a wrapper for the swagger generated client to make
111
121
// calling of the API easier.
112
122
func (f * Client ) PutGuestBootSource (ctx context.Context , source * models.BootSource , opts ... PutGuestBootSourceOpt ) (* ops.PutGuestBootSourceNoContent , error ) {
113
- timeout , cancel := context .WithTimeout (ctx , firecrackerRequestTimeout )
123
+ timeout , cancel := context .WithTimeout (ctx , time . Duration ( f . firecrackerRequestTimeout ) * time . Millisecond )
114
124
defer cancel ()
115
125
116
126
bootSource := ops .NewPutGuestBootSourceParamsWithContext (timeout )
@@ -129,7 +139,7 @@ type PutGuestNetworkInterfaceByIDOpt func(*ops.PutGuestNetworkInterfaceByIDParam
129
139
// PutGuestNetworkInterfaceByID is a wrapper for the swagger generated client
130
140
// to make calling of the API easier.
131
141
func (f * Client ) PutGuestNetworkInterfaceByID (ctx context.Context , ifaceID string , ifaceCfg * models.NetworkInterface , opts ... PutGuestNetworkInterfaceByIDOpt ) (* ops.PutGuestNetworkInterfaceByIDNoContent , error ) {
132
- timeout , cancel := context .WithTimeout (ctx , firecrackerRequestTimeout )
142
+ timeout , cancel := context .WithTimeout (ctx , time . Duration ( f . firecrackerRequestTimeout ) * time . Millisecond )
133
143
defer cancel ()
134
144
135
145
cfg := ops .NewPutGuestNetworkInterfaceByIDParamsWithContext (timeout )
@@ -149,7 +159,7 @@ type PatchGuestNetworkInterfaceByIDOpt func(*ops.PatchGuestNetworkInterfaceByIDP
149
159
// PatchGuestNetworkInterfaceByID is a wrapper for the swagger generated client to make calling of the
150
160
// API easier.
151
161
func (f * Client ) PatchGuestNetworkInterfaceByID (ctx context.Context , ifaceID string , ifaceCfg * models.PartialNetworkInterface , opts ... PatchGuestNetworkInterfaceByIDOpt ) (* ops.PatchGuestNetworkInterfaceByIDNoContent , error ) {
152
- timeout , cancel := context .WithTimeout (ctx , firecrackerRequestTimeout )
162
+ timeout , cancel := context .WithTimeout (ctx , time . Duration ( f . firecrackerRequestTimeout ) * time . Millisecond )
153
163
defer cancel ()
154
164
155
165
cfg := ops .NewPatchGuestNetworkInterfaceByIDParamsWithContext (timeout )
@@ -170,7 +180,7 @@ type PutGuestDriveByIDOpt func(*ops.PutGuestDriveByIDParams)
170
180
// PutGuestDriveByID is a wrapper for the swagger generated client to make
171
181
// calling of the API easier.
172
182
func (f * Client ) PutGuestDriveByID (ctx context.Context , driveID string , drive * models.Drive , opts ... PutGuestDriveByIDOpt ) (* ops.PutGuestDriveByIDNoContent , error ) {
173
- timeout , cancel := context .WithTimeout (ctx , 250 * time .Millisecond )
183
+ timeout , cancel := context .WithTimeout (ctx , time . Duration ( f . firecrackerRequestTimeout ) / 2 * time .Millisecond )
174
184
defer cancel ()
175
185
176
186
params := ops .NewPutGuestDriveByIDParamsWithContext (timeout )
@@ -275,7 +285,7 @@ type GetMachineConfigurationOpt func(*ops.GetMachineConfigurationParams)
275
285
// calling of the API easier.
276
286
func (f * Client ) GetMachineConfiguration (opts ... GetMachineConfigurationOpt ) (* ops.GetMachineConfigurationOK , error ) {
277
287
p := ops .NewGetMachineConfigurationParams ()
278
- p .SetTimeout (firecrackerRequestTimeout )
288
+ p .SetTimeout (time . Duration ( f . firecrackerRequestTimeout ) * time . Millisecond )
279
289
for _ , opt := range opts {
280
290
opt (p )
281
291
}
0 commit comments