cubefs

Форк
0
129 строк · 2.6 Кб
1
// Copyright 2022 The CubeFS Authors.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12
// implied. See the License for the specific language governing
13
// permissions and limitations under the License.
14

15
package example
16

17
import (
18
	"io"
19
	"net/http"
20
	"time"
21

22
	"github.com/cubefs/cubefs/blobstore/common/rpc"
23
	"github.com/cubefs/cubefs/blobstore/common/trace"
24
)
25

26
// Filer file interface
27
type Filer interface {
28
	Write(*rpc.Context)
29
	Read(*rpc.Context)
30
}
31

32
type file struct{}
33

34
// NewFile new file
35
func NewFile() Filer {
36
	return &file{}
37
}
38

39
type noopReader struct {
40
	size int
41
	io.Reader
42
}
43

44
func (r *noopReader) Read(p []byte) (int, error) {
45
	if len(p) >= r.size {
46
		size := r.size
47
		r.size = 0
48
		return size, io.EOF
49
	}
50
	r.size -= len(p)
51
	return len(p), nil
52
}
53

54
type noopWriter struct {
55
	size int
56
	io.Writer
57
}
58

59
func (w *noopWriter) Write(p []byte) (int, error) {
60
	if len(p) >= w.size {
61
		size := w.size
62
		w.size = 0
63
		return size, io.EOF
64
	}
65
	w.size -= len(p)
66
	return len(p), nil
67
}
68

69
// ArgsWrite args upload
70
type ArgsWrite struct {
71
	Size int `flag:"size"`
72
}
73

74
func (f *file) Write(c *rpc.Context) {
75
	span := trace.SpanFromContextSafe(c.Request.Context())
76

77
	args := new(ArgsWrite)
78
	if err := c.ParseArgs(args); err != nil {
79
		c.RespondError(err)
80
		return
81
	}
82
	span.Debugf("receive file write request, args: %#v", args)
83

84
	if args.Size <= 0 {
85
		c.RespondError(errBadRequst)
86
		return
87
	}
88

89
	// do some trace tags
90
	span.SetTag("limit-key", "limit-write")
91

92
	start := time.Now()
93
	w := &noopWriter{size: args.Size}
94
	if _, err := io.CopyN(w, c.Request.Body, int64(args.Size)); err != nil {
95
		span.AppendTrackLog("copy", start, err)
96
		c.RespondError(rpc.NewError(http.StatusGone, "ReadBody", err))
97
		return
98
	}
99
	span.AppendTrackLog("copy", start, nil)
100

101
	c.Respond()
102
}
103

104
// ArgsRead args read
105
type ArgsRead struct {
106
	Size int
107
}
108

109
func (*file) Read(c *rpc.Context) {
110
	span := trace.SpanFromContextSafe(c.Request.Context())
111

112
	args := new(ArgsRead)
113
	if err := c.ParseArgs(args); err != nil {
114
		c.RespondError(err)
115
		return
116
	}
117
	span.Debugf("receive file read request, args: %#v", args)
118

119
	if args.Size < 0 {
120
		c.RespondError(errBadRequst)
121
		return
122
	}
123

124
	// do something
125
	span.SetTag("test-for-nil", nil)
126

127
	c.RespondWithReader(http.StatusOK, args.Size, rpc.MIMEStream,
128
		&noopReader{size: args.Size}, nil)
129
}
130

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.