-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathrequests_port.go
More file actions
688 lines (553 loc) · 22.5 KB
/
requests_port.go
File metadata and controls
688 lines (553 loc) · 22.5 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
package ctrl
import (
"context"
"fmt"
"io"
"net"
"sort"
"strconv"
"strings"
"sync"
"time"
"github.com/fxamacker/cbor/v2"
"github.com/google/uuid"
)
// portInitialData is the data that is sent to the source and destination nodes when a port forwarding is created.
type portInitialData struct {
UUID string
SourceNode Node
SourceSubMethod Method
SourceIPAndPort string
DestinationNode Node
DestinationSubMethod Method
DestinationIPAndPort string
MaxSessionTimeout int
}
// NewPortInitialData creates a new portInitialData struct.
func NewPortInitialData(uuid string, sourceNode Node, sourceSubMethod Method, sourcePort string, destinationNode Node, destinationSubMethod Method, destinationPort string, maxSessionTimeout int) portInitialData {
pid := portInitialData{
UUID: uuid,
SourceNode: sourceNode,
SourceSubMethod: sourceSubMethod,
SourceIPAndPort: sourcePort,
DestinationNode: destinationNode,
DestinationSubMethod: destinationSubMethod,
DestinationIPAndPort: destinationPort,
MaxSessionTimeout: maxSessionTimeout,
}
return pid
}
// The main method that will handle the initial message, and setup whats needed for an outbound connection.
// It will setup a sub process that will handle the individual session of the port forwarding on the
// source node, and also setup the local tcp listener on the source node that a client can connect to.
//
// NB: All logic for the forwarding are done in the subprocesses started.
func methodPortSrc(proc process, message Message, node string) ([]byte, error) {
go func() {
defer proc.processes.wg.Done()
// Message example to start an outbound connection
// ---
// - toNode:
// - node1
// method: portSrc
// methodArgs:
// - node1 # destination node
// - localhost:8080 # destination node and port
// - localhost:8090 # source node and port
// - 100 # max session timeout
// replymethod: console
const (
arg0_DestinationNode = 0
arg1_DestinationIPAndPort = 1
arg2_SourceIPandPort = 2
arg3_MaxSessionTimeout = 3
)
wantMethodArgs := "want: (0)destination-node, (1)destination-ip-and-port, (2) source-ip-and-port, (3)max-session-timeout"
uuid := uuid.New().String()
sourceNode := proc.node
proc.processes.wg.Add(1)
if len(message.MethodArgs) < arg3_MaxSessionTimeout {
proc.errorKernel.logError("methodPortSrc: to few methodArgs defined in message", "want", wantMethodArgs)
return
}
// Destination node
if message.MethodArgs[arg0_DestinationNode] == "" {
proc.errorKernel.logError("methodPortSrc: no destination node specified in method args", "want", wantMethodArgs)
return
}
destinationNode := Node(message.MethodArgs[arg0_DestinationNode])
// Destination port
if message.MethodArgs[arg1_DestinationIPAndPort] == "" {
proc.errorKernel.logError("methodPortSrc: no destination port specified in method args", "want", wantMethodArgs)
return
}
destinationIPAndPort := message.MethodArgs[arg1_DestinationIPAndPort]
// Source port
if message.MethodArgs[arg2_SourceIPandPort] == "" {
proc.errorKernel.logError("methodPortSrc: no source port specified in method args", "want", wantMethodArgs)
return
}
sourceIPAndPort := message.MethodArgs[arg2_SourceIPandPort]
// Max session timeout
if message.MethodArgs[arg3_MaxSessionTimeout] == "" {
proc.errorKernel.logError("methodPortSrc: no max session time specified in method args", "want", wantMethodArgs)
return
}
n, err := strconv.Atoi(message.MethodArgs[arg3_MaxSessionTimeout])
if err != nil {
proc.errorKernel.logError("methodPortSrc: unable to convert max session timeout from string to int", "error", err)
return
}
maxSessionTimeout := n
// Prepare the naming for the dst method here so we can have all the
// information in the pid from the beginning at both ends and not have
// to generate naming on the destination node.
dstSubProcessName := fmt.Sprintf("%v.%v", SUBPortDst, uuid)
destinationSubMethod := Method(dstSubProcessName)
// ------- Create the source sub process -------
// Create a child context to use with the procFunc with timeout set to the max allowed total copy time
// specified in the message.
var ctx context.Context
var cancel context.CancelFunc
func() {
ctx, cancel = context.WithTimeout(proc.ctx, time.Second*time.Duration(maxSessionTimeout))
}()
srcSubProcessName := fmt.Sprintf("%v.%v", SUBPortSrc, uuid)
sourceSubMethod := Method(srcSubProcessName)
// Create a new subprocess that will handle the network source side.
subject := newSubjectNoVerifyHandler(sourceSubMethod, node)
portSrcSubProc := newSubProcess(ctx, proc.server, subject)
// Create a new portInitialData with all the information we need for starting the sub processes.
// NB: Using the destination port as the source port on the source process for now.
pid := NewPortInitialData(uuid, sourceNode, sourceSubMethod, sourceIPAndPort, destinationNode, destinationSubMethod, destinationIPAndPort, maxSessionTimeout)
// Attach a procfunc to the sub process that will do the actual logic with the network source port.
portSrcSubProc.procFunc = portSrcSubProcFunc(pid, message, cancel)
// Assign a handler to the sub process for receiving messages for the subprocess.
portSrcSubProc.handler = portSrcSubHandler()
// Start sub process. The process will be killed when the context expires.
go portSrcSubProc.start(true)
proc.errorKernel.logDebug("methodPortSrc, pid", "pid", pid)
// ------- Prepare the data payload to send to the dst to start the dst sub process -------
cb, err := cbor.Marshal(pid)
if err != nil {
er := fmt.Errorf("error: methodPortSrc: cbor marshalling failed: %v, message: %v", err, message)
proc.errorKernel.errSend(proc, message, er, logWarning)
cancel()
}
// Send a message to the the destination node, to also start up a sub
// process there.
msg := Message{
ToNode: pid.DestinationNode,
Method: PortDst,
Data: cb,
ACKTimeout: message.ACKTimeout,
Retries: message.Retries,
ReplyMethod: Console, // TODO: Adding for debug output
ReplyACKTimeout: message.ReplyACKTimeout,
ReplyRetries: message.ReplyRetries,
}
proc.newMessagesCh <- msg
replyData := fmt.Sprintf("info: succesfully started port source process: procName=%v, srcNode=%v, sourcePort=%v, dstNode=%v, starting sub process=%v", portSrcSubProc.processName, node, pid.SourceIPAndPort, pid.DestinationNode, srcSubProcessName)
newReplyMessage(proc, message, []byte(replyData))
}()
ackMsg := []byte("confirmed from: " + node + ": " + fmt.Sprint(message.ID))
return ackMsg, nil
}
// The main method that will handle the initial message sendt from the source process, and setup
// sub process that will handle the individual session of the port forwarding, and also
// setup the connection the final tcp endpoint on the destination node.
//
// NB: All logic for the forwarding are done in the subprocesses started.
func methodPortDst(proc process, message Message, node string) ([]byte, error) {
var subProcessName string
proc.processes.wg.Add(1)
go func() {
defer proc.processes.wg.Done()
// Get the status message sent from source.
var pid portInitialData
err := cbor.Unmarshal(message.Data, &pid)
if err != nil {
er := fmt.Errorf("error: methodPortDst: failed to cbor Unmarshal data: %v, message=%v", err, message)
proc.errorKernel.errSend(proc, message, er, logWarning)
return
}
proc.errorKernel.logDebug("methodPortDst: got pid", "pid", pid)
// Create a child context to use with the procFunc
var ctx context.Context
var cancel context.CancelFunc
func() {
ctx, cancel = context.WithTimeout(proc.ctx, time.Second*time.Duration(pid.MaxSessionTimeout))
}()
// Start preparing to start a sub process.
sub := newSubjectNoVerifyHandler(pid.DestinationSubMethod, node)
// But first...
// Check if we already got a sub process registered and started with
// the processName. If true, return here and don't start up another
// process.
//
// This check is put in here if a message for some reason are
// received more than once. The reason that this might happen can be
// that a message for the same copy request was received earlier, but
// was unable to start up within the timeout specified. The Sender of
// that request will then resend the message, but at the time that
// second message is received the subscriber process started for the
// previous message is then fully up and running, so we just discard
// that second message in those cases.
pn := processNameGet(sub.name())
proc.processes.active.mu.Lock()
_, ok := proc.processes.active.procNames[pn]
proc.processes.active.mu.Unlock()
if ok {
proc.errorKernel.logDebug("methodCopyDst: subprocesses already existed, will not start another subscriber for", "processName", pn)
// If the process name already existed we return here before any
// new information is registered in the process map and we avoid
// having to clean that up later.
return
}
// Create a new sub process.
portDstSubProc := newSubProcess(ctx, proc.server, sub)
// Give the sub process a procFunc that will do the actual networking within a procFunc,
portDstSubProc.procFunc = portDstSubProcFunc(pid, message, cancel)
// assign a handler to the sub process
portDstSubProc.handler = portDstSubHandler()
// The process will be killed when the context expires.
go portDstSubProc.start(true)
replyData := fmt.Sprintf("info: succesfully initiated port destination process: procName=%v, srcNode=%v, starting sub process=%v for the actual copying", portDstSubProc.processName, node, subProcessName)
newReplyMessage(proc, message, []byte(replyData))
}()
ackMsg := []byte("confirmed from: " + node + ": " + fmt.Sprint(message.ID))
return ackMsg, nil
}
// portSrcSubHandler is the handler for messages received and destined for the port source sub process.
// It will pass the message to the procFunc of the port source sub process, which will do the actual
// handling of the messages.
func portSrcSubHandler() func(process, Message, string) ([]byte, error) {
h := func(proc process, message Message, node string) ([]byte, error) {
select {
case <-proc.ctx.Done():
proc.errorKernel.logDebug("copySrcHandler: process ended", "processName", proc.processName)
case proc.procFuncCh <- message:
proc.errorKernel.logDebug("copySrcHandler: passing message over to procFunc", "processName", proc.processName)
}
return nil, nil
}
return h
}
// portDstSubHandler is the handler for messages received and destined for the port destination sub process.
// It will pass the message to the procFunc of the port destination sub process, which will do the actual
// handling of the messages.
func portDstSubHandler() func(process, Message, string) ([]byte, error) {
h := func(proc process, message Message, node string) ([]byte, error) {
select {
case <-proc.ctx.Done():
proc.errorKernel.logDebug("copyDstHandler: process ended", "processName", proc.processName)
case proc.procFuncCh <- message:
proc.errorKernel.logDebug("copyDstHandler: passing message over to procFunc", "processName", proc.processName)
}
return nil, nil
}
return h
}
type portData struct {
OK bool
ErrorMsg string
Data []byte
ID int
}
// portSrcSubProcFunc is the function that will be run by the portSrcSubProc process.
// It will listen on the source IP and port, and send messages to the destination node
// to write the data to the destination IP and port.
// It will also handle the incomming messages from the destination node and write the
// data in the message to the source IP and port.
func portSrcSubProcFunc(pid portInitialData, initialMessage Message, cancel context.CancelFunc) func(context.Context, process, chan Message) error {
pf := func(ctx context.Context, proc process, procFuncCh chan Message) error {
proc.errorKernel.logDebug("STARTED PROCFUNC", "processName", proc.subject.name())
defer cancel()
defer proc.errorKernel.logDebug("portSrcProcFunc: canceled procFunc", "processName", proc.processName)
listener, err := net.Listen("tcp", pid.SourceIPAndPort)
if err != nil {
// TODO: Send a message to destination sub process that there was an error,
// and that it should stop.
proc.errorKernel.logError("portSrcSubProcFunc: net.Listen failed", "err", err)
return err
}
// Start a goroutine to handle the tcp listener.
go func() {
for {
conn, err := listener.Accept()
if err != nil {
// TODO: Send a message to destination sub process that there was an error,
// and that it should stop.
proc.errorKernel.logError("portSrcSubProcFunc: listener.Accept failed", "err", err)
return
}
defer func() {
conn.Close()
listener.Close()
proc.errorKernel.logDebug("portSrcSubProcFunc: closed connection and listener")
}()
// Read the data from the tcp connection, create messages from it, and
// send it to the dst node.
go func() {
id := 0
for {
b := make([]byte, 65535)
n, err := conn.Read(b)
if err != nil {
proc.errorKernel.logError("portSrcSubProcFunc: conn.Read failed", "err=", err)
return
}
pd := portData{
ID: id,
Data: b[:n],
}
id++
cb, err := cbor.Marshal(pd)
if err != nil {
proc.errorKernel.logError("portDstSubProcFunc: cbor marshalling failed", "err", err)
return
}
msg := Message{
ToNode: pid.DestinationNode,
Method: Method(pid.DestinationSubMethod),
Data: cb,
ACKTimeout: initialMessage.ACKTimeout,
Retries: initialMessage.Retries,
ReplyMethod: None,
}
proc.errorKernel.logDebug("portSrcSubProcFunc: Created message to send", "pd.ID", pd.ID)
select {
case <-ctx.Done():
proc.errorKernel.logDebug("portSrcProcFunc: canceling procFunc", "processName", proc.processName)
return
case proc.newMessagesCh <- msg:
proc.errorKernel.logDebug(" ---->: Sending message", "pd.ID", pd.ID, "length", len(pd.Data))
}
}
}()
// -----------------------------------------------------------------------------------
// Read from messages from dst node and write to the source network connection.
// -----------------------------------------------------------------------------------
expectedID := 0
buffer := NewPortSortedBuffer()
for {
select {
case <-ctx.Done():
proc.errorKernel.logDebug("portSrcProcFunc: canceling procFunc", "processName", proc.processName)
return
// Handle the messages reveived from the sub process on the src node.
// The messages will contain the data to be sent to the dst node.
case message := <-procFuncCh:
var pd portData
err := cbor.Unmarshal(message.Data, &pd)
if err != nil {
proc.errorKernel.logError("portSrcSubProcFunc: cbor unmarshalling failed", "err", err)
return
}
proc.errorKernel.logDebug("<---- GOT MESSAGE ON SRC", "pd.OK", pd.OK, "pd.ID", pd.ID, "length", len(pd.Data))
buffer.Push(pd)
err = func() error {
// Write the data to the network connection.
for {
nextID, _ := buffer.PeekNextID()
if expectedID != nextID {
proc.errorKernel.logDebug("portSrcSubProcFunc: WRONG ID, WILL WAIT FOR NEXT MESSAGE", "expectedID", expectedID, "nextID", pd.ID)
return nil
}
proc.errorKernel.logDebug("portSrcSubProcFunc correct id", "EXPECTED", expectedID, "GOT", pd.ID)
pdPopped, ok := buffer.Pop()
if !ok {
proc.errorKernel.logDebug("portSrcSubProcFunc: Buffer is empty, break out, and wait for next message.")
return nil
}
proc.errorKernel.logDebug("portSrcSubProcFunc: popped", "id", pdPopped.ID, "size", len(pdPopped.Data))
n, err := conn.Write(pdPopped.Data)
if err != nil {
proc.errorKernel.logError("portSrcSubProcFunc: conn.Write failed", "err", err)
return err
}
proc.errorKernel.logDebug("--------> conn: portSrcSubProcFunc: wrote bytes with ID to connection, exptedID was", "bytes", n, "popped id", pdPopped.ID, "expectedID", expectedID)
expectedID++
if !pdPopped.OK {
proc.errorKernel.logDebug("error: portSrcSubProcFunc: pdd.OK is false", "err", pdPopped.ErrorMsg)
return fmt.Errorf("%v", pdPopped.ErrorMsg)
}
}
}()
if err != nil {
return
}
}
}
}
}()
<-ctx.Done()
return nil
}
return pf
}
// portDstSubProcFunc is the function that will be run by the portDstSubProc process.
// It will connect to the destination IP and port, and send messages to the source node
// to write the data to the source IP and port.
// It will also handle the incomming messages from the source node and write the
// data in the message to the destination IP and port.
func portDstSubProcFunc(pid portInitialData, message Message, cancel context.CancelFunc) func(context.Context, process, chan Message) error {
pf := func(ctx context.Context, proc process, procFuncCh chan Message) error {
defer cancel()
proc.errorKernel.logDebug("portDstSubProcFunc: STARTED PROCFUNC", "processName", proc.subject.name())
defer proc.errorKernel.logDebug("portDstProcFunc: canceled procFunc", "processName", proc.processName)
// TODO: Start the tcp connection for the dst node here.
// ------------
conn, err := net.Dial("tcp", pid.DestinationIPAndPort)
if err != nil {
proc.errorKernel.logError("portDstSubProcFunc: dial failed", "err", err)
return err
}
defer conn.Close()
// Read from destination network connection and send messages to src node of whats read.
go func() {
id := 0
for {
var errorMsg string
ok := true
b := make([]byte, 65535)
n, err := conn.Read(b)
if err != nil {
// If there was an error while reading, set the value of errorMsg and ok accordingly.
// This information will then be used on the src node to stop sub processes etc.
switch {
case err == io.EOF:
ok = false
proc.errorKernel.logError("portDstSubProcFunc: conn.Read() returned EOF", "bytes", n)
case strings.Contains(err.Error(), "use of closed network connection"):
ok = false
proc.errorKernel.logError("portDstSubProcFunc: conn.Read(): closed network connection", "err", err, "bytes", n)
default:
ok = false
proc.errorKernel.logError("portDstSubProcFunc: conn.Read(): other error", "err", err, "bytes", n)
}
}
proc.errorKernel.logDebug("portDstSubProcFunc: read from network conn", "bytes", n)
pdd := portData{
OK: ok,
ErrorMsg: errorMsg,
Data: b[:n],
ID: id,
}
id++
cb, err := cbor.Marshal(pdd)
if err != nil {
proc.errorKernel.logError("portDstSubProcFunc: cbor marshalling failed", "err", err)
return
}
msg := Message{
ToNode: pid.SourceNode,
Method: Method(pid.SourceSubMethod),
Data: cb,
ACKTimeout: message.ACKTimeout,
Retries: message.Retries,
ReplyMethod: None,
}
proc.newMessagesCh <- msg
proc.errorKernel.logDebug("portDstSubProcFunc: Created message to send", "id", id)
// If there was en error while reading, we exit the loop, so the connection is closed.
if !ok {
// TODO: Check out the cancelation!!!
//cancel()
return
}
}
}()
// -----------------------------------------------------------------------------------
// Read from messages from src node and write to the destination network connection.
// -----------------------------------------------------------------------------------
expectedID := 0
buffer := NewPortSortedBuffer()
for {
select {
case <-ctx.Done():
proc.errorKernel.logDebug("portDstProcFunc: got <-ctx.Done() cancelling procFunc", "processName", proc.processName)
return nil
// Pick up the message recived by the copySrcSubHandler.
case message := <-procFuncCh:
var pd portData
err := cbor.Unmarshal(message.Data, &pd)
if err != nil {
proc.errorKernel.logError("portDstSubProcFunc: cbor unmarshalling failed", "err", err)
}
proc.errorKernel.logDebug("portdstSubProcFunc: <---- GOT DATA ON DST, id: %v, length: %v\n", pd.ID, len(pd.Data))
buffer.Push(pd)
err = func() error {
// Loop over eventual messages in the buffer and write them to the connection.
for {
nextID, _ := buffer.PeekNextID()
if expectedID != nextID {
proc.errorKernel.logDebug("portdstSubProcFunc: WRONG ID, WILL WAIT FOR NEXT MESSAGE", "expectedID", expectedID, "nextID", pd.ID)
return nil
}
proc.errorKernel.logDebug("portDstSubProcFunc: CORRECT ID, EXPECTED: %v, GOT: %v\n", expectedID, pd.ID)
pdPopped, ok := buffer.Pop()
if !ok {
// Buffer is empty, break out and wait for next message.
return nil
}
n, err := conn.Write(pdPopped.Data)
if err != nil {
err := fmt.Errorf("error: portDstSubProcFunc: conn.Write failed. err=%v", err)
proc.errorKernel.logError(err.Error())
return err
}
proc.errorKernel.logDebug("portDstSubProcFunc: --------> conn: wrote to connection", "bytes", n)
expectedID++
}
}()
// Check if there was an error in the above function.
if err != nil {
return err
}
}
}
}
return pf
}
// -----------------------------------------------------------
// portSortedBuffer is a thread-safe buffer that sorts the data by ID.
type portSortedBuffer struct {
buffer []portData
mu sync.Mutex
}
// NewPortSortedBuffer creates a new portSortedBuffer.
func NewPortSortedBuffer() *portSortedBuffer {
b := portSortedBuffer{}
return &b
}
// Push adds a new portData to the buffer and sorts it by ID.
func (b *portSortedBuffer) Push(value portData) {
b.buffer = append(b.buffer, value)
b.mu.Lock()
defer b.mu.Unlock()
sort.SliceStable(b.buffer, func(i, j int) bool {
return b.buffer[i].ID < b.buffer[j].ID
})
}
// Pop removes and returns the first portData from the buffer.
func (b *portSortedBuffer) Pop() (portData, bool) {
b.mu.Lock()
defer b.mu.Unlock()
if len(b.buffer) == 0 {
return portData{}, false
}
value := b.buffer[0]
b.buffer = b.buffer[1:]
return value, true
}
// PeekNextID returns the ID of the next portData in the buffer without removing it.
func (b *portSortedBuffer) PeekNextID() (int, bool) {
b.mu.Lock()
defer b.mu.Unlock()
if len(b.buffer) == 0 {
return -1, false
}
id := b.buffer[0].ID
return id, true
}