Skip to content

Commit ae5b749

Browse files
author
samshi
committed
feat: update base and example
1 parent 0fab75b commit ae5b749

File tree

7 files changed

+33
-28
lines changed

7 files changed

+33
-28
lines changed

schedule/example/go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ replace github.com/tencent-connect/botgo-plugins/schedule => ../
66

77
require (
88
github.com/tencent-connect/botgo v0.0.0-20211122124126-a4936f507e42
9-
github.com/tencent-connect/botgo-plugins/cluster/base v0.0.0-20211124034518-37adad080eb7
10-
github.com/tencent-connect/botgo-plugins/cluster/impl/etcd v0.0.0-20211124035403-b2b577538a18
9+
github.com/tencent-connect/botgo-plugins/cluster/base v0.0.0-20211124073815-757ae5fa4913
10+
github.com/tencent-connect/botgo-plugins/cluster/impl/etcd v0.0.0-20211124080534-0fab75bca648
1111
github.com/tencent-connect/botgo-plugins/schedule v0.0.0-00010101000000-000000000000
1212
)

schedule/example/go.sum

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,14 +121,21 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
121121
github.com/tencent-connect/botgo v0.0.0-20211122124126-a4936f507e42 h1:WLhRgqr1iysdg0lYK/6gP+QDwxBCtEgBrn5DEh/D2kg=
122122
github.com/tencent-connect/botgo v0.0.0-20211122124126-a4936f507e42/go.mod h1:tevM5u+HybWL417Ef4YexIZj/mkTlB3zfCJ07f+EE+g=
123123
github.com/tencent-connect/botgo-plugins v0.0.0-20211124035403-b2b577538a18 h1:V6iIqIIyDO9j/oIIxwQgDnOXN1gxpmq8JQxvuzITSCU=
124+
github.com/tencent-connect/botgo-plugins v0.0.0-20211124073815-757ae5fa4913 h1:9z4QxvtZmvGUbJ0uUHthnreil3v7cwO9f44QdoSQ3K0=
124125
github.com/tencent-connect/botgo-plugins/cluster/base v0.0.0-20211122141011-6d922fabf381 h1:yyJPc7X6EUk0MTcV/DcP5zUCxaPg22/vLmegf92bu7c=
125126
github.com/tencent-connect/botgo-plugins/cluster/base v0.0.0-20211122141011-6d922fabf381/go.mod h1:Mqc/VXp1cMJehHB6TC0EWf2sXsDTZwliNOkytByuxjI=
126127
github.com/tencent-connect/botgo-plugins/cluster/base v0.0.0-20211124034518-37adad080eb7 h1:Fz+8sKChKw66LkT8LsDLro3G1BGWPfEDEtX9HkGs78Q=
127128
github.com/tencent-connect/botgo-plugins/cluster/base v0.0.0-20211124034518-37adad080eb7/go.mod h1:Mqc/VXp1cMJehHB6TC0EWf2sXsDTZwliNOkytByuxjI=
129+
github.com/tencent-connect/botgo-plugins/cluster/base v0.0.0-20211124073815-757ae5fa4913 h1:IsBPvxKKdZBqLpnm0EbgxwD3dQnnmeWSZwWpOo4McTs=
130+
github.com/tencent-connect/botgo-plugins/cluster/base v0.0.0-20211124073815-757ae5fa4913/go.mod h1:Mqc/VXp1cMJehHB6TC0EWf2sXsDTZwliNOkytByuxjI=
128131
github.com/tencent-connect/botgo-plugins/cluster/impl/etcd v0.0.0-20211122142543-8a0b12d77536 h1:LRA7T6EpST/kCw3BHpkH9R9QsVzoGzFJh9T+ZIwOSug=
129132
github.com/tencent-connect/botgo-plugins/cluster/impl/etcd v0.0.0-20211122142543-8a0b12d77536/go.mod h1:KLhTRRWXX3KA0LSBpT+QwBvX5gNmVEA6ecyck/q8qMc=
130133
github.com/tencent-connect/botgo-plugins/cluster/impl/etcd v0.0.0-20211124035403-b2b577538a18 h1:QabzxhjoV45VyjlbdzEUIbw+KK8XnYFYP5VgTnGoXDg=
131134
github.com/tencent-connect/botgo-plugins/cluster/impl/etcd v0.0.0-20211124035403-b2b577538a18/go.mod h1:pKeRAKbXN3d7vy3eOPvKSQw3DIEPQPLNVZ+RbVmXziY=
135+
github.com/tencent-connect/botgo-plugins/cluster/impl/etcd v0.0.0-20211124073815-757ae5fa4913 h1:UpYTmT1cv2nu046TUmRS4l9tS9lOi8/zyir/6RogubU=
136+
github.com/tencent-connect/botgo-plugins/cluster/impl/etcd v0.0.0-20211124073815-757ae5fa4913/go.mod h1:pKeRAKbXN3d7vy3eOPvKSQw3DIEPQPLNVZ+RbVmXziY=
137+
github.com/tencent-connect/botgo-plugins/cluster/impl/etcd v0.0.0-20211124080534-0fab75bca648 h1:Tqdd+yKa8ZfuEoJ0ofd0NHVDrb+zb5j3aS6oM7R2QMU=
138+
github.com/tencent-connect/botgo-plugins/cluster/impl/etcd v0.0.0-20211124080534-0fab75bca648/go.mod h1:f/aOFm65lNGSSDOD97lQfGniZHdUJBZXvW5QmovD4XI=
132139
github.com/tidwall/gjson v1.9.3 h1:hqzS9wAHMO+KVBBkLxYdkEeeFHuqr95GfClRLKlgK0E=
133140
github.com/tidwall/gjson v1.9.3/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
134141
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=

schedule/example/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ func main() {
3535
}
3636
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
3737
defer cancel()
38-
// name传空,自动使用ip作为名称,如果是容器场景,可能存在ip相同情况,此时请使用例如容器id作为name,而不要使用ip,避免实例名重复引发问题
38+
// id传空,自动使用ip作为名称,如果是容器场景,可能存在ip相同情况,
39+
// 此时请使用例如容器id而不要使用ip,避免实例名重复引发问题
3940
ins, err := cluster.RegInstance(ctx, "")
4041
if err != nil {
4142
fmt.Printf("reg failed. err:%v\n", err)

schedule/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ go 1.15
55
require (
66
github.com/agiledragon/gomonkey/v2 v2.2.0
77
github.com/tencent-connect/botgo v0.0.0-20211122124126-a4936f507e42
8-
github.com/tencent-connect/botgo-plugins/cluster/base v0.0.0-20211124034518-37adad080eb7
8+
github.com/tencent-connect/botgo-plugins/cluster/base v0.0.0-20211124073815-757ae5fa4913
99
)

schedule/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ github.com/tencent-connect/botgo-plugins/cluster/base v0.0.0-20211122141011-6d92
1919
github.com/tencent-connect/botgo-plugins/cluster/base v0.0.0-20211122141011-6d922fabf381/go.mod h1:Mqc/VXp1cMJehHB6TC0EWf2sXsDTZwliNOkytByuxjI=
2020
github.com/tencent-connect/botgo-plugins/cluster/base v0.0.0-20211124034518-37adad080eb7 h1:Fz+8sKChKw66LkT8LsDLro3G1BGWPfEDEtX9HkGs78Q=
2121
github.com/tencent-connect/botgo-plugins/cluster/base v0.0.0-20211124034518-37adad080eb7/go.mod h1:Mqc/VXp1cMJehHB6TC0EWf2sXsDTZwliNOkytByuxjI=
22+
github.com/tencent-connect/botgo-plugins/cluster/base v0.0.0-20211124073815-757ae5fa4913 h1:IsBPvxKKdZBqLpnm0EbgxwD3dQnnmeWSZwWpOo4McTs=
23+
github.com/tencent-connect/botgo-plugins/cluster/base v0.0.0-20211124073815-757ae5fa4913/go.mod h1:Mqc/VXp1cMJehHB6TC0EWf2sXsDTZwliNOkytByuxjI=
2224
github.com/tidwall/gjson v1.9.3 h1:hqzS9wAHMO+KVBBkLxYdkEeeFHuqr95GfClRLKlgK0E=
2325
github.com/tidwall/gjson v1.9.3/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
2426
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=

schedule/schedule.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ func (sched *Scheduler) sharding() error {
169169
}
170170

171171
func (sched *Scheduler) isSelf(ins base.Instance) bool {
172-
return sched.localInstance.IsSame(ins)
172+
return sched.localInstance.GetID() == ins.GetID()
173173
}
174174

175175
func (s *shardInfo) isValid() bool {
@@ -285,7 +285,7 @@ func (sched *Scheduler) countValidIns(allIns []base.Instance) (int, uint) {
285285
var validInsNum uint
286286
selfIdx := -1
287287
for _, ins := range allIns {
288-
log.Debugf("[Instance] %v", ins.GetName())
288+
log.Debugf("[Instance] %v", ins.GetID())
289289
if !ins.IsValid() {
290290
// 跳过无效instance
291291
continue

schedule/schedule_test.go

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ var testArgs = Args{
2626
var testScheduler = &Scheduler{
2727
args: &testArgs,
2828
sessionCtx: nil,
29-
localInstance: &mockInstance{name: "127.0.0.1"},
29+
localInstance: &mockInstance{id: "127.0.0.1"},
3030
}
3131

3232
type mockCluster struct {
3333
}
3434

3535
// RegInstance 注册本地实例
36-
func (m *mockCluster) RegInstance(ctx context.Context, name string) (base.Instance, error) {
36+
func (m *mockCluster) RegInstance(ctx context.Context, id string) (base.Instance, error) {
3737
return nil, nil
3838
}
3939

@@ -44,7 +44,7 @@ func (m *mockCluster) UnregInstance(ctx context.Context) error {
4444

4545
// GetLocalInstance 获取本地实例
4646
func (m *mockCluster) GetLocalInstance(ctx context.Context) (base.Instance, error) {
47-
return &mockInstance{name: "127.0.0.1"}, nil
47+
return &mockInstance{id: "127.0.0.1"}, nil
4848
}
4949

5050
// GetAllInstances 获取所有实例的列表
@@ -188,7 +188,7 @@ func TestScheduler_calShard(t *testing.T) {
188188
name: "case2",
189189
args: args{
190190
allIns: []base.Instance{
191-
&mockInstance{name: ""}, &mockInstance{name: "127.0.0.1"},
191+
&mockInstance{id: ""}, &mockInstance{id: "127.0.0.1"},
192192
},
193193
},
194194
want: &shardInfo{
@@ -199,9 +199,9 @@ func TestScheduler_calShard(t *testing.T) {
199199
name: "case with min ws shard num 1",
200200
args: args{
201201
allIns: []base.Instance{
202-
&mockInstance{name: "fakeip1"},
203-
&mockInstance{name: "fakeip2"},
204-
&mockInstance{name: "127.0.0.1"},
202+
&mockInstance{id: "fakeip1"},
203+
&mockInstance{id: "fakeip2"},
204+
&mockInstance{id: "127.0.0.1"},
205205
},
206206
},
207207
want: &shardInfo{
@@ -212,9 +212,9 @@ func TestScheduler_calShard(t *testing.T) {
212212
name: "case with min ws shard num 2",
213213
args: args{
214214
allIns: []base.Instance{
215-
&mockInstance{name: "127.0.0.1"},
216-
&mockInstance{name: "fakeip3"},
217-
&mockInstance{name: "fakeip4"},
215+
&mockInstance{id: "127.0.0.1"},
216+
&mockInstance{id: "fakeip3"},
217+
&mockInstance{id: "fakeip4"},
218218
},
219219
},
220220
want: &shardInfo{
@@ -225,9 +225,9 @@ func TestScheduler_calShard(t *testing.T) {
225225
name: "case with min ws shard num 3",
226226
args: args{
227227
allIns: []base.Instance{
228-
&mockInstance{name: "fakeip5"},
229-
&mockInstance{name: "127.0.0.1"},
230-
&mockInstance{name: "fakeip6"},
228+
&mockInstance{id: "fakeip5"},
229+
&mockInstance{id: "127.0.0.1"},
230+
&mockInstance{id: "fakeip6"},
231231
},
232232
},
233233
want: &shardInfo{
@@ -259,22 +259,17 @@ func TestScheduler_calShard(t *testing.T) {
259259

260260
// mockInstance 模拟服务实例信息
261261
type mockInstance struct {
262-
name string
262+
id string
263263
}
264264

265265
// GetName 获取名称
266-
func (m *mockInstance) GetName() string {
267-
return m.name
266+
func (m *mockInstance) GetID() string {
267+
return m.id
268268
}
269269

270270
// IsValid 是否有效
271271
func (m *mockInstance) IsValid() bool {
272-
return m.name != ""
273-
}
274-
275-
// IsSame 是否相同
276-
func (m *mockInstance) IsSame(ins base.Instance) bool {
277-
return m.name == ins.GetName()
272+
return m.id != ""
278273
}
279274

280275
func Test_shardsEqual(t *testing.T) {

0 commit comments

Comments
 (0)