storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/cmd/http/stats/http-traffic-recorder.go (about) 1 /* 2 * MinIO Cloud Storage, (C) 2019-2020 MinIO, Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package stats 18 19 import ( 20 "io" 21 "net/http" 22 ) 23 24 // IncomingTrafficMeter counts the incoming bytes from the underlying request.Body. 25 type IncomingTrafficMeter struct { 26 io.ReadCloser 27 countBytes int 28 } 29 30 // Read calls the underlying Read and counts the transferred bytes. 31 func (r *IncomingTrafficMeter) Read(p []byte) (n int, err error) { 32 n, err = r.ReadCloser.Read(p) 33 r.countBytes += n 34 return n, err 35 } 36 37 // BytesCount returns the number of transferred bytes 38 func (r IncomingTrafficMeter) BytesCount() int { 39 return r.countBytes 40 } 41 42 // OutgoingTrafficMeter counts the outgoing bytes through the responseWriter. 43 type OutgoingTrafficMeter struct { 44 // wrapper for underlying http.ResponseWriter. 45 http.ResponseWriter 46 countBytes int 47 } 48 49 // Write calls the underlying write and counts the output bytes 50 func (w *OutgoingTrafficMeter) Write(p []byte) (n int, err error) { 51 n, err = w.ResponseWriter.Write(p) 52 w.countBytes += n 53 return n, err 54 } 55 56 // Flush calls the underlying Flush. 57 func (w *OutgoingTrafficMeter) Flush() { 58 w.ResponseWriter.(http.Flusher).Flush() 59 } 60 61 // BytesCount returns the number of transferred bytes 62 func (w OutgoingTrafficMeter) BytesCount() int { 63 return w.countBytes 64 }