mirror of
https://github.com/kubenetworks/kubevpn.git
synced 2025-12-24 11:51:13 +08:00
feat: pprof use random port
This commit is contained in:
@@ -32,11 +32,12 @@ func Main(filename string, port uint, logger *log.Logger) {
|
||||
log.Fatal(fmt.Errorf("failed to create file watcher, err: %v", err))
|
||||
}
|
||||
defer watcher.Close()
|
||||
err = watcher.Add(filename)
|
||||
if err != nil {
|
||||
if err = watcher.Add(filename); err != nil {
|
||||
log.Fatal(fmt.Errorf("failed to add file: %s to wather, err: %v", filename, err))
|
||||
}
|
||||
go Watch(watcher, filename, notifyCh)
|
||||
go func() {
|
||||
log.Fatal(Watch(watcher, filename, notifyCh))
|
||||
}()
|
||||
|
||||
for {
|
||||
select {
|
||||
|
||||
@@ -2,6 +2,7 @@ package controlplane
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"math"
|
||||
"math/rand"
|
||||
@@ -55,10 +56,11 @@ func (p *Processor) ProcessFile(file NotifyMessage) {
|
||||
}
|
||||
lastConfig, ok := p.expireCache.Get(config.Uid)
|
||||
if ok && reflect.DeepEqual(lastConfig.(*Virtual), config) {
|
||||
p.logger.Infof("config are same, not needs to update, %v", config)
|
||||
marshal, _ := json.Marshal(config)
|
||||
p.logger.Debugf("config are same, not needs to update, config: %s", string(marshal))
|
||||
continue
|
||||
}
|
||||
p.logger.Infof("update config, version %d, config %v", p.version, config)
|
||||
p.logger.Debugf("update config, version %d, config %v", p.version, config)
|
||||
|
||||
listeners, clusters, routes, endpoints := config.To()
|
||||
resources := map[resource.Type][]types.Resource{
|
||||
@@ -69,7 +71,8 @@ func (p *Processor) ProcessFile(file NotifyMessage) {
|
||||
resource.RuntimeType: {}, // runtimes
|
||||
resource.SecretType: {}, // secrets
|
||||
}
|
||||
snapshot, err := cache.NewSnapshot(p.newVersion(), resources)
|
||||
var snapshot *cache.Snapshot
|
||||
snapshot, err = cache.NewSnapshot(p.newVersion(), resources)
|
||||
|
||||
if err != nil {
|
||||
p.logger.Errorf("snapshot inconsistency: %v, err: %v", snapshot, err)
|
||||
@@ -85,6 +88,7 @@ func (p *Processor) ProcessFile(file NotifyMessage) {
|
||||
p.logger.Errorf("snapshot error %q for %v", err, snapshot)
|
||||
p.logger.Fatal(err)
|
||||
}
|
||||
|
||||
p.expireCache.Set(config.Uid, config, time.Minute*5)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@ package controlplane
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
|
||||
clusterservice "github.com/envoyproxy/go-control-plane/envoy/service/cluster/v3"
|
||||
@@ -14,6 +13,7 @@ import (
|
||||
runtimeservice "github.com/envoyproxy/go-control-plane/envoy/service/runtime/v3"
|
||||
secretservice "github.com/envoyproxy/go-control-plane/envoy/service/secret/v3"
|
||||
serverv3 "github.com/envoyproxy/go-control-plane/pkg/server/v3"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
@@ -38,7 +38,7 @@ func RunServer(ctx context.Context, server serverv3.Server, port uint) {
|
||||
secretservice.RegisterSecretDiscoveryServiceServer(grpcServer, server)
|
||||
runtimeservice.RegisterRuntimeDiscoveryServiceServer(grpcServer, server)
|
||||
|
||||
log.Printf("management server listening on %d\n", port)
|
||||
log.Infof("management server listening on %d", port)
|
||||
if err = grpcServer.Serve(listener); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package controlplane
|
||||
|
||||
import (
|
||||
"log"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/fsnotify/fsnotify"
|
||||
@@ -20,12 +20,14 @@ type NotifyMessage struct {
|
||||
FilePath string
|
||||
}
|
||||
|
||||
func Watch(watcher *fsnotify.Watcher, filename string, notifyCh chan<- NotifyMessage) {
|
||||
func Watch(watcher *fsnotify.Watcher, filename string, notifyCh chan<- NotifyMessage) error {
|
||||
ticker := time.NewTicker(time.Second * 5)
|
||||
defer ticker.Stop()
|
||||
for {
|
||||
select {
|
||||
case event, ok := <-watcher.Events:
|
||||
if !ok {
|
||||
return
|
||||
return fmt.Errorf("watcher has closed")
|
||||
}
|
||||
if event.Op&fsnotify.Write == fsnotify.Write {
|
||||
notifyCh <- NotifyMessage{
|
||||
@@ -46,11 +48,11 @@ func Watch(watcher *fsnotify.Watcher, filename string, notifyCh chan<- NotifyMes
|
||||
|
||||
case err, ok := <-watcher.Errors:
|
||||
if !ok {
|
||||
return
|
||||
return fmt.Errorf("watcher error closed")
|
||||
}
|
||||
log.Println("error:", err)
|
||||
return err
|
||||
|
||||
case <-time.Tick(time.Second * 5):
|
||||
case <-ticker.C:
|
||||
notifyCh <- NotifyMessage{
|
||||
Operation: Modify,
|
||||
FilePath: filename,
|
||||
|
||||
Reference in New Issue
Block a user