本文整理汇总了Golang中github.com/couchbase/gomemcached.MCRequest类的典型用法代码示例。如果您正苦于以下问题:Golang MCRequest类的具体用法?Golang MCRequest怎么用?Golang MCRequest使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了MCRequest类的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: doReceive
func doReceive(
uprconn *uprConnection, host string, msgch chan msgT, killSwitch chan bool) {
var hdr [mcd.HDR_LEN]byte
var msg msgT
var pkt mcd.MCRequest
var err error
mcconn := uprconn.conn.Hijack()
loop:
for {
if _, err = pkt.Receive(mcconn, hdr[:]); err != nil {
msg = msgT{uprconn: uprconn, err: err}
} else {
msg = msgT{uprconn: uprconn, pkt: pkt}
}
select {
case msgch <- msg:
case <-killSwitch:
break loop
}
}
return
}
示例2: runFeed
// Internal goroutine that reads from the socket and writes events to
// the channel
func (mc *Client) runFeed(ch chan TapEvent, feed *TapFeed) {
defer close(ch)
var headerBuf [gomemcached.HDR_LEN]byte
loop:
for {
// Read the next request from the server.
//
// (Can't call mc.Receive() because it reads a
// _response_ not a request.)
var pkt gomemcached.MCRequest
n, err := pkt.Receive(mc.conn, headerBuf[:])
if TapRecvHook != nil {
TapRecvHook(&pkt, n, err)
}
if err != nil {
if err != io.EOF {
feed.Error = err
}
break loop
}
//log.Printf("** TapFeed received %#v : %q", pkt, pkt.Body)
if pkt.Opcode == gomemcached.TAP_CONNECT {
// This is not an event from the server; it's
// an error response to my connect request.
feed.Error = fmt.Errorf("tap connection failed: %s", pkt.Body)
break loop
}
event := makeTapEvent(pkt)
if event != nil {
if event.Opcode == tapEndStream {
break loop
}
select {
case ch <- *event:
case <-feed.closer:
break loop
}
}
if len(pkt.Extras) >= 4 {
reqFlags := binary.BigEndian.Uint16(pkt.Extras[2:])
if reqFlags&gomemcached.TAP_ACK != 0 {
if _, err := mc.sendAck(&pkt); err != nil {
feed.Error = err
break loop
}
}
}
}
if err := mc.Close(); err != nil {
log.Printf("Error closing memcached client: %v", err)
}
}
示例3: transmitRequest
func transmitRequest(o io.Writer, req *gomemcached.MCRequest) (int, error) {
if o == nil {
return 0, errNoConn
}
n, err := req.Transmit(o)
if TransmitHook != nil {
TransmitHook(req, n, err)
}
return n, err
}
示例4: TestTransmitReq
func TestTransmitReq(t *testing.T) {
b := bytes.NewBuffer([]byte{})
buf := bufio.NewWriter(b)
req := gomemcached.MCRequest{
Opcode: gomemcached.SET,
Cas: 938424885,
Opaque: 7242,
VBucket: 824,
Extras: []byte{},
Key: []byte("somekey"),
Body: []byte("somevalue"),
}
// Verify nil transmit is OK
_, err := transmitRequest(nil, &req)
if err != errNoConn {
t.Errorf("Expected errNoConn with no conn, got %v", err)
}
_, err = transmitRequest(buf, &req)
if err != nil {
t.Fatalf("Error transmitting request: %v", err)
}
buf.Flush()
expected := []byte{
gomemcached.REQ_MAGIC, byte(gomemcached.SET),
0x0, 0x7, // length of key
0x0, // extra length
0x0, // reserved
0x3, 0x38, // vbucket
0x0, 0x0, 0x0, 0x10, // Length of value
0x0, 0x0, 0x1c, 0x4a, // opaque
0x0, 0x0, 0x0, 0x0, 0x37, 0xef, 0x3a, 0x35, // CAS
's', 'o', 'm', 'e', 'k', 'e', 'y',
's', 'o', 'm', 'e', 'v', 'a', 'l', 'u', 'e'}
if len(b.Bytes()) != req.Size() {
t.Fatalf("Expected %v bytes, got %v", req.Size(),
len(b.Bytes()))
}
if !reflect.DeepEqual(b.Bytes(), expected) {
t.Fatalf("Expected:\n%#v\n -- got -- \n%#v",
expected, b.Bytes())
}
}
示例5: BenchmarkTransmitReqNull
func BenchmarkTransmitReqNull(b *testing.B) {
req := gomemcached.MCRequest{
Opcode: gomemcached.SET,
Cas: 938424885,
Opaque: 7242,
VBucket: 824,
Extras: []byte{},
Key: []byte("somekey"),
Body: []byte("somevalue"),
}
b.SetBytes(int64(req.Size()))
for i := 0; i < b.N; i++ {
_, err := transmitRequest(ioutil.Discard, &req)
if err != nil {
b.Fatalf("Error transmitting request: %v", err)
}
}
}
示例6: TestTransmitReqWithExtMeta
func TestTransmitReqWithExtMeta(t *testing.T) {
// test data for extended metadata
ExtMetaStr := "extmeta"
b := bytes.NewBuffer([]byte{})
buf := bufio.NewWriter(b)
req := gomemcached.MCRequest{
Opcode: gomemcached.SET,
Cas: 938424885,
Opaque: 7242,
VBucket: 824,
Key: []byte("somekey"),
Body: []byte("somevalue"),
ExtMeta: []byte(ExtMetaStr),
}
// add length of extended metadata to the corresponding bytes in Extras
req.Extras = make([]byte, 30)
binary.BigEndian.PutUint32(req.Extras[28:30], len(ExtMetaStr))
// Verify nil transmit is OK
_, err := transmitRequest(nil, &req)
if err != errNoConn {
t.Errorf("Expected errNoConn with no conn, got %v", err)
}
_, err = transmitRequest(buf, &req)
if err != nil {
t.Fatalf("Error transmitting request: %v", err)
}
buf.Flush()
expected := []byte{
gomemcached.REQ_MAGIC, byte(gomemcached.SET),
0x0, 0x7, // length of key
0x1e, // extra length = 30 = 0x1e
0x0, // reserved
0x3, 0x38, // vbucket
0x0, 0x0, 0x0, 0x35, // Length of value = 7(key) + 9(value) + 30(extras) + 7(extmeta) = 53 = 0x35
0x0, 0x0, 0x1c, 0x4a, // opaque
0x0, 0x0, 0x0, 0x0, 0x37, 0xef, 0x3a, 0x35, // CAS
's', 'o', 'm', 'e', 'k', 'e', 'y',
's', 'o', 'm', 'e', 'v', 'a', 'l', 'u', 'e',
'e', 'x', 't', 'm', 'e', 't', 'a'}
if len(b.Bytes()) != req.Size() {
t.Fatalf("Expected %v bytes, got %v", req.Size(),
len(b.Bytes()))
}
if !reflect.DeepEqual(b.Bytes(), expected) {
t.Fatalf("Expected:\n%#v\n -- got -- \n%#v",
expected, b.Bytes())
}
}
示例7: BenchmarkTransmitReqLarge
func BenchmarkTransmitReqLarge(b *testing.B) {
bout := bytes.NewBuffer([]byte{})
req := gomemcached.MCRequest{
Opcode: gomemcached.SET,
Cas: 938424885,
Opaque: 7242,
VBucket: 824,
Extras: []byte{},
Key: []byte("somekey"),
Body: make([]byte, 24*1024),
}
b.SetBytes(int64(req.Size()))
for i := 0; i < b.N; i++ {
bout.Reset()
buf := bufio.NewWriterSize(bout, req.Size()*2)
_, err := transmitRequest(buf, &req)
if err != nil {
b.Fatalf("Error transmitting request: %v", err)
}
}
}
示例8: worker
// Connect once to the server and work the UPR stream. If anything
// goes wrong, return our level of progress in order to let our caller
// control any potential retries.
func (d *bucketDataSource) worker(server string, workerCh chan []uint16) int {
atomic.AddUint64(&d.stats.TotWorkerBody, 1)
if !d.isRunning() {
return -1
}
atomic.AddUint64(&d.stats.TotWorkerConnect, 1)
connect := d.options.Connect
if connect == nil {
connect = memcached.Connect
}
client, err := connect("tcp", server)
if err != nil {
atomic.AddUint64(&d.stats.TotWorkerConnectErr, 1)
d.receiver.OnError(fmt.Errorf("worker connect, server: %s, err: %v",
server, err))
return 0
}
defer client.Close()
atomic.AddUint64(&d.stats.TotWorkerConnectOk, 1)
if d.auth != nil {
var user, pswd string
var adminCred bool
if auth, ok := d.auth.(couchbase.AuthWithSaslHandler); ok {
user, pswd = auth.GetSaslCredentials()
adminCred = true
} else {
user, pswd, _ = d.auth.GetCredentials()
}
if user != "" {
atomic.AddUint64(&d.stats.TotWorkerAuth, 1)
res, err := client.Auth(user, pswd)
if err != nil {
atomic.AddUint64(&d.stats.TotWorkerAuthErr, 1)
d.receiver.OnError(fmt.Errorf("worker auth, server: %s, user: %s, err: %v",
server, user, err))
return 0
}
if res.Status != gomemcached.SUCCESS {
atomic.AddUint64(&d.stats.TotWorkerAuthFail, 1)
d.receiver.OnError(&AuthFailError{ServerURL: server, User: user})
return 0
}
if adminCred {
atomic.AddUint64(&d.stats.TotWorkerAuthOk, 1)
_, err = client.SelectBucket(d.bucketName)
if err != nil {
atomic.AddUint64(&d.stats.TotWorkerSelBktFail, 1)
d.receiver.OnError(fmt.Errorf("worker select bucket err: %v", err))
return 0
}
atomic.AddUint64(&d.stats.TotWorkerSelBktOk, 1)
}
}
}
uprOpenName := d.options.Name
if uprOpenName == "" {
uprOpenName = fmt.Sprintf("cbdatasource-%x", rand.Int63())
}
err = UPROpen(client, uprOpenName, d.options.FeedBufferSizeBytes)
if err != nil {
atomic.AddUint64(&d.stats.TotWorkerUPROpenErr, 1)
d.receiver.OnError(err)
return 0
}
atomic.AddUint64(&d.stats.TotWorkerUPROpenOk, 1)
ackBytes :=
uint32(d.options.FeedBufferAckThreshold * float32(d.options.FeedBufferSizeBytes))
sendCh := make(chan *gomemcached.MCRequest, 1)
sendEndCh := make(chan struct{})
recvEndCh := make(chan struct{})
cleanup := func(progress int, err error) int {
if err != nil {
d.receiver.OnError(err)
}
go func() {
<-recvEndCh
close(sendCh)
}()
return progress
}
currVBuckets := make(map[uint16]*VBucketState)
currVBucketsMutex := sync.Mutex{} // Protects currVBuckets.
go func() { // Sender goroutine.
defer close(sendEndCh)
atomic.AddUint64(&d.stats.TotWorkerTransmitStart, 1)
//.........这里部分代码省略.........
示例9: runFeed
func (feed *UprFeed) runFeed(ch chan *UprEvent) {
defer close(ch)
var headerBuf [gomemcached.HDR_LEN]byte
var pkt gomemcached.MCRequest
var event *UprEvent
mc := feed.conn.Hijack()
uprStats := &feed.stats
loop:
for {
sendAck := false
bytes, err := pkt.Receive(mc, headerBuf[:])
if err != nil {
ul.LogError("", "", "Error in receive %s", err.Error())
feed.Error = err
// send all the stream close messages to the client
feed.doStreamClose(ch)
break loop
} else {
event = nil
res := &gomemcached.MCResponse{
Opcode: pkt.Opcode,
Cas: pkt.Cas,
Opaque: pkt.Opaque,
Status: gomemcached.Status(pkt.VBucket),
Extras: pkt.Extras,
Key: pkt.Key,
Body: pkt.Body,
}
vb := vbOpaque(pkt.Opaque)
uprStats.TotalBytes = uint64(bytes)
feed.mu.RLock()
stream := feed.vbstreams[vb]
feed.mu.RUnlock()
switch pkt.Opcode {
case gomemcached.UPR_STREAMREQ:
if stream == nil {
ul.LogError("", "", "Stream not found for vb %d: %#v", vb, pkt)
break loop
}
status, rb, flog, err := handleStreamRequest(res)
if status == gomemcached.ROLLBACK {
event = makeUprEvent(pkt, stream)
// rollback stream
msg := "UPR_STREAMREQ with rollback %d for vb %d Failed: %v"
ul.LogError("", "", msg, rb, vb, err)
// delete the stream from the vbmap for the feed
feed.mu.Lock()
delete(feed.vbstreams, vb)
feed.mu.Unlock()
} else if status == gomemcached.SUCCESS {
event = makeUprEvent(pkt, stream)
event.Seqno = stream.StartSeq
event.FailoverLog = flog
stream.connected = true
ul.LogInfo("", "", "UPR_STREAMREQ for vb %d successful", vb)
} else if err != nil {
msg := "UPR_STREAMREQ for vbucket %d erro %s"
ul.LogError("", "", msg, vb, err.Error())
event = &UprEvent{
Opcode: gomemcached.UPR_STREAMREQ,
Status: status,
VBucket: vb,
Error: err,
}
}
case gomemcached.UPR_MUTATION,
gomemcached.UPR_DELETION,
gomemcached.UPR_EXPIRATION:
if stream == nil {
ul.LogError("", "", "Stream not found for vb %d: %#v", vb, pkt)
break loop
}
event = makeUprEvent(pkt, stream)
uprStats.TotalMutation++
sendAck = true
case gomemcached.UPR_STREAMEND:
if stream == nil {
ul.LogError("", "", "Stream not found for vb %d: %#v", vb, pkt)
break loop
}
//stream has ended
event = makeUprEvent(pkt, stream)
ul.LogInfo("", "", "Stream Ended for vb %d", vb)
sendAck = true
feed.mu.Lock()
delete(feed.vbstreams, vb)
feed.mu.Unlock()
case gomemcached.UPR_SNAPSHOT:
if stream == nil {
//.........这里部分代码省略.........