forked from tedcy/fdfs_client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
293 lines (253 loc) · 6.84 KB
/
client.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
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package fdfs_client
import (
"fmt"
"net"
"sync"
)
type Client struct {
trackerPools map[string]*connPool
storagePools map[string]*connPool
storagePoolLock *sync.RWMutex
config *config
}
func NewClient(conf *config) (*Client, error) {
client := &Client{
config: conf,
storagePoolLock: &sync.RWMutex{},
}
client.trackerPools = make(map[string]*connPool)
client.storagePools = make(map[string]*connPool)
for _, addr := range conf.trackerAddr {
trackerPool, err := newConnPool(addr, conf.maxConns, conf.ConnectTimeOut)
if err != nil {
return nil, err
}
client.trackerPools[addr] = trackerPool
}
return client, nil
}
func NewClientWithConfig(configName string) (*Client, error) {
config, err := newConfig(configName)
if err != nil {
return nil, err
}
config.maxConns = 10
config.ConnectTimeOut = 5
client := &Client{
config: config,
storagePoolLock: &sync.RWMutex{},
}
client.trackerPools = make(map[string]*connPool)
client.storagePools = make(map[string]*connPool)
for _, addr := range config.trackerAddr {
trackerPool, err := newConnPool(addr, config.maxConns, config.ConnectTimeOut)
if err != nil {
return nil, err
}
client.trackerPools[addr] = trackerPool
}
return client, nil
}
func (this *Client) Destory() {
if this == nil {
return
}
for _, pool := range this.trackerPools {
pool.Destory()
}
for _, pool := range this.storagePools {
pool.Destory()
}
}
func (this *Client) UploadByFilename(fileName string) (string, error) {
fileInfo, err := newFileInfo(fileName, nil, "")
defer fileInfo.Close()
if err != nil {
return "", err
}
storageInfo, err := this.queryStorageInfoWithTracker(TRACKER_PROTO_CMD_SERVICE_QUERY_STORE_WITHOUT_GROUP_ONE, "", "")
if err != nil {
return "", err
}
task := &storageUploadTask{}
//req
task.fileInfo = fileInfo
task.storagePathIndex = storageInfo.storagePathIndex
if err := this.doStorage(task, storageInfo); err != nil {
return "", err
}
return task.fileId, nil
}
func (this *Client) UploadByBuffer(buffer []byte, fileExtName string) (string, error) {
fileInfo, err := newFileInfo("", buffer, fileExtName)
defer fileInfo.Close()
if err != nil {
return "", err
}
storageInfo, err := this.queryStorageInfoWithTracker(TRACKER_PROTO_CMD_SERVICE_QUERY_STORE_WITHOUT_GROUP_ONE, "", "")
if err != nil {
return "", err
}
task := &storageUploadTask{}
//req
task.fileInfo = fileInfo
task.storagePathIndex = storageInfo.storagePathIndex
if err := this.doStorage(task, storageInfo); err != nil {
return "", err
}
return task.fileId, nil
}
func (this *Client) DownloadToFile(fileId string, localFilename string, offset int64, downloadBytes int64) error {
groupName, remoteFilename, err := splitFileId(fileId)
if err != nil {
return err
}
storageInfo, err := this.queryStorageInfoWithTracker(TRACKER_PROTO_CMD_SERVICE_QUERY_FETCH_ONE, groupName, remoteFilename)
if err != nil {
return err
}
task := &storageDownloadTask{}
//req
task.groupName = groupName
task.remoteFilename = remoteFilename
task.offset = offset
task.downloadBytes = downloadBytes
//res
task.localFilename = localFilename
return this.doStorage(task, storageInfo)
}
//deprecated
func (this *Client) DownloadToBuffer(fileId string, offset int64, downloadBytes int64) ([]byte, error) {
groupName, remoteFilename, err := splitFileId(fileId)
if err != nil {
return nil, err
}
storageInfo, err := this.queryStorageInfoWithTracker(TRACKER_PROTO_CMD_SERVICE_QUERY_FETCH_ONE, groupName, remoteFilename)
if err != nil {
return nil, err
}
task := &storageDownloadTask{}
//req
task.groupName = groupName
task.remoteFilename = remoteFilename
task.offset = offset
task.downloadBytes = downloadBytes
//res
if err := this.doStorage(task, storageInfo); err != nil {
return nil, err
}
return task.buffer, nil
}
func (this *Client) DownloadToAllocatedBuffer(fileId string, buffer []byte,offset int64, downloadBytes int64) (error) {
groupName, remoteFilename, err := splitFileId(fileId)
if err != nil {
return err
}
storageInfo, err := this.queryStorageInfoWithTracker(TRACKER_PROTO_CMD_SERVICE_QUERY_FETCH_ONE, groupName, remoteFilename)
if err != nil {
return err
}
task := &storageDownloadTask{}
//req
task.groupName = groupName
task.remoteFilename = remoteFilename
task.offset = offset
task.downloadBytes = downloadBytes
task.buffer = buffer //allocate buffer by user
//res
if err := this.doStorage(task, storageInfo); err != nil {
return err
}
return nil
}
func (this *Client) DeleteFile(fileId string) error {
groupName, remoteFilename, err := splitFileId(fileId)
if err != nil {
return err
}
storageInfo, err := this.queryStorageInfoWithTracker(TRACKER_PROTO_CMD_SERVICE_QUERY_FETCH_ONE, groupName, remoteFilename)
if err != nil {
return err
}
task := &storageDeleteTask{}
//req
task.groupName = groupName
task.remoteFilename = remoteFilename
return this.doStorage(task, storageInfo)
}
func (this *Client) doTracker(task task) error {
trackerConn, err := this.getTrackerConn()
if err != nil {
return err
}
defer trackerConn.Close()
if err := task.SendReq(trackerConn); err != nil {
return err
}
if err := task.RecvRes(trackerConn); err != nil {
return err
}
return nil
}
func (this *Client) doStorage(task task, storageInfo *storageInfo) error {
storageConn, err := this.getStorageConn(storageInfo)
if err != nil {
return err
}
defer storageConn.Close()
if err := task.SendReq(storageConn); err != nil {
return err
}
if err := task.RecvRes(storageConn); err != nil {
return err
}
return nil
}
func (this *Client) queryStorageInfoWithTracker(cmd int8, groupName string, remoteFilename string) (*storageInfo, error) {
task := &trackerTask{}
task.cmd = cmd
task.groupName = groupName
task.remoteFilename = remoteFilename
if err := this.doTracker(task); err != nil {
return nil, err
}
return &storageInfo{
addr: fmt.Sprintf("%s:%d", task.ipAddr, task.port),
storagePathIndex: task.storePathIndex,
}, nil
}
func (this *Client) getTrackerConn() (net.Conn, error) {
var trackerConn net.Conn
var err error
var getOne bool
for _, trackerPool := range this.trackerPools {
trackerConn, err = trackerPool.get()
if err == nil {
getOne = true
break
}
}
if getOne {
return trackerConn, nil
}
if err == nil {
return nil, fmt.Errorf("no connPool can be use")
}
return nil, err
}
func (this *Client) getStorageConn(storageInfo *storageInfo) (net.Conn, error) {
this.storagePoolLock.Lock()
storagePool, ok := this.storagePools[storageInfo.addr]
if ok {
this.storagePoolLock.Unlock()
return storagePool.get()
}
storagePool, err := newConnPool(storageInfo.addr, this.config.maxConns, this.config.ConnectTimeOut)
if err != nil {
this.storagePoolLock.Unlock()
return nil, err
}
this.storagePools[storageInfo.addr] = storagePool
this.storagePoolLock.Unlock()
return storagePool.get()
}