Skip to content

Commit

Permalink
*: add label to cluster member
Browse files Browse the repository at this point in the history
Signed-off-by: Lonng <[email protected]>
  • Loading branch information
lonng committed Jun 30, 2019
1 parent ebd3230 commit 5dcf564
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 41 deletions.
4 changes: 4 additions & 0 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/lonng/nano/component"
"github.com/lonng/nano/internal/env"
"github.com/lonng/nano/internal/log"
"github.com/lonng/nano/internal/runtime"
"github.com/lonng/nano/scheduler"
)

Expand All @@ -50,6 +51,7 @@ func run(addr string, isWs bool, certificate string, key string, opts ...Option)
}

node := &cluster.Node{
Label: opt.label,
IsMaster: opt.isMaster,
AdvertiseAddr: opt.advertiseAddr,
MemberAddr: opt.memberAddr,
Expand All @@ -64,6 +66,7 @@ func run(addr string, isWs bool, certificate string, key string, opts ...Option)
if err != nil {
log.Fatalf("Node startup failed: %v", err)
}
runtime.CurrentNode = node

log.Println(fmt.Sprintf("Nano server %s started, listen at %s", app.name, addr))
scheduler.Sched()
Expand All @@ -80,6 +83,7 @@ func run(addr string, isWs bool, certificate string, key string, opts ...Option)
log.Println("Nano server is stopping...")

node.Shutdown()
runtime.CurrentNode = nil
scheduler.Close()
atomic.StoreInt32(&running, 0)
}
72 changes: 36 additions & 36 deletions cluster/clusterpb/cluster.pb.go

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

2 changes: 1 addition & 1 deletion cluster/clusterpb/proto/cluster.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ syntax = "proto3";
package clusterpb;

message MemberInfo {
string memberType = 1;
string label = 1;
string memberAddr = 2;
repeated string services = 3;
}
Expand Down
10 changes: 6 additions & 4 deletions cluster/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ import (
// All services will register to cluster and messages will be forwarded to the node
// which provides respective service
type Node struct {
IsMaster bool
AdvertiseAddr string
Label string
IsMaster bool // indicate if the current node is master
IsGate bool // indicate if the current node is gate
AdvertiseAddr string // master server service address
MemberAddr string
ServerAddr string
Components *component.Components
Expand Down Expand Up @@ -140,7 +142,7 @@ func (n *Node) initMaster() error {
member := &Member{
isMaster: true,
memberInfo: &clusterpb.MemberInfo{
MemberType: "master",
Label: n.Label,
MemberAddr: n.AdvertiseAddr,
Services: n.handler.LocalService(),
},
Expand Down Expand Up @@ -178,7 +180,7 @@ func (n *Node) initMember() error {
client := clusterpb.NewMasterClient(conns.Get())
request := &clusterpb.RegisterRequest{
MemberInfo: &clusterpb.MemberInfo{
MemberType: "member",
Label: n.Label,
MemberAddr: n.MemberAddr,
Services: n.handler.LocalService(),
},
Expand Down
20 changes: 20 additions & 0 deletions internal/log/logger.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
// Copyright (c) nano Authors. All Rights Reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

package log

import (
Expand Down
25 changes: 25 additions & 0 deletions internal/runtime/runtime.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) nano Authors. All Rights Reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

package runtime

import "github.com/lonng/nano/cluster"

var CurrentNode *cluster.Node
7 changes: 7 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type (
memberAddr string
isMaster bool
components *component.Components
label string
}

Option func(*options)
Expand Down Expand Up @@ -121,3 +122,9 @@ func WithSerializer(serializer serialize.Serializer) Option {
env.Serializer = serializer
}
}

func WithLabel(label string) Option {
return func(opt *options) {
opt.label = label
}
}

0 comments on commit 5dcf564

Please sign in to comment.