阶段性提交

This commit is contained in:
刘祥超
2020-07-22 22:18:47 +08:00
parent 219732c1df
commit fa7edac94b
19 changed files with 716 additions and 14 deletions

View File

@@ -1,13 +1,20 @@
package nodes
import (
"context"
"errors"
"github.com/TeaOSLab/EdgeNode/internal/configs"
"github.com/iwind/TeaGo/logs"
"net"
"net/http"
"sync"
)
type Listener struct {
group *configs.ServerGroup
group *configs.ServerGroup
isListening bool
listener interface{} // 监听器
locker sync.RWMutex
}
@@ -34,13 +41,13 @@ func (this *Listener) Listen() error {
}
protocol := this.group.Protocol()
switch protocol {
case configs.ProtocolHTTP:
case configs.ProtocolHTTP, configs.ProtocolHTTP4, configs.ProtocolHTTP6:
return this.listenHTTP()
case configs.ProtocolHTTPS:
case configs.ProtocolHTTPS, configs.ProtocolHTTPS4, configs.ProtocolHTTPS6:
return this.ListenHTTPS()
case configs.ProtocolTCP:
case configs.ProtocolTCP, configs.ProtocolTCP4, configs.ProtocolTCP6:
return this.listenTCP()
case configs.ProtocolTLS:
case configs.ProtocolTLS, configs.ProtocolTLS4, configs.ProtocolTLS6:
return this.listenTLS()
case configs.ProtocolUnix:
return this.listenUnix()
@@ -57,25 +64,66 @@ func (this *Listener) Close() error {
}
func (this *Listener) listenHTTP() error {
listener, err := this.createListener()
if err != nil {
return err
}
mux := http.NewServeMux()
mux.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
_, _ = writer.Write([]byte("Hello, World"))
})
server := &http.Server{
Addr: this.group.Addr(),
Handler: mux,
}
go func() {
err = server.Serve(listener)
if err != nil {
logs.Println("[LISTENER]" + err.Error())
}
}()
return nil
}
func (this *Listener) ListenHTTPS() error {
// TODO 需要实现
return nil
}
func (this *Listener) listenTCP() error {
// TODO 需要实现
return nil
}
func (this *Listener) listenTLS() error {
// TODO 需要实现
return nil
}
func (this *Listener) listenUnix() error {
// TODO 需要实现
return nil
}
func (this *Listener) listenUDP() error {
// TODO 需要实现
return nil
}
func (this *Listener) createListener() (net.Listener, error) {
listenConfig := net.ListenConfig{
Control: nil,
KeepAlive: 0,
}
switch this.group.Protocol() {
case configs.ProtocolHTTP4, configs.ProtocolHTTPS4, configs.ProtocolTLS4:
return listenConfig.Listen(context.Background(), "tcp4", this.group.Addr())
case configs.ProtocolHTTP6, configs.ProtocolHTTPS6, configs.ProtocolTLS6:
return listenConfig.Listen(context.Background(), "tcp6", this.group.Addr())
}
return listenConfig.Listen(context.Background(), "tcp", this.group.Addr())
}

View File

@@ -0,0 +1,18 @@
package nodes
import (
"github.com/TeaOSLab/EdgeNode/internal/configs"
"testing"
)
func TestListener_Listen(t *testing.T) {
listener := NewListener()
group := configs.NewServerGroup("http://:1234")
listener.Reload(group)
err := listener.Listen()
if err != nil {
t.Fatal(err)
}
}

View File

@@ -2,10 +2,12 @@ package nodes
import (
"github.com/TeaOSLab/EdgeNode/internal/configs"
"github.com/TeaOSLab/EdgeNode/internal/utils"
"github.com/iwind/TeaGo/logs"
)
var sharedNodeConfig *configs.NodeConfig = nil
var stop = make(chan bool)
type Node struct {
}
@@ -15,6 +17,7 @@ func NewNode() *Node {
}
func (this *Node) Start() {
// 读取配置
nodeConfig, err := configs.SharedNodeConfig()
if err != nil {
logs.Println("[NODE]start failed: read node config failed: " + err.Error())
@@ -22,5 +25,15 @@ func (this *Node) Start() {
}
sharedNodeConfig = nodeConfig
logs.PrintAsJSON(nodeConfig)
// 设置rlimit
_ = utils.SetRLimit(1024 * 1024)
// 启动端口
err = sharedListenerManager.Start(nodeConfig)
if err != nil {
logs.Println("[NODE]start failed: " + err.Error())
}
// hold住进程
<-stop
}

View File

@@ -0,0 +1,50 @@
package nodes
import (
"context"
"errors"
"github.com/TeaOSLab/EdgeNode/internal/configs"
"github.com/TeaOSLab/EdgeNode/internal/rpc/node"
"github.com/iwind/TeaGo/rands"
"google.golang.org/grpc"
)
type RPCClient struct {
nodeClients []node.ServiceClient
}
func NewRPCClient(apiConfig *configs.APIConfig) (*RPCClient, error) {
nodeClients := []node.ServiceClient{}
conns := []*grpc.ClientConn{}
for _, endpoint := range apiConfig.RPC.Endpoints {
conn, err := grpc.Dial(endpoint, grpc.WithInsecure())
if err != nil {
return nil, err
}
conns = append(conns, conn)
}
if len(conns) == 0 {
return nil, errors.New("[RPC]no available endpoints")
}
// node clients
for _, conn := range conns {
nodeClients = append(nodeClients, node.NewServiceClient(conn))
}
return &RPCClient{
nodeClients: nodeClients,
}, nil
}
func (this *RPCClient) NodeRPC() node.ServiceClient {
if len(this.nodeClients) > 0 {
return this.nodeClients[rands.Int(0, len(this.nodeClients)-1)]
}
return nil
}
func (this *RPCClient) Context() context.Context {
return context.Background()
}

View File

@@ -0,0 +1,30 @@
package nodes
import (
"github.com/TeaOSLab/EdgeNode/internal/configs"
"github.com/TeaOSLab/EdgeNode/internal/rpc/node"
"testing"
"time"
)
func TestRPCClient_NodeRPC(t *testing.T) {
before := time.Now()
defer func() {
t.Log(time.Since(before).Seconds()*1000, "ms")
}()
config, err := configs.LoadAPIConfig()
if err != nil {
t.Fatal(err)
}
rpc, err := NewRPCClient(config)
if err != nil {
t.Fatal(err)
}
resp, err := rpc.NodeRPC().Config(rpc.Context(), &node.ConfigRequest{
NodeId: "123456",
})
if err != nil {
t.Fatal(err)
}
t.Log(resp)
}