-
Notifications
You must be signed in to change notification settings - Fork 175
Expand file tree
/
Copy pathvolumes.go
More file actions
257 lines (227 loc) · 6.14 KB
/
Copy pathvolumes.go
File metadata and controls
257 lines (227 loc) · 6.14 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package boxlite
/*
#include "bridge.h"
#include <stdlib.h>
*/
import "C"
import (
"context"
"runtime/cgo"
"sync"
"unsafe"
)
// VolumeInfo holds metadata about a volume.
//
// CreatedAt is an RFC 3339 timestamp string (the C side formats the creation
// time as a string, mirroring the Node/Python SDKs). SizeBytes is nil when the
// payload size could not be computed.
type VolumeInfo struct {
Id string
// Name is mountable in place of Id. The server defaults it to Id.
Name string
CreatedAt string
SizeBytes *uint64
}
// Volumes is a runtime-scoped handle for named-volume operations.
type Volumes struct {
mu sync.RWMutex
runtime *Runtime
handle *C.CBoxliteVolumeHandle
}
func closedVolumesError() error {
return &Error{Code: ErrInvalidState, Message: "volume handle is closed"}
}
// Volumes returns a runtime-scoped handle for named-volume operations.
//
// The C-side volume handle is created synchronously; async operations
// (Create, List, Get, Remove) post events into the parent runtime's event
// queue and are dispatched by the runtime drain goroutine.
func (r *Runtime) Volumes() (*Volumes, error) {
var handle *C.CBoxliteVolumeHandle
var cerr C.CBoxliteError
code := C.boxlite_runtime_volumes(r.handle, &handle, &cerr)
if code != C.Ok {
return nil, freeError(&cerr)
}
return &Volumes{runtime: r, handle: handle}, nil
}
// Create creates a volume and returns its metadata.
//
// name may be mounted in place of the volume's id; pass "" to let the server
// name the volume after its id.
func (v *Volumes) Create(ctx context.Context, name string) (*VolumeInfo, error) {
if v == nil {
return nil, closedVolumesError()
}
v.mu.RLock()
if v.handle == nil {
v.mu.RUnlock()
return nil, closedVolumesError()
}
v.runtime.ensureDrainRunning()
ch := make(chan volumeResult, 1)
h := registerHandleForDispatch(cgo.NewHandle(ch))
var cName *C.char
if name != "" {
cName = toCString(name)
defer C.free(unsafe.Pointer(cName))
}
var cerr C.CBoxliteError
code := C.boxlite_volume_create(v.handle, cName, C.cbVolumeCreate(), handleToPtr(h), &cerr)
v.mu.RUnlock()
if code != C.Ok {
deleteHandleForDispatch(h)
return nil, freeError(&cerr)
}
select {
case res := <-ch:
return res.value, res.err
case <-ctx.Done():
drainAndDelete(ch, h, v.runtime.closing)
return nil, ctx.Err()
case <-v.runtime.closing:
drainAndDelete(ch, h, v.runtime.closing)
return nil, ErrRuntimeClosed
}
}
// List lists named volumes for this runtime.
func (v *Volumes) List(ctx context.Context) ([]VolumeInfo, error) {
if v == nil {
return nil, closedVolumesError()
}
v.mu.RLock()
if v.handle == nil {
v.mu.RUnlock()
return nil, closedVolumesError()
}
v.runtime.ensureDrainRunning()
ch := make(chan volumeListResult, 1)
h := registerHandleForDispatch(cgo.NewHandle(ch))
var cerr C.CBoxliteError
code := C.boxlite_volume_list(v.handle, C.cbVolumeList(), handleToPtr(h), &cerr)
v.mu.RUnlock()
if code != C.Ok {
deleteHandleForDispatch(h)
return nil, freeError(&cerr)
}
select {
case res := <-ch:
return res.value, res.err
case <-ctx.Done():
drainAndDelete(ch, h, v.runtime.closing)
return nil, ctx.Err()
case <-v.runtime.closing:
drainAndDelete(ch, h, v.runtime.closing)
return nil, ErrRuntimeClosed
}
}
// Get returns metadata for a single volume by id.
func (v *Volumes) Get(ctx context.Context, id string) (*VolumeInfo, error) {
if v == nil {
return nil, closedVolumesError()
}
v.mu.RLock()
if v.handle == nil {
v.mu.RUnlock()
return nil, closedVolumesError()
}
v.runtime.ensureDrainRunning()
cID := toCString(id)
defer C.free(unsafe.Pointer(cID))
ch := make(chan volumeResult, 1)
h := registerHandleForDispatch(cgo.NewHandle(ch))
var cerr C.CBoxliteError
code := C.boxlite_volume_get(v.handle, cID, C.cbVolumeGet(), handleToPtr(h), &cerr)
v.mu.RUnlock()
if code != C.Ok {
deleteHandleForDispatch(h)
return nil, freeError(&cerr)
}
select {
case res := <-ch:
return res.value, res.err
case <-ctx.Done():
drainAndDelete(ch, h, v.runtime.closing)
return nil, ctx.Err()
case <-v.runtime.closing:
drainAndDelete(ch, h, v.runtime.closing)
return nil, ErrRuntimeClosed
}
}
// Remove removes a volume by id. With force, a missing volume is a no-op.
func (v *Volumes) Remove(ctx context.Context, id string, force bool) error {
if v == nil {
return closedVolumesError()
}
v.mu.RLock()
if v.handle == nil {
v.mu.RUnlock()
return closedVolumesError()
}
v.runtime.ensureDrainRunning()
cID := toCString(id)
defer C.free(unsafe.Pointer(cID))
forceFlag := C.int(0)
if force {
forceFlag = 1
}
ch := make(chan error, 1)
h := registerHandleForDispatch(cgo.NewHandle(ch))
var cerr C.CBoxliteError
code := C.boxlite_volume_remove(v.handle, cID, forceFlag, C.cbVolumeRemove(), handleToPtr(h), &cerr)
v.mu.RUnlock()
if code != C.Ok {
deleteHandleForDispatch(h)
return freeError(&cerr)
}
select {
case err := <-ch:
return err
case <-ctx.Done():
abandonAsyncErr(ch, h, v.runtime.closing)
return ctx.Err()
case <-v.runtime.closing:
abandonAsyncErr(ch, h, v.runtime.closing)
return ErrRuntimeClosed
}
}
// Close releases the volume handle.
func (v *Volumes) Close() error {
if v != nil {
v.mu.Lock()
defer v.mu.Unlock()
if v.handle != nil {
C.boxlite_volume_free(v.handle)
v.handle = nil
}
}
return nil
}
// cVolumeInfoToGo materialises a single CVolumeInfo into a Go VolumeInfo. It
// does not free the C struct; the caller owns that.
func cVolumeInfoToGo(info *C.CVolumeInfo) VolumeInfo {
var size *uint64
if info.has_size != 0 {
v := uint64(info.size_bytes)
size = &v
}
return VolumeInfo{
Id: cString(info.id),
Name: cString(info.name),
CreatedAt: cString(info.created_at),
SizeBytes: size,
}
}
// convertVolumeInfoList materialises a CVolumeInfoList* into a Go VolumeInfo
// slice. The caller is responsible for freeing the C list afterwards.
func convertVolumeInfoList(list *C.CVolumeInfoList) []VolumeInfo {
if list == nil || list.count == 0 || list.items == nil {
return nil
}
items := unsafe.Slice(list.items, int(list.count))
volumes := make([]VolumeInfo, len(items))
for idx := range items {
volumes[idx] = cVolumeInfoToGo(&items[idx])
}
return volumes
}