Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/grayscale release #11

Merged
merged 4 commits into from
Oct 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
type ChannelImpl struct {
id string
Conn
meta Meta
writechan chan []byte
writeWait time.Duration
readwait time.Duration
Expand All @@ -22,10 +23,11 @@ type ChannelImpl struct {
}

// NewChannel NewChannel
func NewChannel(id string, conn Conn, gpool *ants.Pool) Channel {
func NewChannel(id string, meta Meta, conn Conn, gpool *ants.Pool) Channel {
ch := &ChannelImpl{
id: id,
Conn: conn,
meta: meta,
writechan: make(chan []byte, 5),
writeWait: DefaultWriteWait, //default value
readwait: DefaultReadWait,
Expand Down Expand Up @@ -141,3 +143,5 @@ func (ch *ChannelImpl) Readloop(lst MessageListener) error {
}
}
}

func (ch *ChannelImpl) GetMeta() Meta { return ch.meta }
12 changes: 7 additions & 5 deletions default_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func (s *DefaultServer) connHandler(rawconn net.Conn, gpool *ants.Pool) {
rawconn.Close()
return
}
id, err := s.Accept(conn, s.options.Loginwait)
id, meta, err := s.Accept(conn, s.options.Loginwait)
if err != nil {
_ = conn.WriteFrame(OpClose, []byte(err.Error()))
conn.Close()
Expand All @@ -154,8 +154,10 @@ func (s *DefaultServer) connHandler(rawconn net.Conn, gpool *ants.Pool) {
conn.Close()
return
}

channel := NewChannel(id, conn, gpool)
if meta == nil {
meta = Meta{}
}
channel := NewChannel(id, meta, conn, gpool)
channel.SetReadWait(s.options.Readwait)
channel.SetWriteWait(s.options.Writewait)

Expand Down Expand Up @@ -240,6 +242,6 @@ type defaultAcceptor struct {
}

// Accept defaultAcceptor
func (a *defaultAcceptor) Accept(conn Conn, timeout time.Duration) (string, error) {
return ksuid.New().String(), nil
func (a *defaultAcceptor) Accept(conn Conn, timeout time.Duration) (string, Meta, error) {
return ksuid.New().String(), Meta{}, nil
}
8 changes: 4 additions & 4 deletions examples/mock/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,20 @@ type ServerHandler struct {
}

// Accept this connection
func (h *ServerHandler) Accept(conn kim.Conn, timeout time.Duration) (string, error) {
func (h *ServerHandler) Accept(conn kim.Conn, timeout time.Duration) (string, kim.Meta, error) {
// 1. 读取:客户端发送的鉴权数据包
frame, err := conn.ReadFrame()
if err != nil {
return "", err
return "", nil, err
}
// 2. 解析:数据包内容就是userId
userID := string(frame.GetPayload())
// 3. 鉴权:这里只是为了示例做一个fake验证,非空
if userID == "" {
return "", errors.New("user id is invalid")
return "", nil, errors.New("user id is invalid")
}
logger.Infof("logined %s", userID)
return userID, nil
return userID, nil, nil
}

// Receive default listener
Expand Down
8 changes: 5 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ require (
github.com/golang/mock v1.6.0
github.com/golang/protobuf v1.4.3
github.com/hashicorp/consul/api v1.8.1
github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88 // indirect
github.com/kataras/iris/v12 v12.2.0-alpha2.0.20210705170737-afb15b860124
github.com/kelseyhightower/envconfig v1.4.0
github.com/kr/text v0.2.0 // indirect
github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible
github.com/lestrrat-go/strftime v1.0.4 // indirect
github.com/lionsoul2014/ip2region v2.2.0-release+incompatible // indirect
github.com/lionsoul2014/ip2region v2.2.0-release+incompatible
github.com/mattn/go-colorable v0.1.8 // indirect
github.com/mattn/go-isatty v0.0.13 // indirect
github.com/panjf2000/ants/v2 v2.4.6
Expand All @@ -29,9 +30,10 @@ require (
github.com/spf13/cobra v0.0.5
github.com/spf13/viper v1.7.1
github.com/stretchr/testify v1.7.0
github.com/yudai/pp v2.0.1+incompatible // indirect
golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e // indirect
golang.org/x/net v0.0.0-20210614182718-04defd469f4e // indirect
golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf // indirect
golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d // indirect
golang.org/x/sys v0.0.0-20211020174200-9d6173849985 // indirect
google.golang.org/grpc v1.33.2
google.golang.org/protobuf v1.25.0
gorm.io/driver/mysql v1.1.1
Expand Down
13 changes: 9 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,8 @@ github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88 h1:uC1QfSlInpQF+M0ao65imhwqKnz3Q2z/d8PWZRMQvDM=
github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88/go.mod h1:3w7q1U84EfirKl04SVQ/s7nPm1ZPhiXd34z40TNz36k=
github.com/kataras/blocks v0.0.4 h1:lvp/Yr7WoYJKuHpI8f4Shlsl1lb+PE2Lyt0qta5kYWA=
github.com/kataras/blocks v0.0.4/go.mod h1:fu8wIPm3TgpiqW1fdPUSR8m/VMcZgj52vBYe1aS1mu0=
github.com/kataras/golog v0.1.7 h1:0TY5tHn5L5DlRIikepcaRR/6oInIr9AiWsxzt0vvlBE=
Expand Down Expand Up @@ -445,6 +447,8 @@ github.com/yudai/gojsondiff v1.0.0 h1:27cbfqXLVEJ1o8I6v3y9lg8Ydm53EKqHXAOMxEGlCO
github.com/yudai/gojsondiff v1.0.0/go.mod h1:AY32+k2cwILAkW1fbgxQ5mUmMiZFgLIV+FBNExI05xg=
github.com/yudai/golcs v0.0.0-20170316035057-ecda9a501e82 h1:BHyfKlQyqbsFN5p3IfnEUduWvb9is428/nNb5L3U01M=
github.com/yudai/golcs v0.0.0-20170316035057-ecda9a501e82/go.mod h1:lgjkn3NuSvDfVJdfcVVdX+jpBxNmX4rDAzaS45IcYoM=
github.com/yudai/pp v2.0.1+incompatible h1:Q4//iY4pNF6yPLZIigmvcl7k/bPgrcTPIFIcmawg5bI=
github.com/yudai/pp v2.0.1+incompatible/go.mod h1:PuxR/8QJ7cyCkFp/aUDS+JY727OFEZkTdatxwunjIkc=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
Expand Down Expand Up @@ -513,8 +517,8 @@ golang.org/x/net v0.0.0-20201202161906-c7110b5ffcbb/go.mod h1:sp8m0HH+o8qH0wwXwY
golang.org/x/net v0.0.0-20210119194325-5f4716e94777/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
golang.org/x/net v0.0.0-20210614182718-04defd469f4e h1:XpT3nA5TvE525Ne3hInMh6+GETgn27Zfm9dxsThnX2Q=
golang.org/x/net v0.0.0-20210614182718-04defd469f4e/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d h1:20cMwl2fHAzkJMEA+8J4JgqBQcQGzbisXo31MIeenXI=
golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
Expand All @@ -524,6 +528,7 @@ golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Expand Down Expand Up @@ -564,8 +569,8 @@ golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf h1:2ucpDCmfkl8Bd/FsLtiD653Wf96cW37s+iGx93zsu4k=
golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211020174200-9d6173849985 h1:LOlKVhfDyahgmqa97awczplwkjzNaELFg3zRIJ13RYo=
golang.org/x/sys v0.0.0-20211020174200-9d6173849985/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand Down
5 changes: 4 additions & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ type Server interface {
type Acceptor interface {
// Accept 返回一个握手完成的Channel对象或者一个error。
// 业务层需要处理不同协议和网络环境下的连接握手协议
Accept(Conn, time.Duration) (string, error)
Accept(Conn, time.Duration) (string, Meta, error)
}

// MessageListener 监听消息
Expand All @@ -82,10 +82,13 @@ type StateListener interface {
Disconnect(string) error
}

type Meta map[string]string

// Agent is interface of client side
type Agent interface {
ID() string
Push([]byte) error
GetMeta() Meta
}

// Conn Connection
Expand Down
35 changes: 32 additions & 3 deletions server_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion services/gateway/conf/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type Config struct {
ConsulURL string
MonitorPort int `default:"8001"`
AppSecret string
LogLevel string `default:"INFO"`
LogLevel string `default:"DEBUG"`
MessageGPool int `default:"10000"`
ConnectionGPool int `default:"15000"`
}
Expand Down
64 changes: 64 additions & 0 deletions services/gateway/conf/route.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package conf

import (
"encoding/json"
"io/ioutil"

"github.com/sirupsen/logrus"
)

type Zone struct {
ID string
Weight int
}

type Route struct {
RouteBy string
Zones []Zone
Whitelist map[string]string
Slots []int
}

func ReadRoute(path string) (*Route, error) {
var conf struct {
RouteBy string `json:"route_by,omitempty"`
Zones []Zone `json:"zones,omitempty"`
Whitelist []struct {
Key string `json:"key,omitempty"`
Value string `json:"value,omitempty"`
} `json:"whitelist,omitempty"`
}

bts, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}

err = json.Unmarshal(bts, &conf)
if err != nil {
return nil, err
}

var rt = Route{
RouteBy: conf.RouteBy,
Zones: conf.Zones,
Whitelist: make(map[string]string, len(conf.Whitelist)),
Slots: make([]int, 0),
}
// build slots
for i, zone := range conf.Zones {
// 1.通过权重生成分片中的slots
shard := make([]int, zone.Weight)
// 2. 给当前slots设置值,指向索引i
for j := 0; j < zone.Weight; j++ {
shard[j] = i
}
// 2. 追加到Slots中
rt.Slots = append(rt.Slots, shard...)
}
for _, wl := range conf.Whitelist {
rt.Whitelist[wl.Key] = wl.Value
}
logrus.Infoln(rt)
return &rt, nil
}
23 changes: 23 additions & 0 deletions services/gateway/route.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"routeBy": "app",
"zones": [
{
"id": "zone_ali_01",
"weight": 80
},
{
"id": "zone_ali_02",
"weight": 10
},
{
"id": "zone_ali_03",
"weight": 10
}
],
"whitelist": [
{
"key": "kim",
"value": "zone_ali_03"
}
]
}
Loading