|
| 1 | +package gorums |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "math" |
| 6 | + "math/rand" |
| 7 | + "sync" |
| 8 | + "sync/atomic" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/relab/gorums/ordering" |
| 12 | + "google.golang.org/grpc" |
| 13 | + "google.golang.org/grpc/codes" |
| 14 | + "google.golang.org/grpc/status" |
| 15 | + "google.golang.org/protobuf/reflect/protoreflect" |
| 16 | +) |
| 17 | + |
| 18 | +type request struct { |
| 19 | + ctx context.Context |
| 20 | + msg *Message |
| 21 | + opts callOptions |
| 22 | +} |
| 23 | + |
| 24 | +type response struct { |
| 25 | + nid uint32 |
| 26 | + msg protoreflect.ProtoMessage |
| 27 | + err error |
| 28 | +} |
| 29 | + |
| 30 | +type channel struct { |
| 31 | + sendQ chan request |
| 32 | + node *Node // needed for ID and setLastError |
| 33 | + rand *rand.Rand |
| 34 | + gorumsClient ordering.GorumsClient |
| 35 | + gorumsStream ordering.Gorums_NodeStreamClient |
| 36 | + streamMut sync.RWMutex |
| 37 | + streamBroken atomicFlag |
| 38 | + parentCtx context.Context |
| 39 | + streamCtx context.Context |
| 40 | + cancelStream context.CancelFunc |
| 41 | + responseRouter map[uint64]chan<- response |
| 42 | + responseMut sync.Mutex |
| 43 | +} |
| 44 | + |
| 45 | +func newChannel(n *Node) *channel { |
| 46 | + return &channel{ |
| 47 | + sendQ: make(chan request, n.mgr.opts.sendBuffer), |
| 48 | + node: n, |
| 49 | + rand: rand.New(rand.NewSource(time.Now().UnixNano())), |
| 50 | + responseRouter: make(map[uint64]chan<- response), |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +func (c *channel) connect(ctx context.Context, conn *grpc.ClientConn) error { |
| 55 | + var err error |
| 56 | + c.parentCtx = ctx |
| 57 | + c.streamCtx, c.cancelStream = context.WithCancel(c.parentCtx) |
| 58 | + c.gorumsClient = ordering.NewGorumsClient(conn) |
| 59 | + c.gorumsStream, err = c.gorumsClient.NodeStream(c.streamCtx) |
| 60 | + if err != nil { |
| 61 | + return err |
| 62 | + } |
| 63 | + go c.sendMsgs() |
| 64 | + go c.recvMsgs() |
| 65 | + return nil |
| 66 | +} |
| 67 | + |
| 68 | +func (c *channel) routeResponse(msgID uint64, resp response) { |
| 69 | + c.responseMut.Lock() |
| 70 | + defer c.responseMut.Unlock() |
| 71 | + if ch, ok := c.responseRouter[msgID]; ok { |
| 72 | + ch <- resp |
| 73 | + delete(c.responseRouter, msgID) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +func (c *channel) enqueue(req request, responseChan chan<- response) { |
| 78 | + if responseChan != nil { |
| 79 | + c.responseMut.Lock() |
| 80 | + c.responseRouter[req.msg.Metadata.MessageID] = responseChan |
| 81 | + c.responseMut.Unlock() |
| 82 | + } |
| 83 | + c.sendQ <- req |
| 84 | +} |
| 85 | + |
| 86 | +func (c *channel) sendMsg(req request) (err error) { |
| 87 | + // unblock the waiting caller unless noSendWaiting is enabled |
| 88 | + defer func() { |
| 89 | + if req.opts.callType == E_Multicast || req.opts.callType == E_Unicast && !req.opts.noSendWaiting { |
| 90 | + c.routeResponse(req.msg.Metadata.MessageID, response{}) |
| 91 | + } |
| 92 | + }() |
| 93 | + |
| 94 | + // don't send if context is already cancelled. |
| 95 | + if req.ctx.Err() != nil { |
| 96 | + return req.ctx.Err() |
| 97 | + } |
| 98 | + |
| 99 | + c.streamMut.RLock() |
| 100 | + defer c.streamMut.RUnlock() |
| 101 | + |
| 102 | + done := make(chan struct{}, 1) |
| 103 | + |
| 104 | + // wait for either the message to be sent, or the request context being cancelled. |
| 105 | + // if the request context was cancelled, then we most likely have a blocked stream. |
| 106 | + go func() { |
| 107 | + select { |
| 108 | + case <-done: |
| 109 | + case <-req.ctx.Done(): |
| 110 | + c.cancelStream() |
| 111 | + } |
| 112 | + }() |
| 113 | + |
| 114 | + err = c.gorumsStream.SendMsg(req.msg) |
| 115 | + if err != nil { |
| 116 | + c.node.setLastErr(err) |
| 117 | + c.streamBroken.set() |
| 118 | + } |
| 119 | + done <- struct{}{} |
| 120 | + |
| 121 | + return err |
| 122 | +} |
| 123 | + |
| 124 | +func (c *channel) sendMsgs() { |
| 125 | + var req request |
| 126 | + for { |
| 127 | + select { |
| 128 | + case <-c.parentCtx.Done(): |
| 129 | + return |
| 130 | + case req = <-c.sendQ: |
| 131 | + } |
| 132 | + // return error if stream is broken |
| 133 | + if c.streamBroken.get() { |
| 134 | + err := status.Errorf(codes.Unavailable, "stream is down") |
| 135 | + c.routeResponse(req.msg.Metadata.MessageID, response{nid: c.node.ID(), msg: nil, err: err}) |
| 136 | + continue |
| 137 | + } |
| 138 | + // else try to send message |
| 139 | + err := c.sendMsg(req) |
| 140 | + if err != nil { |
| 141 | + // return the error |
| 142 | + c.routeResponse(req.msg.Metadata.MessageID, response{nid: c.node.ID(), msg: nil, err: err}) |
| 143 | + } |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +func (c *channel) recvMsgs() { |
| 148 | + for { |
| 149 | + resp := newMessage(responseType) |
| 150 | + c.streamMut.RLock() |
| 151 | + err := c.gorumsStream.RecvMsg(resp) |
| 152 | + if err != nil { |
| 153 | + c.streamBroken.set() |
| 154 | + c.streamMut.RUnlock() |
| 155 | + c.node.setLastErr(err) |
| 156 | + // attempt to reconnect |
| 157 | + c.reconnect() |
| 158 | + } else { |
| 159 | + c.streamMut.RUnlock() |
| 160 | + err := status.FromProto(resp.Metadata.GetStatus()).Err() |
| 161 | + c.routeResponse(resp.Metadata.MessageID, response{nid: c.node.ID(), msg: resp.Message, err: err}) |
| 162 | + } |
| 163 | + |
| 164 | + select { |
| 165 | + case <-c.parentCtx.Done(): |
| 166 | + return |
| 167 | + default: |
| 168 | + } |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +func (c *channel) reconnect() { |
| 173 | + c.streamMut.Lock() |
| 174 | + defer c.streamMut.Unlock() |
| 175 | + backoffCfg := c.node.mgr.opts.backoff |
| 176 | + |
| 177 | + var retries float64 |
| 178 | + for { |
| 179 | + var err error |
| 180 | + |
| 181 | + c.streamCtx, c.cancelStream = context.WithCancel(c.parentCtx) |
| 182 | + c.gorumsStream, err = c.gorumsClient.NodeStream(c.streamCtx) |
| 183 | + if err == nil { |
| 184 | + c.streamBroken.clear() |
| 185 | + return |
| 186 | + } |
| 187 | + c.cancelStream() |
| 188 | + c.node.setLastErr(err) |
| 189 | + delay := float64(backoffCfg.BaseDelay) |
| 190 | + max := float64(backoffCfg.MaxDelay) |
| 191 | + for r := retries; delay < max && r > 0; r-- { |
| 192 | + delay *= backoffCfg.Multiplier |
| 193 | + } |
| 194 | + delay = math.Min(delay, max) |
| 195 | + delay *= 1 + backoffCfg.Jitter*(rand.Float64()*2-1) |
| 196 | + select { |
| 197 | + case <-time.After(time.Duration(delay)): |
| 198 | + retries++ |
| 199 | + case <-c.parentCtx.Done(): |
| 200 | + return |
| 201 | + } |
| 202 | + } |
| 203 | +} |
| 204 | + |
| 205 | +type atomicFlag struct { |
| 206 | + flag int32 |
| 207 | +} |
| 208 | + |
| 209 | +func (f *atomicFlag) set() { atomic.StoreInt32(&f.flag, 1) } |
| 210 | +func (f *atomicFlag) get() bool { return atomic.LoadInt32(&f.flag) == 1 } |
| 211 | +func (f *atomicFlag) clear() { atomic.StoreInt32(&f.flag, 0) } |
0 commit comments