-
Notifications
You must be signed in to change notification settings - Fork 99
/
Copy pathframer_builder.go
77 lines (65 loc) · 1.97 KB
/
framer_builder.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
//
//
// Tencent is pleased to support the open source community by making tRPC available.
//
// Copyright (C) 2023 THL A29 Limited, a Tencent company.
// All rights reserved.
//
// If you have downloaded a copy of the tRPC source code from Tencent,
// please note that tRPC source code is licensed under the Apache 2.0 License,
// A copy of the Apache 2.0 License is included in this file.
//
//
package codec
import (
"bufio"
"io"
)
// DefaultReaderSize is the default size of reader in bit.
const DefaultReaderSize = 4 * 1024
// readerSizeConfig is the default size of buffer when framer read package.
var readerSizeConfig = DefaultReaderSize
// NewReaderSize returns a reader with read buffer. Size <= 0 means no buffer.
func NewReaderSize(r io.Reader, size int) io.Reader {
if size <= 0 {
return r
}
return bufio.NewReaderSize(r, size)
}
// NewReader returns reader with the default buffer size.
func NewReader(r io.Reader) io.Reader {
return bufio.NewReaderSize(r, readerSizeConfig)
}
// GetReaderSize returns size of read buffer in bit.
func GetReaderSize() int {
return readerSizeConfig
}
// SetReaderSize sets the size of read buffer in bit.
func SetReaderSize(size int) {
readerSizeConfig = size
}
// FramerBuilder defines how to build a framer. In general, each connection
// build a framer.
type FramerBuilder interface {
New(io.Reader) Framer
}
// Framer defines how to read a data frame.
type Framer interface {
ReadFrame() ([]byte, error)
}
// SafeFramer is a special framer, provides an isSafe() method
// to describe if it is safe when concurrent read.
type SafeFramer interface {
Framer
// IsSafe returns if this framer is safe when concurrent read.
IsSafe() bool
}
// IsSafeFramer returns if this framer is safe when concurrent read. The input
// parameter f should implement SafeFramer interface. If not , this method will return false.
func IsSafeFramer(f interface{}) bool {
framer, ok := f.(SafeFramer)
if ok && framer.IsSafe() {
return true
}
return false
}