1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2026-01-22 00:55:29 -05:00

add custom probe interval for observer

This commit is contained in:
Shelikhoo
2021-06-30 19:33:37 +01:00
parent 4773e3a1ed
commit 54fc21e537
6 changed files with 97 additions and 14 deletions

View File

@@ -0,0 +1,33 @@
package duration
import (
"encoding/json"
"fmt"
"time"
)
type Duration int64
func (d *Duration) MarshalJSON() ([]byte, error) {
dr := time.Duration(*d)
return json.Marshal(dr.String())
}
func (d *Duration) UnmarshalJSON(b []byte) error {
var v interface{}
if err := json.Unmarshal(b, &v); err != nil {
return err
}
switch value := v.(type) {
case string:
var err error
dr, err := time.ParseDuration(value)
if err != nil {
return err
}
*d = Duration(dr)
return nil
default:
return fmt.Errorf("invalid duration: %v", v)
}
}

View File

@@ -0,0 +1,32 @@
package duration_test
import (
"encoding/json"
"github.com/v2fly/v2ray-core/v4/infra/conf/cfgcommon/duration"
"testing"
"time"
)
type testWithDuration struct {
Duration duration.Duration
}
func TestDurationJSON(t *testing.T) {
expected := &testWithDuration{
Duration: duration.Duration(time.Hour),
}
data, err := json.Marshal(expected)
if err != nil {
t.Error(err)
return
}
actual := &testWithDuration{}
err = json.Unmarshal(data, &actual)
if err != nil {
t.Error(err)
return
}
if actual.Duration != expected.Duration {
t.Errorf("expected: %s, actual: %s", time.Duration(expected.Duration), time.Duration(actual.Duration))
}
}