Skip to content

Commit 5f4a6af

Browse files
committed
增加FactoryManager
1 parent f663326 commit 5f4a6af

File tree

4 files changed

+87
-52
lines changed

4 files changed

+87
-52
lines changed

factory/multisource.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// Copyright (C) 2019-2020, Xiongfa Li.
2+
// @author xiongfa.li
3+
// @version V1.0
4+
// Description:
5+
6+
package factory
7+
8+
import (
9+
"github.com/xfali/loadbalance"
10+
)
11+
12+
type LoadBalanceType int
13+
14+
const (
15+
LBRoundRobbin LoadBalanceType = loadbalance.LBRoundRobbin
16+
LBRoundRobbinWeight LoadBalanceType = loadbalance.LBRoundRobbinWeight
17+
LBRandom LoadBalanceType = loadbalance.LBRandom
18+
LBRandomWeight LoadBalanceType = loadbalance.LBRandomWeight
19+
20+
DefaultGroup = "default"
21+
)
22+
23+
type Manager interface {
24+
Bind(action string, weight int, factory Factory)
25+
Select(action string) Factory
26+
}
27+
28+
type SingleSource struct {
29+
fac Factory
30+
}
31+
32+
func NewSingleSource(fac Factory) *SingleSource {
33+
return &SingleSource{fac: fac}
34+
}
35+
36+
func (lb *SingleSource) Bind(action string, weight int, factory Factory) {
37+
lb.fac = factory
38+
}
39+
40+
func (lb *SingleSource) Select(action string) Factory {
41+
return lb.fac
42+
}
43+
44+
type DefaultMultiSource struct {
45+
lbType int
46+
actionMaps map[string]loadbalance.LoadBalance
47+
factoryMaps map[Factory]loadbalance.LoadBalance
48+
}
49+
50+
func NewMultiSource(t LoadBalanceType) *DefaultMultiSource {
51+
return &DefaultMultiSource{
52+
actionMaps: map[string]loadbalance.LoadBalance{},
53+
lbType: int(t),
54+
}
55+
}
56+
57+
func (lb *DefaultMultiSource) Bind(action string, weight int, factory Factory) {
58+
if action == "" {
59+
action = DefaultGroup
60+
}
61+
62+
if v, ok := lb.actionMaps[action]; ok {
63+
v.Add(weight, factory)
64+
} else {
65+
if f, ok := lb.factoryMaps[factory]; ok {
66+
lb.actionMaps[action] = f
67+
} else {
68+
newlb := loadbalance.Create(lb.lbType)
69+
newlb.Add(weight, factory)
70+
lb.actionMaps[action] = newlb
71+
lb.factoryMaps[factory] = newlb
72+
}
73+
}
74+
}
75+
76+
func (lb *DefaultMultiSource) Select(action string) Factory {
77+
if v, ok := lb.actionMaps[action]; ok {
78+
f := v.Select(nil)
79+
if f != nil {
80+
return f.(Factory)
81+
}
82+
}
83+
return nil
84+
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ require (
66
github.com/go-sql-driver/mysql v1.4.1 // indirect
77
github.com/lib/pq v1.3.0 // indirect
88
github.com/mattn/go-sqlite3 v1.10.0 // indirect
9+
github.com/xfali/loadbalance v0.0.1 // indirect
910
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ github.com/lib/pq v1.3.0 h1:/qkRGz8zljWiDcFvgpwUpwIAPu3r07TDvs3Rws+o/pU=
44
github.com/lib/pq v1.3.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
55
github.com/mattn/go-sqlite3 v1.10.0 h1:jbhqpg7tQe4SupckyijYiy0mJJ/pRyHvXf7JdWK860o=
66
github.com/mattn/go-sqlite3 v1.10.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
7+
github.com/xfali/loadbalance v0.0.1 h1:UVOuuDipJ740KyTaBUxTeW6UEC+fukiQCOn/5YQgexA=
8+
github.com/xfali/loadbalance v0.0.1/go.mod h1:yrzHHRZMdt2wBpLnBDeU2zbjnRYeNvUWV6sq6+3KVG0=

lb/loadbalance.go

Lines changed: 0 additions & 52 deletions
This file was deleted.

0 commit comments

Comments
 (0)