/
request.go
165 lines (137 loc) · 2.75 KB
/
request.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
// Copyright 2016 CodisLabs. All Rights Reserved.
// Licensed under the MIT (MIT-LICENSE.txt) license.
package proxy
import (
"sync"
"unsafe"
"github.com/CodisLabs/codis/pkg/proxy/redis"
"github.com/CodisLabs/codis/pkg/utils/sync2/atomic2"
)
type Request struct {
Multi []*redis.Resp
Batch *sync.WaitGroup
Group *sync.WaitGroup
Broken *atomic2.Bool
OpStr string
OpFlag
Database int32
UnixNano int64
*redis.Resp
Err error
Coalesce func() error
}
func (r *Request) IsBroken() bool {
return r.Broken != nil && r.Broken.IsTrue()
}
func (r *Request) MakeSubRequest(n int) []Request {
var sub = make([]Request, n)
for i := range sub {
x := &sub[i]
x.Batch = r.Batch
x.OpStr = r.OpStr
x.OpFlag = r.OpFlag
x.Broken = r.Broken
x.Database = r.Database
x.UnixNano = r.UnixNano
}
return sub
}
const GOLDEN_RATIO_PRIME_32 = 0x9e370001
func (r *Request) Seed16() uint {
h32 := uint32(r.UnixNano) + uint32(uintptr(unsafe.Pointer(r)))
h32 *= GOLDEN_RATIO_PRIME_32
return uint(h32 >> 16)
}
type RequestChan struct {
lock sync.Mutex
cond *sync.Cond
data []*Request
buff []*Request
waits int
closed bool
}
const DefaultRequestChanBuffer = 128
func NewRequestChan() *RequestChan {
return NewRequestChanBuffer(0)
}
func NewRequestChanBuffer(n int) *RequestChan {
if n <= 0 {
n = DefaultRequestChanBuffer
}
var ch = &RequestChan{
buff: make([]*Request, n),
}
ch.cond = sync.NewCond(&ch.lock)
return ch
}
func (c *RequestChan) Close() {
c.lock.Lock()
if !c.closed {
c.closed = true
c.cond.Broadcast()
}
c.lock.Unlock()
}
func (c *RequestChan) Buffered() int {
c.lock.Lock()
n := len(c.data)
c.lock.Unlock()
return n
}
func (c *RequestChan) PushBack(r *Request) int {
c.lock.Lock()
n := c.lockedPushBack(r)
c.lock.Unlock()
return n
}
func (c *RequestChan) PopFront() (*Request, bool) {
c.lock.Lock()
r, ok := c.lockedPopFront()
c.lock.Unlock()
return r, ok
}
func (c *RequestChan) lockedPushBack(r *Request) int {
if c.closed {
panic("send on closed chan")
}
if c.waits != 0 {
c.cond.Signal()
}
c.data = append(c.data, r)
return len(c.data)
}
func (c *RequestChan) lockedPopFront() (*Request, bool) {
for len(c.data) == 0 {
if c.closed {
return nil, false
}
c.data = c.buff[:0]
c.waits++
c.cond.Wait()
c.waits--
}
var r = c.data[0]
c.data, c.data[0] = c.data[1:], nil
return r, true
}
func (c *RequestChan) IsEmpty() bool {
return c.Buffered() == 0
}
func (c *RequestChan) PopFrontAll(onRequest func(r *Request) error) error {
for {
r, ok := c.PopFront()
if ok {
if err := onRequest(r); err != nil {
return err
}
} else {
return nil
}
}
}
func (c *RequestChan) PopFrontAllVoid(onRequest func(r *Request)) {
c.PopFrontAll(func(r *Request) error {
onRequest(r)
return nil
})
}