This repository has been archived by the owner on Sep 2, 2022. It is now read-only.
/
session.go
203 lines (179 loc) · 5.05 KB
/
session.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
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
194
195
196
197
198
199
200
201
202
203
// session between consumer and producer.
package goupr
import (
"github.com/couchbaselabs/go-couchbase"
mcd "github.com/dustin/gomemcached"
mc "github.com/dustin/gomemcached/client"
"io"
"log"
"net"
"sync"
"time"
)
const (
OPEN_OPAQUE uint32 = 0xDADADADA
RESPONSE_BUFFER int = 10
)
type Client struct {
bucket *couchbase.Bucket // upr client for bucket.
host string // host to connect with
name string // name of the connection
conn *mc.Client // client side socket
healthy bool // whether the connection is still valid
eventch chan *StreamEvent
// A map of `Opaque` value to Stream consumer.
streams map[uint32]*Stream
sync.RWMutex // lock to access `streams` field.
// when received message, by doRecieve(), is a response to a client request
// pass them to this channel
response chan *mcd.MCResponse
// automatically restart the connection and open streams when a disconnect
// happens in the connection
autoRestart bool
backoff time.Duration
}
// Create a new consumer instance.
func NewClient(bucket *couchbase.Bucket, eventch chan *StreamEvent) *Client {
return &Client{
bucket: bucket,
healthy: false,
eventch: eventch,
streams: make(map[uint32]*Stream),
response: make(chan *mcd.MCResponse),
backoff: 1,
}
}
// Connect to producer identified by `port` and `dest`.
// - `open` and `name` the connection.
func (client *Client) Connect(dest, name string, restart bool) (err error) {
if client.conn, err = defaultMkConn(client.bucket, dest); err != nil {
return
}
client.healthy = true
client.name = name
req := NewRequest(0, 0, OPEN_OPAQUE, 0)
flags := uint32(0x1)
go doRecieve(client) // Don't reshuffle this line
client.UprOpen(req, name, 0, flags)
client.host, client.name = dest, name
streams := make(map[uint32]*Stream)
for opaque, stream := range client.streams {
streams[opaque] = stream
}
for restart && (len(streams) > 0) { // Whether to restart the streams
log.Printf("There are %v streams to restart\n", len(streams))
time.Sleep(client.backoff * time.Second)
client.backoff <<= 1
for opaque, stream := range streams {
log.Println("Trying to reconnect", opaque)
if err = client.restartStream(stream); err != nil {
log.Println("ERROR: restarting stream :", err)
} else {
delete(streams, opaque)
}
}
}
return
}
func (client *Client) AutoRestart(what bool) {
client.autoRestart = what
}
func (client *Client) IsHealthy() bool {
return client.healthy
}
// Hang-up
func (client *Client) Close() (ret bool) {
client.autoRestart = false
if client.healthy {
client.conn.Close()
}
ret, client.healthy = client.healthy, false
return ret
}
// Go routine that will wait for new messages from producer and handle them.
func doRecieve(client *Client) {
var err error
var res *mcd.MCResponse
for {
// We generally call the message as request.
if res, err = client.conn.Receive(); err != nil {
// TODO: temporary fix. because UPR_MUTATION gives non SUCCESS status
if _, ok := err.(*mcd.MCResponse); !ok {
if client.tryError(err) {
break
}
}
}
switch res.Opcode {
case UPR_OPEN, UPR_FAILOVER_LOG, UPR_STREAM_REQ:
client.response <- res
case UPR_STREAM_END:
stream := client.getStream(res.Opaque)
if stream.autoRestart {
stream.healthy = false
go client.restartStream(stream)
} else {
client.evictStream(res.Opaque)
}
case UPR_MUTATION, UPR_DELETION:
stream := client.getStream(res.Opaque)
if res.Opaque > 0 && stream != nil {
if stream.IsHealthy() {
stream.Handlecommand(mkRequest(res))
} else {
stream.Queuecommand(mkRequest(res))
}
} else {
log.Println("ERROR: un-known opaque received %v", res)
}
case UPR_CLOSE_STREAM:
panic("UPR_CLOSE_STREAM not yet implemented")
case UPR_SNAPSHOTM, UPR_EXPIRATION, UPR_FLUSH, UPR_ADD_STREAM:
log.Printf("Opcode %v not implemented", res.Opcode)
default:
log.Println("ERROR: un-known opcode received", res)
}
}
if client.autoRestart { // auto restart this connection
log.Printf("Client %v attempting autorestart", client.name)
client.Connect(client.host, client.name, true)
}
}
func (client *Client) tryError(err error) bool {
if err == io.EOF {
log.Printf("ERROR: Connection closed by remote %v", client.name)
client.Close()
return true
} else if neterr, ok := err.(net.Error); ok && neterr.Timeout() {
log.Println("ERROR: Connection timeout ...")
client.Close()
return true
} else if err != nil {
log.Println("Unknown connection error:", err)
return true
}
return false
}
func mkRequest(res *mcd.MCResponse) *mcd.MCRequest {
req := mcd.MCRequest{
Opcode: res.Opcode,
Cas: res.Cas,
Opaque: res.Opaque,
VBucket: uint16(res.Status),
Extras: res.Extras,
Key: res.Key,
Body: res.Body,
}
return &req
}
func defaultMkConn(b *couchbase.Bucket, h string) (*mc.Client, error) {
if conn, err := mc.Connect("tcp", h); err != nil {
return nil, err
} else {
if _, err = conn.Auth(b.Name, ""); err != nil {
conn.Close()
return nil, err
}
return conn, nil
}
}