forked from ccfos/nightingale
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrpc.go
60 lines (46 loc) · 1010 Bytes
/
rpc.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
package rpc
import (
"bufio"
"fmt"
"io"
"net"
"net/rpc"
"os"
"reflect"
"time"
"github.com/toolkits/pkg/logger"
"github.com/ugorji/go/codec"
"github.com/didi/nightingale/v5/config"
)
type Server int
func Start() {
go serve()
}
func serve() {
addr := config.Config.RPC.Listen
server := rpc.NewServer()
server.Register(new(Server))
l, err := net.Listen("tcp", addr)
if err != nil {
fmt.Printf("fail to listen on: %s, error: %v\n", addr, err)
os.Exit(1)
}
fmt.Println("rpc.listening:", addr)
var mh codec.MsgpackHandle
mh.MapType = reflect.TypeOf(map[string]interface{}(nil))
duration := time.Duration(100) * time.Millisecond
for {
conn, err := l.Accept()
if err != nil {
logger.Warningf("listener accept error: %v", err)
time.Sleep(duration)
continue
}
var bufconn = struct {
io.Closer
*bufio.Reader
*bufio.Writer
}{conn, bufio.NewReader(conn), bufio.NewWriter(conn)}
go server.ServeCodec(codec.MsgpackSpecRpc.ServerCodec(bufconn, &mh))
}
}