Browse Source

add custom probe interval for observer

Shelikhoo 4 years ago
parent
commit
54fc21e537

+ 20 - 10
app/observatory/config.pb.go

@@ -294,6 +294,7 @@ type Config struct {
 	// @Document The selectors for outbound under observation
 	SubjectSelector []string `protobuf:"bytes,2,rep,name=subject_selector,json=subjectSelector,proto3" json:"subject_selector,omitempty"`
 	ProbeUrl        string   `protobuf:"bytes,3,opt,name=probe_url,json=probeUrl,proto3" json:"probe_url,omitempty"`
+	ProbeInterval   int64    `protobuf:"varint,4,opt,name=probe_interval,json=probeInterval,proto3" json:"probe_interval,omitempty"`
 }
 
 func (x *Config) Reset() {
@@ -342,6 +343,13 @@ func (x *Config) GetProbeUrl() string {
 	return ""
 }
 
+func (x *Config) GetProbeInterval() int64 {
+	if x != nil {
+		return x.ProbeInterval
+	}
+	return 0
+}
+
 var File_app_observatory_config_proto protoreflect.FileDescriptor
 
 var file_app_observatory_config_proto_rawDesc = []byte{
@@ -377,20 +385,22 @@ var file_app_observatory_config_proto_rawDesc = []byte{
 	0x6f, 0x6e, 0x22, 0x32, 0x0a, 0x09, 0x49, 0x6e, 0x74, 0x65, 0x6e, 0x73, 0x69, 0x74, 0x79, 0x12,
 	0x25, 0x0a, 0x0e, 0x70, 0x72, 0x6f, 0x62, 0x65, 0x5f, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x76, 0x61,
 	0x6c, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x0d, 0x70, 0x72, 0x6f, 0x62, 0x65, 0x49, 0x6e,
-	0x74, 0x65, 0x72, 0x76, 0x61, 0x6c, 0x22, 0x50, 0x0a, 0x06, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67,
+	0x74, 0x65, 0x72, 0x76, 0x61, 0x6c, 0x22, 0x77, 0x0a, 0x06, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67,
 	0x12, 0x29, 0x0a, 0x10, 0x73, 0x75, 0x62, 0x6a, 0x65, 0x63, 0x74, 0x5f, 0x73, 0x65, 0x6c, 0x65,
 	0x63, 0x74, 0x6f, 0x72, 0x18, 0x02, 0x20, 0x03, 0x28, 0x09, 0x52, 0x0f, 0x73, 0x75, 0x62, 0x6a,
 	0x65, 0x63, 0x74, 0x53, 0x65, 0x6c, 0x65, 0x63, 0x74, 0x6f, 0x72, 0x12, 0x1b, 0x0a, 0x09, 0x70,
 	0x72, 0x6f, 0x62, 0x65, 0x5f, 0x75, 0x72, 0x6c, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08,
-	0x70, 0x72, 0x6f, 0x62, 0x65, 0x55, 0x72, 0x6c, 0x42, 0x6f, 0x0a, 0x1e, 0x63, 0x6f, 0x6d, 0x2e,
-	0x76, 0x32, 0x72, 0x61, 0x79, 0x2e, 0x63, 0x6f, 0x72, 0x65, 0x2e, 0x61, 0x70, 0x70, 0x2e, 0x6f,
-	0x62, 0x73, 0x65, 0x72, 0x76, 0x61, 0x74, 0x6f, 0x72, 0x79, 0x50, 0x01, 0x5a, 0x2e, 0x67, 0x69,
-	0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x76, 0x32, 0x66, 0x6c, 0x79, 0x2f, 0x76,
-	0x32, 0x72, 0x61, 0x79, 0x2d, 0x63, 0x6f, 0x72, 0x65, 0x2f, 0x76, 0x34, 0x2f, 0x61, 0x70, 0x70,
-	0x2f, 0x6f, 0x62, 0x73, 0x65, 0x72, 0x76, 0x61, 0x74, 0x6f, 0x72, 0x79, 0xaa, 0x02, 0x1a, 0x56,
-	0x32, 0x52, 0x61, 0x79, 0x2e, 0x43, 0x6f, 0x72, 0x65, 0x2e, 0x41, 0x70, 0x70, 0x2e, 0x4f, 0x62,
-	0x73, 0x65, 0x72, 0x76, 0x61, 0x74, 0x6f, 0x72, 0x79, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f,
-	0x33,
+	0x70, 0x72, 0x6f, 0x62, 0x65, 0x55, 0x72, 0x6c, 0x12, 0x25, 0x0a, 0x0e, 0x70, 0x72, 0x6f, 0x62,
+	0x65, 0x5f, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x76, 0x61, 0x6c, 0x18, 0x04, 0x20, 0x01, 0x28, 0x03,
+	0x52, 0x0d, 0x70, 0x72, 0x6f, 0x62, 0x65, 0x49, 0x6e, 0x74, 0x65, 0x72, 0x76, 0x61, 0x6c, 0x42,
+	0x6f, 0x0a, 0x1e, 0x63, 0x6f, 0x6d, 0x2e, 0x76, 0x32, 0x72, 0x61, 0x79, 0x2e, 0x63, 0x6f, 0x72,
+	0x65, 0x2e, 0x61, 0x70, 0x70, 0x2e, 0x6f, 0x62, 0x73, 0x65, 0x72, 0x76, 0x61, 0x74, 0x6f, 0x72,
+	0x79, 0x50, 0x01, 0x5a, 0x2e, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f,
+	0x76, 0x32, 0x66, 0x6c, 0x79, 0x2f, 0x76, 0x32, 0x72, 0x61, 0x79, 0x2d, 0x63, 0x6f, 0x72, 0x65,
+	0x2f, 0x76, 0x34, 0x2f, 0x61, 0x70, 0x70, 0x2f, 0x6f, 0x62, 0x73, 0x65, 0x72, 0x76, 0x61, 0x74,
+	0x6f, 0x72, 0x79, 0xaa, 0x02, 0x1a, 0x56, 0x32, 0x52, 0x61, 0x79, 0x2e, 0x43, 0x6f, 0x72, 0x65,
+	0x2e, 0x41, 0x70, 0x70, 0x2e, 0x4f, 0x62, 0x73, 0x65, 0x72, 0x76, 0x61, 0x74, 0x6f, 0x72, 0x79,
+	0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
 }
 
 var (

+ 2 - 0
app/observatory/config.proto

@@ -66,4 +66,6 @@ message Config {
   repeated string subject_selector = 2;
 
   string probe_url = 3;
+
+  int64 probe_interval = 4;
 }

+ 5 - 1
app/observatory/observer.go

@@ -76,7 +76,11 @@ func (o *Observer) background() {
 			if o.finished.Done() {
 				return
 			}
-			time.Sleep(time.Second * 10)
+			sleepTime := time.Second * 10
+			if o.config.ProbeInterval != 0 {
+				sleepTime = time.Duration(o.config.ProbeInterval)
+			}
+			time.Sleep(sleepTime)
 		}
 	}
 }

+ 33 - 0
infra/conf/cfgcommon/duration/duration.go

@@ -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 - 0
infra/conf/cfgcommon/duration/duration_test.go

@@ -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))
+	}
+}

+ 5 - 3
infra/conf/observatory.go

@@ -2,15 +2,17 @@ package conf
 
 import (
 	"github.com/golang/protobuf/proto"
+	"github.com/v2fly/v2ray-core/v4/infra/conf/cfgcommon/duration"
 
 	"github.com/v2fly/v2ray-core/v4/app/observatory"
 )
 
 type ObservatoryConfig struct {
-	SubjectSelector []string `json:"subjectSelector"`
-	ProbeURL        string   `json:"probeURL"`
+	SubjectSelector []string          `json:"subjectSelector"`
+	ProbeURL        string            `json:"probeURL"`
+	ProbeInterval   duration.Duration `json:"ProbeInterval"`
 }
 
 func (o *ObservatoryConfig) Build() (proto.Message, error) {
-	return &observatory.Config{SubjectSelector: o.SubjectSelector, ProbeUrl: o.ProbeURL}, nil
+	return &observatory.Config{SubjectSelector: o.SubjectSelector, ProbeUrl: o.ProbeURL, ProbeInterval: int64(o.ProbeInterval)}, nil
 }