/
rpcsvr.go
80 lines (65 loc) · 1.34 KB
/
rpcsvr.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
package main
import (
"fmt"
"log"
"net"
"net/rpc"
"net/rpc/jsonrpc"
)
// RPC
type MetricValue struct {
Endpoint string `json:"endpoint"`
Metric string `json:"metric"`
Value interface{} `json:"value"`
Step int64 `json:"step"`
Type string `json:"counterType"`
Tags string `json:"tags"`
Timestamp int64 `json:"timestamp"`
}
type TransferResponse struct {
Message string
Total int
Invalid int
Latency int64
}
func (this *TransferResponse) String() string {
return fmt.Sprintf(
"<Total=%v, Invalid:%v, Latency=%vms, Message:%s>",
this.Total,
this.Invalid,
this.Latency,
this.Message,
)
}
type Transfer int
func (t *Transfer) Update(args []*MetricValue, reply *TransferResponse) error {
reply.Invalid = 0
reply.Latency = 0
reply.Total = len(args)
reply.Message = "ok"
for _, mv := range args {
log.Printf("%+v\n", *mv)
}
return nil
}
// MAIN
func main() {
l, err := net.Listen("tcp", "127.0.0.1:5090")
defer l.Close()
if err != nil {
log.Fatal(err)
}
log.Print("listening:", l.Addr())
trans := new(Transfer)
rpc.Register(trans)
for {
log.Print("waiting for connections...")
c, err := l.Accept()
if err != nil {
log.Printf("accept error: %s", c)
continue
}
log.Printf("connection started: %v", c.RemoteAddr())
go jsonrpc.ServeConn(c)
}
}