-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdefault.go
More file actions
executable file
·179 lines (164 loc) · 5.07 KB
/
default.go
File metadata and controls
executable file
·179 lines (164 loc) · 5.07 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
package replify
import (
"context"
"io"
"net/http"
"sync"
"time"
)
// New creates a new instance of the `wrapper` struct.
//
// This function initializes a `wrapper` struct with its default values,
// including an empty map for the `Debug` field.
//
// Returns:
// - A pointer to a newly created `wrapper` instance with initialized fields.
func New() *wrapper {
w := &wrapper{
meta: defaultMetaValues(),
header: Processing,
}
return w
}
// Pages creates a new instance of the `pagination` struct.
//
// This function initializes a `pagination` struct with its default values.
//
// Returns:
// - A pointer to a newly created `pagination` instance.
func Pages() *pagination {
p := &pagination{}
return p
}
// FromPages creates a new instance of the `pagination` struct
// with specified total items and items per page.
//
// This function initializes a `pagination` struct and sets
// the total number of items and items per page using the provided parameters.
//
// Parameters:
// - totalItems: The total number of items to be paginated.
// - perPage: The number of items to be displayed per page.
//
// Returns:
// - A pointer to a newly created `pagination` instance with the specified settings.
func FromPages(totalItems int, perPage int) *pagination {
return Pages().WithPerPage(perPage).WithTotalItems(totalItems)
}
// Meta creates a new instance of the `meta` struct.
//
// This function initializes a `meta` struct with its default values,
// including an empty `CustomFields` map.
//
// Returns:
// - A pointer to a newly created `meta` instance with initialized fields.
func Meta() *meta {
m := &meta{
customFields: map[string]any{},
}
return m
}
// Header creates a new instance of the `header` struct.
//
// This function initializes a `header` struct with its default values.
//
// Returns:
// - A pointer to a newly created `header` instance.
func Header() *header {
h := &header{}
return h
}
// NewBufferPool creates new buffer pool
//
// This function initializes a `BufferPool` struct with a specified buffer size
// and pool size.
//
// Parameters:
// - bufferSize: The size of each buffer in bytes.
// - poolSize: The maximum number of buffers to maintain in the pool.
//
// Returns:
//
// - A pointer to a newly created `BufferPool` instance with the specified settings.
func NewBufferPool(bufferSize int64, poolSize int) *BufferPool {
return &BufferPool{
buffers: make(chan []byte, poolSize),
size: bufferSize,
}
}
// NewStreamConfig creates default streaming configuration
//
// This function initializes a `StreamConfig` struct with default values
// suitable for typical streaming scenarios.
//
// Returns:
// - A pointer to a newly created `StreamConfig` instance with default settings.
func NewStreamConfig() *StreamConfig {
return &StreamConfig{
ChunkSize: 65536, // 64KB default
Strategy: StrategyBuffered,
Compression: CompressNone,
UseBufferPool: true,
MaxConcurrentChunks: 4,
ReadTimeout: 30 * time.Second,
WriteTimeout: 30 * time.Second,
ThrottleRate: 0, // unlimited
}
}
// NewStreaming creates a new instance of the `StreamingWrapper` struct.
//
// This function initializes a `StreamingWrapper` struct with the provided
// `reader`, and `config`. If the `config` is nil, it uses default
// streaming configuration.
//
// Parameters:
// - `reader`: An `io.Reader` instance from which data will be streamed.
// - `config`: A pointer to a `StreamConfig` struct containing streaming configuration.
//
// Returns:
// - A pointer to a newly created `StreamingWrapper` instance with initialized fields.
func NewStreaming(reader io.Reader, config *StreamConfig) *StreamingWrapper {
if config == nil {
config = NewStreamConfig()
}
ctx, cancel := context.WithCancel(context.Background())
sw := &StreamingWrapper{
wrapper: New(),
config: config,
reader: reader,
ctx: ctx,
cancel: cancel,
progress: &StreamProgress{},
stats: &StreamingStats{StartTime: time.Now()},
errors: make([]error, 0),
isStreaming: false,
compressionBuf: make([]byte, 0),
mu: sync.RWMutex{},
}
// Initialize wrapper defaults
sw.wrapper.WithStatusCode(http.StatusOK)
sw.wrapper.WithMessage("Streaming initialized")
sw.wrapper.WithDebuggingKV("streaming", true)
sw.wrapper.WithDebuggingKV("strategy", string(config.Strategy))
sw.wrapper.WithDebuggingKV("compression", string(config.Compression))
// Initialize buffer pool if enabled
if config.UseBufferPool {
sw.bufferPool = NewBufferPool(config.ChunkSize, 4)
}
return sw
}
// defaultMetaValues returns the default values for the `meta` struct.
//
// This function creates a new `meta` instance with the default values,
// including the locale, API version, requested time, and request ID.
//
// Returns:
// - A pointer to a newly created `meta` instance with the default values.
func defaultMetaValues() *meta {
return Meta().
WithLocale("en_US"). // vi_VN, en_US
WithApiVersion("v0.0.1").
WithRequestedTime(time.Now()).
WithRequestID(cryptoID()).
WithDeltaValue(0)
}