2022-11-21 06:18:33 -08:00
|
|
|
package queue
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/wormhole-foundation/wormhole/sdk/vaa"
|
|
|
|
)
|
|
|
|
|
|
|
|
// VAAInMemoryOption represents a VAA queue in memory option function.
|
|
|
|
type VAAInMemoryOption func(*VAAInMemory)
|
|
|
|
|
|
|
|
// VAAInMemory represents VAA queue in memory.
|
|
|
|
type VAAInMemory struct {
|
2024-02-06 16:00:45 -08:00
|
|
|
ch chan Message[[]byte]
|
2022-11-21 06:18:33 -08:00
|
|
|
size int
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewVAAInMemory creates a VAA queue in memory instances.
|
|
|
|
func NewVAAInMemory(opts ...VAAInMemoryOption) *VAAInMemory {
|
|
|
|
m := &VAAInMemory{size: 100}
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt(m)
|
|
|
|
}
|
2024-02-06 16:00:45 -08:00
|
|
|
m.ch = make(chan Message[[]byte], m.size)
|
2022-11-21 06:18:33 -08:00
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithSize allows to specify an channel size when setting a value.
|
|
|
|
func WithSize(v int) VAAInMemoryOption {
|
|
|
|
return func(i *VAAInMemory) {
|
|
|
|
i.size = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Publish sends the message to a channel.
|
|
|
|
func (i *VAAInMemory) Publish(_ context.Context, v *vaa.VAA, data []byte) error {
|
2023-02-02 09:51:33 -08:00
|
|
|
i.ch <- &memoryConsumerMessage{
|
|
|
|
data: data,
|
2022-11-21 06:18:33 -08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Consume returns the channel with the received messages.
|
2024-02-06 16:00:45 -08:00
|
|
|
func (i *VAAInMemory) Consume(_ context.Context) <-chan Message[[]byte] {
|
2022-11-21 06:18:33 -08:00
|
|
|
return i.ch
|
|
|
|
}
|
2023-02-02 09:51:33 -08:00
|
|
|
|
|
|
|
type memoryConsumerMessage struct {
|
|
|
|
data []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *memoryConsumerMessage) Data() []byte {
|
|
|
|
return m.data
|
|
|
|
}
|
|
|
|
|
2023-05-10 11:49:40 -07:00
|
|
|
func (m *memoryConsumerMessage) Done(_ context.Context) {}
|
2023-02-02 09:51:33 -08:00
|
|
|
|
|
|
|
func (m *memoryConsumerMessage) Failed() {}
|
|
|
|
|
|
|
|
func (m *memoryConsumerMessage) IsExpired() bool {
|
|
|
|
return false
|
|
|
|
}
|