cubefs

Форк
0
/
recovery.go 
77 строк · 1.9 Кб
1
// Copyright 2022 The CubeFS Authors.
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
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12
// implied. See the License for the specific language governing
13
// permissions and limitations under the License.
14

15
package rpc
16

17
import (
18
	"bytes"
19
	"fmt"
20
	"net"
21
	"net/http"
22
	"os"
23
	"runtime"
24
	"strings"
25

26
	"github.com/cubefs/cubefs/blobstore/util/log"
27
)
28

29
// defaultRecovery logging panic info, then panic to next handler
30
func defaultRecovery(w http.ResponseWriter, req *http.Request, err interface{}) {
31
	var brokenPipe bool
32
	if ne, ok := err.(*net.OpError); ok {
33
		if se, ok := ne.Err.(*os.SyscallError); ok {
34
			if strings.Contains(strings.ToLower(se.Error()), "broken pipe") ||
35
				strings.Contains(strings.ToLower(se.Error()), "connection reset by peer") {
36
				brokenPipe = true
37
			}
38
		}
39
	}
40

41
	stack := stack(3)
42
	if brokenPipe {
43
		log.Warnf("handle panic: %s on broken pipe\n%s", err, stack)
44
	} else {
45
		log.Errorf("handle panic: %s\n%s", err, stack)
46
		panic(err)
47
	}
48
}
49

50
func stack(skip int) []byte {
51
	buf := new(bytes.Buffer)
52
	for i := skip; ; i++ {
53
		pc, file, line, ok := runtime.Caller(i)
54
		if !ok {
55
			break
56
		}
57
		fmt.Fprintf(buf, "%s:%d (0x%x:%s)\n", file, line, pc, funcname(pc))
58
	}
59
	return buf.Bytes()
60
}
61

62
// funcname returns the name of the function
63
func funcname(pc uintptr) []byte {
64
	fn := runtime.FuncForPC(pc)
65
	if fn == nil {
66
		return []byte("???")
67
	}
68
	name := []byte(fn.Name())
69

70
	if last := bytes.LastIndex(name, []byte("/")); last >= 0 {
71
		name = name[last+1:]
72
	}
73
	if first := bytes.Index(name, []byte(".")); first >= 0 {
74
		name = name[first+1:]
75
	}
76
	return name
77
}
78

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.