github.com/bingoohuang/gg@v0.0.0-20240325092523-45da7dee9335/pkg/sqlparse/tidbparser/dependency/util/memory/action.go (about) 1 // Copyright 2018 PingCAP, Inc. 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 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package memory 15 16 import ( 17 "sync" 18 19 "github.com/bingoohuang/gg/pkg/sqlparse/tidbparser/dependency/mysql" 20 "github.com/bingoohuang/gg/pkg/sqlparse/tidbparser/dependency/terror" 21 ) 22 23 // ActionOnExceed is the action taken when memory usage exceeds memory quota. 24 // NOTE: All the implementors should be thread-safe. 25 type ActionOnExceed interface { 26 // Action will be called when memory usage exceeds memory quota by the 27 // corresponding Tracker. 28 Action(t *Tracker) 29 } 30 31 // LogOnExceed logs a warning only once when memory usage exceeds memory quota. 32 type LogOnExceed struct { 33 mutex sync.Mutex // For synchronization. 34 acted bool 35 } 36 37 // Action logs a warning only once when memory usage exceeds memory quota. 38 func (a *LogOnExceed) Action(t *Tracker) { 39 a.mutex.Lock() 40 defer a.mutex.Unlock() 41 if !a.acted { 42 a.acted = true 43 // log.Warnf(errMemExceedThreshold.GenByArgs(t.label, t.BytesConsumed(), t.bytesLimit, t.String()).Error()) 44 } 45 } 46 47 // PanicOnExceed panics when when memory usage exceeds memory quota. 48 type PanicOnExceed struct { 49 mutex sync.Mutex // For synchronization. 50 acted bool 51 } 52 53 // Action panics when when memory usage exceeds memory quota. 54 func (a *PanicOnExceed) Action(t *Tracker) { 55 a.mutex.Lock() 56 if a.acted { 57 a.mutex.Unlock() 58 return 59 } 60 a.acted = true 61 a.mutex.Unlock() 62 panic(PanicMemoryExceed + t.String()) 63 } 64 65 var errMemExceedThreshold = terror.ClassExecutor.New(codeMemExceedThreshold, mysql.MySQLErrName[mysql.ErrMemExceedThreshold]) 66 67 const ( 68 codeMemExceedThreshold terror.ErrCode = 8001 69 70 // PanicMemoryExceed represents the panic message when out of memory quota. 71 PanicMemoryExceed string = "Out Of Memory Quota!" 72 )