-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Feat: perf Signed-off-by: Yin Da <yd219913@alibaba-inc.com> * Feat: add test Signed-off-by: Yin Da <yd219913@alibaba-inc.com> * Feat: add error handler Signed-off-by: Yin Da <yd219913@alibaba-inc.com> --------- Signed-off-by: Yin Da <yd219913@alibaba-inc.com> Co-authored-by: Tianxin Dong <dongtianxin.tx@alibaba-inc.com>
- Loading branch information
Showing
5 changed files
with
208 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
Copyright 2023 The KubeVela Authors. | ||
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 errhandler | ||
|
||
// ErrorHandler handler for error | ||
type ErrorHandler func(error) | ||
|
||
// NotifyOrPanic if given errChan is nil, panic on error, otherwise send error | ||
// to errChan | ||
func NotifyOrPanic(errChan chan error) ErrorHandler { | ||
return func(err error) { | ||
if err == nil { | ||
return | ||
} | ||
if errChan != nil { | ||
errChan <- err | ||
} else { | ||
panic(err) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
Copyright 2023 The KubeVela Authors. | ||
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 errhandler_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/kubevela/pkg/util/errhandler" | ||
) | ||
|
||
func TestNotifyOrPanic(t *testing.T) { | ||
ch := make(chan error, 1) | ||
h1 := errhandler.NotifyOrPanic(ch) | ||
h2 := errhandler.NotifyOrPanic(nil) | ||
h1(nil) | ||
require.Equal(t, 0, len(ch)) | ||
e := fmt.Errorf("err") | ||
h1(e) | ||
require.Equal(t, 1, len(ch)) | ||
func() { | ||
defer func() { | ||
err := recover() | ||
require.NotNil(t, err) | ||
}() | ||
h2(e) | ||
}() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
Copyright 2023 The KubeVela Authors. | ||
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 profiling | ||
|
||
import "github.com/spf13/pflag" | ||
|
||
var ( | ||
// Addr the address for starting profiling server | ||
Addr = "" | ||
) | ||
|
||
// AddFlags . | ||
func AddFlags(fs *pflag.FlagSet) { | ||
fs.StringVarP(&Addr, "profiling-addr", "", Addr, "if not empty, start the profiling server at the given address") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
Copyright 2023 The KubeVela Authors. | ||
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 profiling | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
"net/http/pprof" | ||
"runtime" | ||
|
||
"k8s.io/klog/v2" | ||
|
||
"github.com/kubevela/pkg/util/errhandler" | ||
) | ||
|
||
// NewProfilingHandler create a profiling handler | ||
func NewProfilingHandler() http.Handler { | ||
mux := http.NewServeMux() | ||
mux.HandleFunc("/debug/pprof/", pprof.Index) | ||
mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline) | ||
mux.HandleFunc("/debug/pprof/profile", pprof.Profile) | ||
mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol) | ||
mux.HandleFunc("/debug/pprof/trace", pprof.Trace) | ||
mux.HandleFunc("/mem/stat", func(writer http.ResponseWriter, request *http.Request) { | ||
var ms runtime.MemStats | ||
runtime.ReadMemStats(&ms) | ||
bs, _ := json.Marshal(ms) | ||
_, _ = writer.Write(bs) | ||
}) | ||
mux.HandleFunc("/gc", func(writer http.ResponseWriter, request *http.Request) { | ||
runtime.GC() | ||
}) | ||
return mux | ||
} | ||
|
||
// StartProfilingServer listen to the pprofAddr and export the profiling results | ||
// If the errChan is nil, this function will panic when the listening error occurred. | ||
func StartProfilingServer(errChan chan error) { | ||
if Addr == "" { | ||
return | ||
} | ||
klog.Infof("start profiling server at %s", Addr) | ||
err := http.ListenAndServe(Addr, NewProfilingHandler()) | ||
errhandler.NotifyOrPanic(errChan)(err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
Copyright 2023 The KubeVela Authors. | ||
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 profiling_test | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/spf13/pflag" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/kubevela/pkg/util/profiling" | ||
) | ||
|
||
func TestStartProfilingServer(t *testing.T) { | ||
fn := profiling.NewProfilingHandler() | ||
server := httptest.NewServer(fn) | ||
defer server.Close() | ||
_, err := http.Get(server.URL + "/mem/stat") | ||
require.NoError(t, err) | ||
_, err = http.Get(server.URL + "/gc") | ||
require.NoError(t, err) | ||
|
||
profiling.AddFlags(pflag.NewFlagSet("", pflag.ExitOnError)) | ||
profiling.StartProfilingServer(nil) | ||
} |