Skip to content

Commit

Permalink
Merge pull request #149 from brzyangg/master
Browse files Browse the repository at this point in the history
自动 watchUpdate 热加载配置
  • Loading branch information
niubell authored Mar 4, 2020
2 parents a713cc9 + 1860685 commit 75dca12
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 16 deletions.
1 change: 1 addition & 0 deletions cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,4 +231,5 @@ func init() {
} else {
slog.Infof(ctx, "%s cache configer:%v been set", fun, constants.ConfigerTypeApollo)
}
WatchUpdate(ctx)
}
33 changes: 20 additions & 13 deletions cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package cache
import (
"context"
"encoding/json"
"fmt"
"github.com/stretchr/testify/assert"
"testing"
"time"
)

type Test struct {
Expand Down Expand Up @@ -62,22 +64,27 @@ func TestCache_Set(t *testing.T) {

func TestCache_Get(t *testing.T) {
ctx := context.Background()
c, err := NewCacheByNamespace(ctx, "test/test","test",60)
c, err := NewCacheByNamespace(ctx, "base/test","test",60)
if err != nil {
t.Errorf("NewCacheByNamespace err: %s", err.Error())
}
WatchUpdate(ctx)
//WatchUpdate(ctx)
var test Test
err = c.Get("test", &test)
if err != nil {
t.Errorf("Get err: %s", err.Error())
}
t.Log(test)
c.Del("test")
// load
err = c.Get("test", &test)
if err != nil {
t.Errorf("Get err: %s", err.Error())
for {
err = c.Get("test", &test)
if err != nil {
t.Errorf("Get err: %s", err.Error())
return
}
fmt.Println(test)
time.Sleep(time.Second)
}
assert.Equal(t, 1, test.ID)
//t.Log(test)
//c.Del("test")
//// load
//err = c.Get("test", &test)
//if err != nil {
// t.Errorf("Get err: %s", err.Error())
//}
//assert.Equal(t, 1, test.ID)
}
5 changes: 5 additions & 0 deletions cache/redis/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,11 @@ func (m *InstanceManager) applyChangeEvent(ctx context.Context, ce *center.Chang

func (m *InstanceManager) Watch(ctx context.Context) {
fun := "InstanceManager.Watch-->"
defer func() {
if r := recover(); r != nil {
slog.Errorf(ctx, "%s recover r: %v", fun, r)
}
}()
m.watchOnce.Do(func() {
slog.Infof(ctx, "%s start watching updates", fun)
ceChan := DefaultConfiger.Watch(ctx)
Expand Down
2 changes: 2 additions & 0 deletions cache/redisext/redisext.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package redisext
import (
"context"
"fmt"
"github.com/shawnfeng/sutil/cache"
"time"

redis2 "github.com/go-redis/redis"
Expand Down Expand Up @@ -917,4 +918,5 @@ func init() {
} else {
slog.Infof(ctx, "%s redisext configer:%v been set", fun, constants.ConfigerTypeApollo)
}
WatchUpdate(ctx)
}
1 change: 1 addition & 0 deletions cache/value/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,4 +276,5 @@ func init() {
} else {
slog.Infof(ctx, "%s cache configer:%v been set", fun, constants.ConfigerTypeApollo)
}
WatchUpdate(ctx)
}
6 changes: 3 additions & 3 deletions mq/examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type Msg struct {
func main() {

_ = trace.InitDefaultTracer("mq.test")
topic := "palfish.test.test1"
topic := "palfish.test.test"

ctx := context.Background()
ctx = context.WithValue(ctx, scontext.ContextKeyHead, "hahahaha")
Expand All @@ -33,8 +33,8 @@ func main() {
defer span.Finish()
}

_ = mq.SetConfiger(ctx, mq.ConfigerTypeApollo)
mq.WatchUpdate(ctx)
//_ = mq.SetConfiger(ctx, mq.ConfigerTypeApollo)
//mq.WatchUpdate(ctx)

/*
go func() {
Expand Down
5 changes: 5 additions & 0 deletions mq/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,11 @@ func (m *InstanceManager) applyChangeEvent(ctx context.Context, ce *center.Chang

func (m *InstanceManager) watch(ctx context.Context) {
fun := "InstanceManager.watch-->"
defer func() {
if r := recover(); r != nil {
slog.Errorf(ctx, "%s recover r: %v", fun, r)
}
}()
m.watchOnce.Do(func() {
slog.Infof(ctx, "%s start watching updates", fun)
ceChan := DefaultConfiger.Watch(ctx)
Expand Down
1 change: 1 addition & 0 deletions mq/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -456,4 +456,5 @@ func init() {
} else {
slog.Infof(ctx, "%s mq configer:%v been set", fun, ConfigerTypeApollo)
}
WatchUpdate(ctx)
}

0 comments on commit 75dca12

Please sign in to comment.