Skip to content

Commit

Permalink
<refactor> 上传修改
Browse files Browse the repository at this point in the history
  • Loading branch information
刘乾洪 committed May 19, 2021
1 parent de9ec4b commit bca6c82
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 9 deletions.
5 changes: 0 additions & 5 deletions cmd/cmd.go

This file was deleted.

41 changes: 37 additions & 4 deletions etcd/discovery/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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:

}
}
}
}
Expand All @@ -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))
}
}

Expand Down
5 changes: 5 additions & 0 deletions fyne/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/liuqianhong6007/fyne

go 1.15

require fyne.io/fyne/v2 v2.0.3
22 changes: 22 additions & 0 deletions fyne/main.go
Original file line number Diff line number Diff line change
@@ -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()
}

0 comments on commit bca6c82

Please sign in to comment.