Skip to content

Commit

Permalink
fix for tinode#230
Browse files Browse the repository at this point in the history
  • Loading branch information
or-else committed Apr 8, 2019
1 parent 277e99e commit 86e812b
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 25 deletions.
46 changes: 26 additions & 20 deletions server/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ type ClusterReq struct {
Pkt *ClientComMessage

// Root user may send messages on behalf of other users.
OnBahalfOf string
OnBehalfOf string
// AuthLevel of the user specified by root.
AuthLvl int

Expand Down Expand Up @@ -319,7 +319,7 @@ func (c *Cluster) Master(msg *ClusterReq, rejected *bool) error {
sess.platf = msg.Sess.Platform

// Dispatch remote message to a local session.
msg.Pkt.from = msg.OnBahalfOf
msg.Pkt.from = msg.OnBehalfOf
msg.Pkt.authLvl = msg.AuthLvl
sess.dispatch(msg.Pkt)
} else {
Expand Down Expand Up @@ -398,24 +398,30 @@ func (c *Cluster) routeToTopic(msg *ClientComMessage, topic string, sess *Sessio
}
sess.nodes[n.name] = true

return n.forward(
&ClusterReq{
Node: c.thisNodeName,
Signature: c.ring.Signature(),
Pkt: msg,
OnBahalfOf: msg.from,
AuthLvl: msg.authLvl,
RcptTo: topic,
Sess: &ClusterSess{
Uid: sess.uid,
AuthLvl: sess.authLvl,
RemoteAddr: sess.remoteAddr,
UserAgent: sess.userAgent,
Ver: sess.ver,
Lang: sess.lang,
DeviceID: sess.deviceID,
Platform: sess.platf,
Sid: sess.sid}})
req := &ClusterReq{
Node: c.thisNodeName,
Signature: c.ring.Signature(),
Pkt: msg,
RcptTo: topic,
Sess: &ClusterSess{
Uid: sess.uid,
AuthLvl: sess.authLvl,
RemoteAddr: sess.remoteAddr,
UserAgent: sess.userAgent,
Ver: sess.ver,
Lang: sess.lang,
DeviceID: sess.deviceID,
Platform: sess.platf,
Sid: sess.sid}}

if sess.authLvl == auth.LevelRoot {
// Assign these values only when the sender is root
req.OnBehalfOf = msg.from
req.AuthLvl = msg.authLvl
}

return n.forward(req)

}

// Session terminated at origin. Inform remote Master nodes that the session is gone.
Expand Down
39 changes: 35 additions & 4 deletions server/run-cluster.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,38 @@
#!/bin/bash

# Start test cluster on one host. This is provided just as an example.
# Start/stop test cluster on localhost. This is NOT a production script. Use it for reference only.

./server -config=./tinode.conf -cluster_self=one -listen=:6060 &
./server -config=./tinode.conf -cluster_self=two -listen=:6061 &
./server -config=./tinode.conf -cluster_self=three -listen=:6062 &
# Names of cluster nodes
node_names=( one two three )
# Port where the first node will listen for client connections over http
base_http_port=6080
# Port where the first node will listen for gRPC connections.
base_grpc_port=6090

case "$1" in
start)
echo 'Running cluster on localhost, ports 6080-6082'

http_port=$base_http_port
grpc_port=$base_grpc_port
for node_name in "${node_names[@]}"
do
./server -config=./tinode.conf -cluster_self=$node_name -listen=:${http_port} -grpc_listen=:${grpc_port} &
# /var/tmp/ does not requre root access
echo $!> "/var/tmp/tinode-${node_name}.pid"
http_port=$((http_port+1))
grpc_port=$((grpc_port+1))
done
;;
stop)
echo 'Stopping cluster'

for node_name in "${node_names[@]}"
do
kill `cat /var/tmp/tinode-${node_name}.pid`
rm "/var/tmp/tinode-${node_name}.pid"
done
;;
*)
echo $"Usage: $0 {start|stop}"
esac
3 changes: 2 additions & 1 deletion server/tinode.conf
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,8 @@

// List of available nodes.
"nodes": [
// Name and TCP address of every node in the cluster.
// Name and TCP address of every node in the cluster. The ports 12001..12003
// are cluster communication ports. They don't need to be exposed to clients.
{"name": "one", "addr":"localhost:12001"},
{"name": "two", "addr":"localhost:12002"},
{"name": "three", "addr":"localhost:12003"}
Expand Down

0 comments on commit 86e812b

Please sign in to comment.