forked from taisho6339/gord
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
145 lines (132 loc) · 3.96 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package main
import (
"bufio"
"context"
"fmt"
"io"
"os/exec"
"path/filepath"
"strings"
//"github.com/e-dard/netbug"
"github.com/ryogrid/gord-overlay/chord"
"github.com/ryogrid/gord-overlay/core"
"github.com/ryogrid/gossip-overlay/util"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"net"
"os"
"os/signal"
"runtime"
"strconv"
"syscall"
"time"
)
var (
sigs = make(chan os.Signal, 1)
done = make(chan bool, 1)
hostAndPortBase string
existNodeHostAndPort string
)
func main() {
//overlay_setting.OVERLAY_DEBUG = true
runtime.GOMAXPROCS(10)
//go func() {
// r := http.NewServeMux()
// netbug.RegisterHandler("/debug/pprof/", r)
// if err := http.ListenAndServe(":8080", r); err != nil {
// log.Fatal(err)
// }
//}()
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-sigs
done <- true
}()
command := &cobra.Command{
Use: "gordolctl",
Short: "Run gord-overlay process and gRPC server",
Long: "Run gord-overlay process and gRPC server",
Run: func(cmd *cobra.Command, args []string) {
//_, basePort, err := net.SplitHostPort(hostAndPortBase)
host, basePort, err := net.SplitHostPort(hostAndPortBase)
if err != nil {
fmt.Println("invalid hostAndPort. err = %#v", err)
os.Exit(1)
}
basePortNum, err := strconv.Atoi(basePort)
if err != nil {
fmt.Println("invalid basePort. err = %#v", err)
os.Exit(1)
}
peers := &util.Stringset{}
if existNodeHostAndPort != "" {
peers.Set(existNodeHostAndPort)
}
// execute proxy process
exe, err := os.Executable()
if err != nil {
panic(err)
}
dirStr := filepath.Dir(exe)
launchDir := dirStr
var proxyBinaryName = launchDir + "/gossip-port-forward"
if runtime.GOOS == "windows" {
proxyBinaryName = launchDir + "/gossip-port-forward.exe"
}
startCmd := proxyBinaryName
proxyPortNumStr := strconv.Itoa(basePortNum + 3)
argsStr := "both -a " + host + " -f " + strconv.Itoa(basePortNum) + " -l " + proxyPortNumStr
fmt.Println("lauch proxy: ", startCmd+" "+argsStr)
startArgs := strings.Fields(argsStr)
proxyProc := exec.Command(startCmd, startArgs...)
proxyProc.Dir = launchDir
stdout, _ := proxyProc.StdoutPipe()
stderr, _ := proxyProc.StderrPipe()
stdoutStderr := io.MultiReader(stdout, stderr)
// print proxy process output in background
go func(childOut io.Reader) {
scanner := bufio.NewScanner(childOut)
for scanner.Scan() {
fmt.Println(scanner.Text())
}
}(stdoutStderr)
err = proxyProc.Start()
if err != nil {
panic(err)
}
time.Sleep(5 * time.Second)
proxyAddress := host + ":" + proxyPortNumStr
var (
ctx, cancel = context.WithCancel(context.Background())
localNode = chord.NewLocalNode(hostAndPortBase)
//transport = core.NewChordApiClient(localNode, olPeer, time.Second*3)
transport = core.NewChordApiClient(localNode, nil, &proxyAddress, time.Second*3*60)
process = chord.NewProcess(localNode, transport)
opts = []core.InternalServerOptionFunc{
core.WithNodeOption(hostAndPortBase),
//core.WithTimeoutConnNode(time.Second * 3),
core.WithTimeoutConnNode(time.Second * 3 * 60),
}
)
defer cancel()
if existNodeHostAndPort != "" {
opts = append(opts, core.WithProcessOptions(chord.WithExistNode(
chord.NewRemoteNode(existNodeHostAndPort, process.Transport),
)))
}
ins := core.NewChordServer(process, nil, basePort, opts...)
exs := core.NewExternalServer(process, strconv.Itoa(basePortNum+1))
go ins.Run(ctx)
go exs.Run()
<-done
ins.Shutdown()
exs.Shutdown()
process.Shutdown()
},
}
command.PersistentFlags().StringVarP(&hostAndPortBase, "hostAndPort", "l", "127.0.0.1", "host name and port to attach this process.")
command.PersistentFlags().StringVarP(&existNodeHostAndPort, "existNodeHostAndPort", "n", "", "host name of exist node in chord ring.")
if err := command.Execute(); err != nil {
log.Fatalf("err(%#v)", err)
}
}