2014-06-06 23:40:48 +00:00
|
|
|
/*
|
|
|
|
Copyright 2014 Google Inc. All rights reserved.
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
2014-06-12 00:01:02 +00:00
|
|
|
|
2014-06-06 23:40:48 +00:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2014-06-14 01:11:32 +00:00
|
|
|
"fmt"
|
2014-09-04 17:55:30 +00:00
|
|
|
"regexp"
|
2014-06-14 01:11:32 +00:00
|
|
|
"runtime"
|
2014-06-06 23:40:48 +00:00
|
|
|
"time"
|
2014-06-25 03:51:57 +00:00
|
|
|
|
|
|
|
"github.com/golang/glog"
|
2014-06-06 23:40:48 +00:00
|
|
|
)
|
|
|
|
|
2014-06-29 00:52:08 +00:00
|
|
|
// For testing, bypass HandleCrash.
|
|
|
|
var ReallyCrash bool
|
|
|
|
|
2014-07-11 00:32:42 +00:00
|
|
|
// HandleCrash simply catches a crash and logs an error. Meant to be called via defer.
|
2014-06-06 23:40:48 +00:00
|
|
|
func HandleCrash() {
|
2014-06-29 00:52:08 +00:00
|
|
|
if ReallyCrash {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-06-06 23:40:48 +00:00
|
|
|
r := recover()
|
|
|
|
if r != nil {
|
2014-06-14 01:11:32 +00:00
|
|
|
callers := ""
|
|
|
|
for i := 0; true; i++ {
|
|
|
|
_, file, line, ok := runtime.Caller(i)
|
|
|
|
if !ok {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
callers = callers + fmt.Sprintf("%v:%v\n", file, line)
|
|
|
|
}
|
2014-06-25 03:51:57 +00:00
|
|
|
glog.Infof("Recovered from panic: %#v (%v)\n%v", r, r, callers)
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-11 00:32:42 +00:00
|
|
|
// Forever loops forever running f every d. Catches any panics, and keeps going.
|
2014-06-06 23:40:48 +00:00
|
|
|
func Forever(f func(), period time.Duration) {
|
2014-11-28 20:14:49 +00:00
|
|
|
Until(f, period, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Until loops until stop channel is closed, running f every d.
|
|
|
|
// Catches any panics, and keeps going. f may not be invoked if
|
|
|
|
// stop channel is already closed.
|
|
|
|
func Until(f func(), period time.Duration, stopCh <-chan struct{}) {
|
2014-06-06 23:40:48 +00:00
|
|
|
for {
|
2014-11-28 20:14:49 +00:00
|
|
|
select {
|
|
|
|
case <-stopCh:
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
2014-06-06 23:40:48 +00:00
|
|
|
func() {
|
|
|
|
defer HandleCrash()
|
|
|
|
f()
|
|
|
|
}()
|
|
|
|
time.Sleep(period)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-10 18:09:47 +00:00
|
|
|
// IntOrString is a type that can hold an int or a string. When used in
|
|
|
|
// JSON or YAML marshalling and unmarshalling, it produces or consumes the
|
|
|
|
// inner type. This allows you to have, for example, a JSON field that can
|
|
|
|
// accept a name or number.
|
|
|
|
type IntOrString struct {
|
|
|
|
Kind IntstrKind
|
|
|
|
IntVal int
|
|
|
|
StrVal string
|
|
|
|
}
|
|
|
|
|
2014-07-11 00:32:42 +00:00
|
|
|
// IntstrKind represents the stored type of IntOrString.
|
2014-07-10 18:09:47 +00:00
|
|
|
type IntstrKind int
|
|
|
|
|
|
|
|
const (
|
2014-07-11 00:32:42 +00:00
|
|
|
IntstrInt IntstrKind = iota // The IntOrString holds an int.
|
|
|
|
IntstrString // The IntOrString holds a string.
|
2014-07-10 18:09:47 +00:00
|
|
|
)
|
|
|
|
|
2014-08-21 04:27:19 +00:00
|
|
|
// NewIntOrStringFromInt creates an IntOrString object with an int value.
|
|
|
|
func NewIntOrStringFromInt(val int) IntOrString {
|
2014-08-11 06:26:42 +00:00
|
|
|
return IntOrString{Kind: IntstrInt, IntVal: val}
|
|
|
|
}
|
|
|
|
|
2014-09-02 10:00:28 +00:00
|
|
|
// NewIntOrStringFromString creates an IntOrString object with a string value.
|
2014-08-21 04:27:19 +00:00
|
|
|
func NewIntOrStringFromString(val string) IntOrString {
|
2014-08-11 06:26:42 +00:00
|
|
|
return IntOrString{Kind: IntstrString, StrVal: val}
|
|
|
|
}
|
|
|
|
|
2014-07-11 00:32:42 +00:00
|
|
|
// UnmarshalJSON implements the json.Unmarshaller interface.
|
2014-07-10 18:09:47 +00:00
|
|
|
func (intstr *IntOrString) UnmarshalJSON(value []byte) error {
|
|
|
|
if value[0] == '"' {
|
|
|
|
intstr.Kind = IntstrString
|
|
|
|
return json.Unmarshal(value, &intstr.StrVal)
|
|
|
|
}
|
|
|
|
intstr.Kind = IntstrInt
|
|
|
|
return json.Unmarshal(value, &intstr.IntVal)
|
|
|
|
}
|
|
|
|
|
2014-07-11 00:32:42 +00:00
|
|
|
// MarshalJSON implements the json.Marshaller interface.
|
2014-07-10 18:09:47 +00:00
|
|
|
func (intstr IntOrString) MarshalJSON() ([]byte, error) {
|
|
|
|
switch intstr.Kind {
|
|
|
|
case IntstrInt:
|
|
|
|
return json.Marshal(intstr.IntVal)
|
|
|
|
case IntstrString:
|
|
|
|
return json.Marshal(intstr.StrVal)
|
|
|
|
default:
|
2014-07-11 18:22:22 +00:00
|
|
|
return []byte{}, fmt.Errorf("impossible IntOrString.Kind")
|
2014-07-10 18:09:47 +00:00
|
|
|
}
|
|
|
|
}
|
2014-07-26 00:58:36 +00:00
|
|
|
|
2014-09-04 17:55:30 +00:00
|
|
|
// Takes a list of strings and compiles them into a list of regular expressions
|
2014-10-08 13:23:25 +00:00
|
|
|
func CompileRegexps(regexpStrings []string) ([]*regexp.Regexp, error) {
|
2014-09-04 17:55:30 +00:00
|
|
|
regexps := []*regexp.Regexp{}
|
|
|
|
for _, regexpStr := range regexpStrings {
|
|
|
|
r, err := regexp.Compile(regexpStr)
|
|
|
|
if err != nil {
|
|
|
|
return []*regexp.Regexp{}, err
|
|
|
|
}
|
|
|
|
regexps = append(regexps, r)
|
|
|
|
}
|
|
|
|
return regexps, nil
|
|
|
|
}
|