MCPcopy Index your code
hub / github.com/GoEdgeLab/EdgeNode / openWriter

Method openWriter

internal/caches/storage_memory.go:187–249  ·  view source on GitHub ↗
(key string, expiresAt int64, status int, headerSize int, bodySize int64, maxSize int64, isDirty bool)

Source from the content-addressed store, hash-verified

185}
186
187func (this *MemoryStorage) openWriter(key string, expiresAt int64, status int, headerSize int, bodySize int64, maxSize int64, isDirty bool) (Writer, error) {
188 // 待写入队列是否已满
189 if isDirty &&
190 this.parentStorage != nil &&
191 this.dirtyQueueSize > 0 &&
192 len(this.dirtyChan) >= this.dirtyQueueSize-64 /** delta **/ { // 缓存时间过长
193 return nil, ErrWritingQueueFull
194 }
195
196 this.locker.Lock()
197 defer this.locker.Unlock()
198
199 // 是否正在写入
200 var isWriting = false
201 _, ok := this.writingKeyMap[key]
202 if ok {
203 return nil, fmt.Errorf("%w (005)", ErrFileIsWriting)
204 }
205 this.writingKeyMap[key] = zero.New()
206 defer func() {
207 if !isWriting {
208 delete(this.writingKeyMap, key)
209 }
210 }()
211
212 // 检查是否过期
213 var hash = this.hash(key)
214 item, ok := this.valuesMap[hash]
215 if ok && !item.IsExpired() {
216 var hashString = types.String(hash)
217 exists, _, _ := this.list.Exist(hashString)
218 if !exists {
219 // remove from values map
220 delete(this.valuesMap, hash)
221 _ = this.list.Remove(hashString)
222 item = nil
223 } else {
224 return nil, fmt.Errorf("%w (006)", ErrFileIsWriting)
225 }
226 }
227
228 // 检查是否超出容量最大值
229 var capacityBytes = this.memoryCapacityBytes()
230 if bodySize < 0 {
231 bodySize = 0
232 }
233 if capacityBytes > 0 && capacityBytes <= atomic.LoadInt64(&this.usedSize)+bodySize {
234 return nil, NewCapacityError("write memory cache failed: over memory size: " + strconv.FormatInt(capacityBytes, 10) + ", current size: " + strconv.FormatInt(this.usedSize, 10) + " bytes")
235 }
236
237 // 先删除
238 err := this.deleteWithoutLocker(key)
239 if err != nil {
240 return nil, err
241 }
242
243 isWriting = true
244 return NewMemoryWriter(this, key, expiresAt, status, isDirty, bodySize, maxSize, func(valueItem *MemoryItem) {

Callers 2

OpenWriterMethod · 0.95
OpenFlushWriterMethod · 0.95

Calls 13

hashMethod · 0.95
memoryCapacityBytesMethod · 0.95
deleteWithoutLockerMethod · 0.95
NewFunction · 0.92
NewCapacityErrorFunction · 0.85
NewMemoryWriterFunction · 0.85
ErrorfMethod · 0.80
ExistMethod · 0.65
RemoveMethod · 0.65
LockMethod · 0.45
UnlockMethod · 0.45
IsExpiredMethod · 0.45

Tested by

no test coverage detected