forked from xfali/gache
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
608 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Copyright (C) 2019, Xiongfa Li. | ||
// All right reserved. | ||
// @author xiongfa.li | ||
// @version V1.0 | ||
// Description: | ||
|
||
package gossip | ||
|
||
import ( | ||
"github.com/hashicorp/memberlist" | ||
"log" | ||
"sync" | ||
) | ||
|
||
type handle func(meta []byte) | ||
|
||
type NodeDelegate struct { | ||
mu sync.Mutex | ||
Enabled bool | ||
JoinFunc handle | ||
LeaveFunc handle | ||
UpdateFunc handle | ||
} | ||
|
||
func defaultJoin(meta []byte) { | ||
log.Printf("defaultJoin meta: %s\n", string(meta)) | ||
} | ||
|
||
func defaultLeave(meta []byte) { | ||
log.Printf("defaultLeave meta: %s\n", string(meta)) | ||
} | ||
|
||
func defaultUpdate(meta []byte) { | ||
log.Printf("defaultUpdate meta: %s\n", string(meta)) | ||
} | ||
|
||
func DefaultNodeDelegate() *NodeDelegate { | ||
return &NodeDelegate{ | ||
Enabled: true, | ||
JoinFunc: defaultJoin, | ||
LeaveFunc: defaultLeave, | ||
UpdateFunc: defaultUpdate, | ||
} | ||
} | ||
|
||
func (d *NodeDelegate) NotifyJoin(n *memberlist.Node) { | ||
d.mu.Lock() | ||
defer d.mu.Unlock() | ||
if d.Enabled { | ||
d.JoinFunc(n.Meta) | ||
} | ||
} | ||
|
||
// NotifyLeave is invoked when a node is detected to have left. | ||
// The Node argument must not be modified. | ||
func (d *NodeDelegate) NotifyLeave(n *memberlist.Node) { | ||
d.mu.Lock() | ||
defer d.mu.Unlock() | ||
if d.Enabled { | ||
d.LeaveFunc(n.Meta) | ||
} | ||
} | ||
|
||
// NotifyUpdate is invoked when a node is detected to have | ||
// updated, usually involving the meta data. The Node argument | ||
// must not be modified. | ||
func (d *NodeDelegate) NotifyUpdate(n *memberlist.Node) { | ||
d.mu.Lock() | ||
defer d.mu.Unlock() | ||
if d.Enabled { | ||
d.UpdateFunc(n.Meta) | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// Copyright (C) 2019, Xiongfa Li. | ||
// All right reserved. | ||
// @author xiongfa.li | ||
// @version V1.0 | ||
// Description: | ||
|
||
package gossip | ||
|
||
import ( | ||
"gache/cluster" | ||
"gache/config" | ||
"github.com/hashicorp/memberlist" | ||
"os" | ||
"strconv" | ||
"strings" | ||
"time" | ||
) | ||
|
||
type members memberlist.Memberlist | ||
|
||
type Cluster interface { | ||
LocalAddr() string | ||
UpdateLocal(meta []byte) error | ||
UpdateAndWait(meta []byte, timeout time.Duration) error | ||
Close() error | ||
} | ||
|
||
func Startup(conf *config.Config, delegate *NodeDelegate) (Cluster, error) { | ||
hostname, _ := os.Hostname() | ||
config := memberlist.DefaultLocalConfig() | ||
config.Name = hostname + "-" + strconv.Itoa(conf.ClusterPort) | ||
// config := memberlist.DefaultLocalConfig() | ||
config.BindPort = conf.ClusterPort | ||
config.AdvertisePort = conf.ClusterPort | ||
config.Events = delegate | ||
|
||
list, err := memberlist.Create(config) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
_, _, sloterr := cluster.GetSlots(conf.ClusterSlot) | ||
if sloterr != nil { | ||
list.Shutdown() | ||
return nil, sloterr | ||
} | ||
|
||
memList := strings.Split(conf.ClusterMemebers, ",") | ||
var validMembers []string | ||
for _, v := range memList { | ||
mem := strings.TrimSpace(v) | ||
if mem != "" { | ||
validMembers = append(validMembers, mem) | ||
} | ||
} | ||
if len(validMembers) > 0 { | ||
list.Join(validMembers) | ||
} | ||
|
||
return (*members)(list), nil | ||
} | ||
|
||
func (c *members)LocalAddr() string { | ||
return (*memberlist.Memberlist)(c).LocalNode().Address() | ||
} | ||
|
||
func (c *members) UpdateLocal(meta []byte) error { | ||
(*memberlist.Memberlist)(c).LocalNode().Meta = meta | ||
return nil | ||
} | ||
|
||
func (c *members) UpdateAndWait(meta []byte, timeout time.Duration) error { | ||
(*memberlist.Memberlist)(c).LocalNode().Meta = meta | ||
return (*memberlist.Memberlist)(c).UpdateNode(timeout) | ||
} | ||
|
||
func (c *members) Close() error { | ||
return (*memberlist.Memberlist)(c).Shutdown() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.