-
Notifications
You must be signed in to change notification settings - Fork 634
Expand file tree
/
Copy pathclient.go
More file actions
193 lines (162 loc) · 4.29 KB
/
Copy pathclient.go
File metadata and controls
193 lines (162 loc) · 4.29 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package client
import (
"context"
"encoding/json"
"fmt"
"net"
"strings"
"sync"
"sync/atomic"
"yunion.io/x/log"
)
type Client struct {
conn net.Conn
enc *json.Encoder
dec *json.Decoder
seq uint64
pending map[uint64]chan *jsonRpcResponse
pendingMutex sync.Mutex
monitorHandlers map[string]MonitorHandler
monitorHandlersMutex sync.Mutex
stopCh chan struct{}
done chan struct{}
}
type MonitorHandler func(tableUpdates *json.RawMessage)
func NewClient(target string) (*Client, error) {
parts := strings.SplitN(target, ":", 2)
if len(parts) != 2 {
return nil, fmt.Errorf("invalid target %s", target)
}
proto := parts[0]
addr := parts[1]
conn, err := net.Dial(proto, addr)
if err != nil {
return nil, err
}
c := &Client{
conn: conn,
enc: json.NewEncoder(conn),
dec: json.NewDecoder(conn),
pending: make(map[uint64]chan *jsonRpcResponse),
monitorHandlers: make(map[string]MonitorHandler),
stopCh: make(chan struct{}),
done: make(chan struct{}),
}
go c.readLoop()
return c, nil
}
func (c *Client) Close() error {
close(c.stopCh)
c.conn.Close()
<-c.done
return nil
}
func (c *Client) readLoop() {
defer close(c.done)
for {
select {
case <-c.stopCh:
return
default:
}
var rawMsg json.RawMessage
if err := c.dec.Decode(&rawMsg); err != nil {
if strings.Contains(err.Error(), "use of closed network connection") {
return
}
log.Errorf("decode error: %v", err)
// try to recover or exit? for now exit
return
}
// Determine if it is a request, response or notification
// OVSDB server sends responses and notifications
var resp jsonRpcResponse
if err := json.Unmarshal(rawMsg, &resp); err == nil && resp.Id != nil {
// It's a response
idVal, ok := resp.Id.(float64) // json numbers are floats
if !ok {
// check if it's string or other type?
// we use uint64 for id, so we expect number
// let's handle generic id
}
c.pendingMutex.Lock()
var ch chan *jsonRpcResponse
// Find the channel
// Simple match for now, need robust ID handling
// We'll assume ID is integer
id := uint64(idVal)
if ch, ok = c.pending[id]; ok {
delete(c.pending, id)
}
c.pendingMutex.Unlock()
if ch != nil {
ch <- &resp
}
continue
}
var notif jsonRpcNotification
if err := json.Unmarshal(rawMsg, ¬if); err == nil && notif.Method == "update" {
// handle update
var params []json.RawMessage
if err := json.Unmarshal(*notif.Params, ¶ms); err != nil {
log.Errorf("failed to unmarshal update params: %v", err)
continue
}
if len(params) >= 2 {
var monId string
if err := json.Unmarshal(params[0], &monId); err != nil {
// maybe it is not string? RFC says json-value
// We will use string as monitor ID
}
c.monitorHandlersMutex.Lock()
handler, ok := c.monitorHandlers[monId]
c.monitorHandlersMutex.Unlock()
if ok {
handler(¶ms[1])
}
}
continue
}
}
}
func (c *Client) Call(ctx context.Context, method string, params []interface{}) (*json.RawMessage, error) {
id := atomic.AddUint64(&c.seq, 1)
req := jsonRpcRequest{
Method: method,
Params: params,
Id: id,
}
ch := make(chan *jsonRpcResponse, 1)
c.pendingMutex.Lock()
c.pending[id] = ch
c.pendingMutex.Unlock()
defer func() {
c.pendingMutex.Lock()
delete(c.pending, id)
c.pendingMutex.Unlock()
}()
if err := c.enc.Encode(req); err != nil {
return nil, err
}
select {
case resp := <-ch:
if resp.Error != nil {
return nil, fmt.Errorf("rpc error: %v", resp.Error)
}
return resp.Result, nil
case <-ctx.Done():
return nil, ctx.Err()
}
}
func (c *Client) Monitor(ctx context.Context, dbName string, monitorId string, monitorRequests interface{}, handler MonitorHandler) (*json.RawMessage, error) {
c.monitorHandlersMutex.Lock()
c.monitorHandlers[monitorId] = handler
c.monitorHandlersMutex.Unlock()
return c.Call(ctx, "monitor", []interface{}{dbName, monitorId, monitorRequests})
}
func (c *Client) Transact(ctx context.Context, dbName string, operations []interface{}) (*json.RawMessage, error) {
params := make([]interface{}, 0, 1+len(operations))
params = append(params, dbName)
params = append(params, operations...)
return c.Call(ctx, "transact", params)
}