| 1 |
package main |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"flag" |
| 6 |
"log" |
| 7 |
"net/http" |
| 8 |
"os" |
| 9 |
"os/signal" |
| 10 |
"strconv" |
| 11 |
"syscall" |
| 12 |
"time" |
| 13 |
|
| 14 |
_ "net/http/pprof" |
| 15 |
|
| 16 |
"github.com/centrifugal/centrifuge" |
| 17 |
"github.com/centrifugal/centrifuge/_examples/custom_engine_tarantool/tntengine" |
| 18 |
) |
| 19 |
|
| 20 |
var ( |
| 21 |
port = flag.Int("port", 8000, "Port to bind app to") |
| 22 |
sharded = flag.Bool("sharded", false, "Start sharded example") |
| 23 |
ha = flag.Bool("ha", false, "Start high availability example") |
| 24 |
raft = flag.Bool("raft", false, "Using Raft-based replication") |
| 25 |
user = flag.String("user", "guest", "Connection user") |
| 26 |
password = flag.String("password", "", "Connection password") |
| 27 |
) |
| 28 |
|
| 29 |
func handleLog(e centrifuge.LogEntry) { |
| 30 |
log.Printf("[centrifuge] %s: %v", e.Message, e.Fields) |
| 31 |
} |
| 32 |
|
| 33 |
func authMiddleware(h http.Handler) http.Handler { |
| 34 |
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 35 |
ctx := r.Context() |
| 36 |
ctx = centrifuge.SetCredentials(ctx, ¢rifuge.Credentials{ |
| 37 |
UserID: "42", |
| 38 |
Info: []byte(`{"name": "Alexander"}`), |
| 39 |
}) |
| 40 |
r = r.WithContext(ctx) |
| 41 |
h.ServeHTTP(w, r) |
| 42 |
}) |
| 43 |
} |
| 44 |
|
| 45 |
func waitExitSignal(n *centrifuge.Node) { |
| 46 |
sigCh := make(chan os.Signal, 1) |
| 47 |
done := make(chan bool, 1) |
| 48 |
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM) |
| 49 |
go func() { |
| 50 |
<-sigCh |
| 51 |
_ = n.Shutdown(context.Background()) |
| 52 |
done <- true |
| 53 |
}() |
| 54 |
<-done |
| 55 |
} |
| 56 |
|
| 57 |
func main() { |
| 58 |
flag.Parse() |
| 59 |
|
| 60 |
cfg := centrifuge.DefaultConfig |
| 61 |
cfg.LogLevel = centrifuge.LogLevelDebug |
| 62 |
cfg.LogHandler = handleLog |
| 63 |
|
| 64 |
node, _ := centrifuge.New(cfg) |
| 65 |
|
| 66 |
node.OnConnect(func(client *centrifuge.Client) { |
| 67 |
transport := client.Transport() |
| 68 |
log.Printf("user %s connected via %s with protocol: %s", client.UserID(), transport.Name(), transport.Protocol()) |
| 69 |
|
| 70 |
client.OnSubscribe(func(e centrifuge.SubscribeEvent, cb centrifuge.SubscribeCallback) { |
| 71 |
log.Printf("user %s subscribes on %s", client.UserID(), e.Channel) |
| 72 |
cb(centrifuge.SubscribeReply{ |
| 73 |
Options: centrifuge.SubscribeOptions{ |
| 74 |
Presence: true, |
| 75 |
JoinLeave: true, |
| 76 |
Recover: true, |
| 77 |
}, |
| 78 |
}, nil) |
| 79 |
}) |
| 80 |
|
| 81 |
client.OnUnsubscribe(func(e centrifuge.UnsubscribeEvent) { |
| 82 |
log.Printf("user %s unsubscribed from %s", client.UserID(), e.Channel) |
| 83 |
}) |
| 84 |
|
| 85 |
client.OnPublish(func(e centrifuge.PublishEvent, cb centrifuge.PublishCallback) { |
| 86 |
log.Printf("user %s publishes into channel %s: %s", client.UserID(), e.Channel, string(e.Data)) |
| 87 |
cb(centrifuge.PublishReply{ |
| 88 |
Options: centrifuge.PublishOptions{ |
| 89 |
HistorySize: 10, |
| 90 |
HistoryTTL: 10 * time.Minute, |
| 91 |
}, |
| 92 |
}, nil) |
| 93 |
}) |
| 94 |
|
| 95 |
client.OnPresence(func(e centrifuge.PresenceEvent, cb centrifuge.PresenceCallback) { |
| 96 |
log.Printf("user %s calls presence on %s", client.UserID(), e.Channel) |
| 97 |
if !client.IsSubscribed(e.Channel) { |
| 98 |
cb(centrifuge.PresenceReply{}, centrifuge.ErrorPermissionDenied) |
| 99 |
return |
| 100 |
} |
| 101 |
cb(centrifuge.PresenceReply{}, nil) |
| 102 |
}) |
| 103 |
|
| 104 |
client.OnPresenceStats(func(e centrifuge.PresenceStatsEvent, cb centrifuge.PresenceStatsCallback) { |
| 105 |
log.Printf("user %s calls presence stats on %s", client.UserID(), e.Channel) |
| 106 |
if !client.IsSubscribed(e.Channel) { |
| 107 |
cb(centrifuge.PresenceStatsReply{}, centrifuge.ErrorPermissionDenied) |
| 108 |
return |
| 109 |
} |
| 110 |
cb(centrifuge.PresenceStatsReply{}, nil) |
| 111 |
}) |
| 112 |
|
| 113 |
client.OnDisconnect(func(e centrifuge.DisconnectEvent) { |
| 114 |
log.Printf("user %s disconnected, disconnect: %s", client.UserID(), e.Disconnect) |
| 115 |
}) |
| 116 |
}) |
| 117 |
|
| 118 |
// Single Tarantool. |
| 119 |
mode := tntengine.ConnectionModeSingleInstance |
| 120 |
shardAddresses := [][]string{ |
| 121 |
{"127.0.0.1:3301"}, |
| 122 |
} |
| 123 |
|
| 124 |
if *ha { |
| 125 |
if *raft { |
| 126 |
// Single Tarantool RS with automatic leader election with Raft (Tarantool >= 2.7.0). |
| 127 |
shardAddresses = [][]string{ |
| 128 |
{"127.0.0.1:3301", "127.0.0.1:3302", "127.0.0.1:3303"}, |
| 129 |
} |
| 130 |
mode = tntengine.ConnectionModeLeaderFollowerRaft |
| 131 |
} else { |
| 132 |
// Single Tarantool RS with automatic leader election (ex. in Cartridge). |
| 133 |
shardAddresses = [][]string{ |
| 134 |
{"127.0.0.1:3301", "127.0.0.1:3302"}, |
| 135 |
} |
| 136 |
mode = tntengine.ConnectionModeLeaderFollower |
| 137 |
} |
| 138 |
} else if *sharded { |
| 139 |
// Client-side sharding between two Tarantool instances (without HA). |
| 140 |
shardAddresses = [][]string{ |
| 141 |
{"127.0.0.1:3301"}, |
| 142 |
{"127.0.0.1:3302"}, |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
var shards []*tntengine.Shard |
| 147 |
for _, addresses := range shardAddresses { |
| 148 |
shard, err := tntengine.NewShard(tntengine.ShardConfig{ |
| 149 |
Addresses: addresses, |
| 150 |
User: *user, |
| 151 |
Password: *password, |
| 152 |
ConnectionMode: mode, |
| 153 |
}) |
| 154 |
if err != nil { |
| 155 |
log.Fatal(err) |
| 156 |
} |
| 157 |
shards = append(shards, shard) |
| 158 |
} |
| 159 |
|
| 160 |
broker, err := tntengine.NewBroker(node, tntengine.BrokerConfig{ |
| 161 |
UsePolling: false, |
| 162 |
Shards: shards, |
| 163 |
}) |
| 164 |
if err != nil { |
| 165 |
log.Fatal(err) |
| 166 |
} |
| 167 |
node.SetBroker(broker) |
| 168 |
|
| 169 |
presenceManager, err := tntengine.NewPresenceManager(node, tntengine.PresenceManagerConfig{ |
| 170 |
Shards: shards, |
| 171 |
}) |
| 172 |
if err != nil { |
| 173 |
log.Fatal(err) |
| 174 |
} |
| 175 |
node.SetPresenceManager(presenceManager) |
| 176 |
|
| 177 |
if err := node.Run(); err != nil { |
| 178 |
log.Fatal(err) |
| 179 |
} |
| 180 |
|
| 181 |
http.Handle("/connection/websocket", authMiddleware(centrifuge.NewWebsocketHandler(node, centrifuge.WebsocketConfig{}))) |
| 182 |
http.Handle("/", http.FileServer(http.Dir("./"))) |
| 183 |
|
| 184 |
go func() { |
| 185 |
if err := http.ListenAndServe(":"+strconv.Itoa(*port), nil); err != nil { |
| 186 |
log.Fatal(err) |
| 187 |
} |
| 188 |
}() |
| 189 |
|
| 190 |
waitExitSignal(node) |
| 191 |
log.Println("bye!") |
| 192 |
} |