| // Licensed to the Apache Software Foundation (ASF) under one or more |
| // contributor license agreements. See the NOTICE file distributed with |
| // this work for additional information regarding copyright ownership. |
| // The ASF licenses this file to You 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 util |
| |
| import ( |
| "runtime" |
| "sync" |
| |
| "github.com/apache/incubator-eventmesh/eventmesh-server-go/log" |
| ) |
| |
| // PanicBufLen is len of buffer used for stack trace logging |
| // when the goroutine panics, 1024 by default. |
| var PanicBufLen = 1024 |
| |
| // GoAndWait provides safe concurrent handling. Per input handler, it starts a goroutine. |
| // Then it waits until all handlers are done and will recover if any handler panics. |
| // The returned error is the first non-nil error returned by one of the handlers. |
| // It can be set that non-nil error will be returned if the "key" handler fails while other handlers always |
| // return nil error. |
| func GoAndWait(handlers ...func() error) error { |
| var ( |
| wg sync.WaitGroup |
| once sync.Once |
| err error |
| ) |
| for _, f := range handlers { |
| wg.Add(1) |
| go func(handler func() error) { |
| defer func() { |
| if e := recover(); e != nil { |
| buf := make([]byte, PanicBufLen) |
| buf = buf[:runtime.Stack(buf, false)] |
| log.Errorf("[PANIC]%v\n%s\n", e, buf) |
| } |
| wg.Done() |
| }() |
| if e := handler(); e != nil { |
| once.Do(func() { |
| err = e |
| }) |
| } |
| }(f) |
| } |
| wg.Wait() |
| return err |
| } |