|
| 1 | +package outbox |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + |
| 6 | + "github.com/ethereum/go-ethereum/metrics" |
| 7 | + "github.com/ethersphere/swarm/log" |
| 8 | + "github.com/ethersphere/swarm/pss/message" |
| 9 | +) |
| 10 | + |
| 11 | +type Config struct { |
| 12 | + NumberSlots int |
| 13 | + Forward forwardFunction |
| 14 | +} |
| 15 | + |
| 16 | +type Outbox struct { |
| 17 | + forwardFunc forwardFunction |
| 18 | + queue []*outboxMsg |
| 19 | + slots chan int |
| 20 | + process chan int |
| 21 | + stopC chan struct{} |
| 22 | +} |
| 23 | + |
| 24 | +type forwardFunction func(msg *message.Message) error |
| 25 | + |
| 26 | +var ErrOutboxFull = errors.New("outbox full") |
| 27 | + |
| 28 | +func NewOutbox(config *Config) *Outbox { |
| 29 | + outbox := &Outbox{ |
| 30 | + forwardFunc: config.Forward, |
| 31 | + queue: make([]*outboxMsg, config.NumberSlots), |
| 32 | + slots: make(chan int, config.NumberSlots), |
| 33 | + process: make(chan int), |
| 34 | + stopC: make(chan struct{}), |
| 35 | + } |
| 36 | + // fill up outbox slots |
| 37 | + for i := 0; i < cap(outbox.slots); i++ { |
| 38 | + outbox.slots <- i |
| 39 | + } |
| 40 | + return outbox |
| 41 | +} |
| 42 | + |
| 43 | +func (o *Outbox) Start() { |
| 44 | + log.Info("Starting outbox") |
| 45 | + go o.processOutbox() |
| 46 | +} |
| 47 | + |
| 48 | +func (o *Outbox) Stop() { |
| 49 | + log.Info("Stopping outbox") |
| 50 | + close(o.stopC) |
| 51 | +} |
| 52 | + |
| 53 | +// Enqueue a new element in the outbox if there is any slot available. |
| 54 | +// Then send it to process. This method is blocking in the process channel! |
| 55 | +func (o *Outbox) Enqueue(outboxMsg *outboxMsg) error { |
| 56 | + // first we try to obtain a slot in the outbox |
| 57 | + select { |
| 58 | + case slot := <-o.slots: |
| 59 | + o.queue[slot] = outboxMsg |
| 60 | + metrics.GetOrRegisterGauge("pss.outbox.len", nil).Update(int64(o.len())) |
| 61 | + // we send this message slot to process |
| 62 | + select { |
| 63 | + case <-o.stopC: |
| 64 | + case o.process <- slot: |
| 65 | + } |
| 66 | + return nil |
| 67 | + default: |
| 68 | + metrics.GetOrRegisterCounter("pss.enqueue.outbox.full", nil).Inc(1) |
| 69 | + return ErrOutboxFull |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +//ProcessOutbox starts a routine that tries to forward messages present in the outbox queue |
| 74 | +func (o *Outbox) processOutbox() { |
| 75 | + for slot := range o.process { |
| 76 | + go func(slot int) { |
| 77 | + msg := o.queue[slot] |
| 78 | + metrics.GetOrRegisterResettingTimer("pss.handle.outbox", nil).UpdateSince(msg.startedAt) |
| 79 | + if err := o.forwardFunc(msg.msg); err != nil { |
| 80 | + metrics.GetOrRegisterCounter("pss.forward.err", nil).Inc(1) |
| 81 | + log.Debug(err.Error()) |
| 82 | + // requeue the message for processing |
| 83 | + o.requeue(slot) |
| 84 | + log.Debug("Message requeued", "slot", slot) |
| 85 | + return |
| 86 | + } |
| 87 | + //message processed, free the outbox slot |
| 88 | + o.free(slot) |
| 89 | + metrics.GetOrRegisterGauge("pss.outbox.len", nil).Update(int64(o.len())) |
| 90 | + }(slot) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +func (o *Outbox) free(slot int) { |
| 95 | + select { |
| 96 | + case <-o.stopC: |
| 97 | + case o.slots <- slot: |
| 98 | + } |
| 99 | + |
| 100 | +} |
| 101 | + |
| 102 | +func (o *Outbox) requeue(slot int) { |
| 103 | + select { |
| 104 | + case <-o.stopC: |
| 105 | + case o.process <- slot: |
| 106 | + } |
| 107 | +} |
| 108 | +func (o *Outbox) len() int { |
| 109 | + return cap(o.slots) - len(o.slots) |
| 110 | +} |
0 commit comments