2018-05-09 18:59:45 -04:00
|
|
|
/*
|
2019-04-09 14:39:42 -04:00
|
|
|
* MinIO Cloud Storage, (C) 2018 MinIO, Inc.
|
2018-05-09 18:59:45 -04:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package logger
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-10-30 17:55:50 -04:00
|
|
|
"errors"
|
|
|
|
"net/http"
|
2018-05-09 18:59:45 -04:00
|
|
|
"sync"
|
|
|
|
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Holds a map of recently logged errors.
|
|
|
|
type logOnceType struct {
|
|
|
|
IDMap map[interface{}]error
|
|
|
|
sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
// One log message per error.
|
2019-10-11 21:50:54 -04:00
|
|
|
func (l *logOnceType) logOnceIf(ctx context.Context, err error, id interface{}, errKind ...interface{}) {
|
2018-05-09 18:59:45 -04:00
|
|
|
if err == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
l.Lock()
|
|
|
|
shouldLog := false
|
|
|
|
prevErr := l.IDMap[id]
|
|
|
|
if prevErr == nil {
|
|
|
|
l.IDMap[id] = err
|
|
|
|
shouldLog = true
|
|
|
|
} else {
|
|
|
|
if prevErr.Error() != err.Error() {
|
|
|
|
l.IDMap[id] = err
|
|
|
|
shouldLog = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
l.Unlock()
|
|
|
|
|
|
|
|
if shouldLog {
|
2019-10-11 21:50:54 -04:00
|
|
|
LogIf(ctx, err, errKind...)
|
2018-05-09 18:59:45 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cleanup the map every 30 minutes so that the log message is printed again for the user to notice.
|
|
|
|
func (l *logOnceType) cleanupRoutine() {
|
|
|
|
for {
|
2019-02-13 07:59:36 -05:00
|
|
|
l.Lock()
|
|
|
|
l.IDMap = make(map[interface{}]error)
|
|
|
|
l.Unlock()
|
|
|
|
|
|
|
|
time.Sleep(30 * time.Minute)
|
2018-05-09 18:59:45 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns logOnceType
|
|
|
|
func newLogOnceType() *logOnceType {
|
|
|
|
l := &logOnceType{IDMap: make(map[interface{}]error)}
|
|
|
|
go l.cleanupRoutine()
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
|
|
|
var logOnce = newLogOnceType()
|
|
|
|
|
|
|
|
// LogOnceIf - Logs notification errors - once per error.
|
|
|
|
// id is a unique identifier for related log messages, refer to cmd/notification.go
|
|
|
|
// on how it is used.
|
2019-10-11 21:50:54 -04:00
|
|
|
func LogOnceIf(ctx context.Context, err error, id interface{}, errKind ...interface{}) {
|
2020-10-30 17:55:50 -04:00
|
|
|
if err == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if errors.Is(err, context.Canceled) || errors.Is(err, http.ErrServerClosed) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if e := errors.Unwrap(err); e != nil {
|
|
|
|
if e.Error() == "disk not found" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-11 21:50:54 -04:00
|
|
|
logOnce.logOnceIf(ctx, err, id, errKind...)
|
2018-05-09 18:59:45 -04:00
|
|
|
}
|