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:
33
infra/conf/cfgcommon/duration/duration.go
Normal file
33
infra/conf/cfgcommon/duration/duration.go
Normal 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)
|
||||
}
|
||||
}
|
||||
32
infra/conf/cfgcommon/duration/duration_test.go
Normal file
32
infra/conf/cfgcommon/duration/duration_test.go
Normal 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))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user