diff --git a/cmd/cmd.go b/cmd/cmd.go deleted file mode 100644 index 18a25d5..0000000 --- a/cmd/cmd.go +++ /dev/null @@ -1,5 +0,0 @@ -package cmd - -type Command struct { - -} \ No newline at end of file diff --git a/etcd/discovery/discovery.go b/etcd/discovery/discovery.go index ce3ad8b..3d00492 100644 --- a/etcd/discovery/discovery.go +++ b/etcd/discovery/discovery.go @@ -17,6 +17,7 @@ type Discovery struct { leaseGrantMutex sync.Mutex logger *zap.Logger closeChan chan struct{} + sync.Once } func NewDiscovery(endpoints string, logger *zap.Logger) *Discovery { @@ -52,9 +53,6 @@ func (s *Discovery) Register(key, val string) error { return err } - /*keepAliveCtx, keepAliveCancel := context.WithTimeout(context.Background(), 1*time.Nanosecond) - defer keepAliveCancel()*/ - leaseKeepActive, err := s.client.KeepAlive(context.Background(), leaseGrant.ID) if err != nil { return err @@ -63,6 +61,18 @@ func (s *Discovery) Register(key, val string) error { s.leaseGrants[key] = leaseGrant s.logger.Info(fmt.Sprintf("register success: key = %s", key)) + s.doAsync(func() { + <-time.After(10 * time.Second) + + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + + _, err := s.client.Put(ctx, key, "modify", clientv3.WithLease(leaseGrant.ID)) + if err != nil { + panic(err) + } + }) + s.doAsync(func() { for { select { @@ -125,6 +135,29 @@ func (s *Discovery) Watch(keyPrefix string) { for _, ev := range event.Events { s.logger.Info("[watch] receive event", zap.Int("type", int(ev.Type)), zap.String("key", string(ev.Kv.Key)), zap.String("val", string(ev.Kv.Value))) + + switch ev.Type { + case clientv3.EventTypePut: + if ev.Kv.Version == 1 { + s.logger.Info("[CREATE]", zap.String("key", string(ev.Kv.Key)), zap.String("value", string(ev.Kv.Value)), zap.Int64("leaseID", ev.Kv.Lease)) + } else { + s.logger.Info("[UPDATE]", zap.String("key", string(ev.Kv.Key)), zap.String("value", string(ev.Kv.Value)), zap.Int64("leaseID", ev.Kv.Lease)) + s.Once.Do(func() { + s.doAsync(func() { + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + + _, err := s.client.Put(ctx, string(ev.Kv.Key), "modify again", clientv3.WithLease(clientv3.LeaseID(ev.Kv.Lease))) + if err != nil { + panic(err) + } + }) + }) + } + + case clientv3.EventTypeDelete: + + } } } } @@ -140,7 +173,7 @@ func (s *Discovery) Watch(keyPrefix string) { } for _, kv := range rsp.Kvs { - s.logger.Info("[watch] get", zap.String("key", string(kv.Key)), zap.String("val", string(kv.Value))) + s.logger.Info("[watch] get", zap.String("key", string(kv.Key)), zap.String("val", string(kv.Value)), zap.Int64("leaseID", kv.Lease)) } } diff --git a/fyne/go.mod b/fyne/go.mod new file mode 100644 index 0000000..7a19345 --- /dev/null +++ b/fyne/go.mod @@ -0,0 +1,5 @@ +module github.com/liuqianhong6007/fyne + +go 1.15 + +require fyne.io/fyne/v2 v2.0.3 diff --git a/fyne/main.go b/fyne/main.go new file mode 100644 index 0000000..7dc372a --- /dev/null +++ b/fyne/main.go @@ -0,0 +1,22 @@ +package main + +import ( + "fyne.io/fyne/v2/app" + "fyne.io/fyne/v2/container" + "fyne.io/fyne/v2/widget" +) + +func main() { + a := app.New() + w := a.NewWindow("Hello") + + hello := widget.NewLabel("Hello Fyne!") + w.SetContent(container.NewVBox( + hello, + widget.NewButton("Hi!", func() { + hello.SetText("Welcome :)") + }), + )) + + w.ShowAndRun() +}