|
| 1 | +package sql |
| 2 | + |
| 3 | +import ( |
| 4 | + "database/sql" |
| 5 | + "fmt" |
| 6 | + "sync" |
| 7 | + |
| 8 | + "github.com/uber-go/tally/v4" |
| 9 | + "go.uber.org/zap" |
| 10 | + |
| 11 | + "github.com/uber/submitqueue/extensions/queue" |
| 12 | +) |
| 13 | + |
| 14 | +type factory struct { |
| 15 | + db *sql.DB |
| 16 | + config Config |
| 17 | + logger *zap.SugaredLogger |
| 18 | + metrics tally.Scope |
| 19 | + messageStore messageStore |
| 20 | + offsetStore offsetStore |
| 21 | + leaseStore partitionLeaseStore |
| 22 | + publisher queue.Publisher |
| 23 | + subscriber queue.Subscriber |
| 24 | + mu sync.RWMutex |
| 25 | + closed bool |
| 26 | +} |
| 27 | + |
| 28 | +// Params holds dependencies for creating a SQL queue factory |
| 29 | +type Params struct { |
| 30 | + // DB is the database connection (required) |
| 31 | + DB *sql.DB |
| 32 | + |
| 33 | + // Logger for debugging and observability (required) |
| 34 | + Logger *zap.Logger |
| 35 | + |
| 36 | + // MetricsScope for metrics collection (optional, uses noop if nil) |
| 37 | + MetricsScope tally.Scope |
| 38 | + |
| 39 | + // Config holds queue configuration |
| 40 | + Config Config |
| 41 | +} |
| 42 | + |
| 43 | +// NewQueue creates a new SQL-based queue factory |
| 44 | +func NewQueue(params Params) (queue.Queue, error) { |
| 45 | + if params.DB == nil { |
| 46 | + return nil, fmt.Errorf("DB is required") |
| 47 | + } |
| 48 | + if params.Logger == nil { |
| 49 | + return nil, fmt.Errorf("Logger is required") |
| 50 | + } |
| 51 | + |
| 52 | + if err := params.Config.Validate(); err != nil { |
| 53 | + return nil, fmt.Errorf("invalid config: %w", err) |
| 54 | + } |
| 55 | + |
| 56 | + // Use noop scope if not provided |
| 57 | + metricsScope := params.MetricsScope |
| 58 | + if metricsScope == nil { |
| 59 | + metricsScope = tally.NoopScope |
| 60 | + } |
| 61 | + |
| 62 | + // Test connection |
| 63 | + if err := params.DB.Ping(); err != nil { |
| 64 | + return nil, fmt.Errorf("failed to ping database: %w", err) |
| 65 | + } |
| 66 | + |
| 67 | + logger := params.Logger.Sugar().Named("queue.sql") |
| 68 | + logger.Infow("created SQL queue factory", |
| 69 | + "consumer_group", params.Config.ConsumerGroup, |
| 70 | + "worker_id", params.Config.WorkerID, |
| 71 | + "poll_interval", params.Config.PollInterval, |
| 72 | + "batch_size", params.Config.BatchSize, |
| 73 | + ) |
| 74 | + |
| 75 | + // Create stores |
| 76 | + messageStore := newMessageStore(params.DB, params.Config, params.Logger, metricsScope) |
| 77 | + offsetStore := newOffsetStore(params.DB, params.Config, params.Logger, metricsScope) |
| 78 | + leaseStore := newPartitionLeaseStore(params.DB, params.Config, params.Logger, metricsScope) |
| 79 | + |
| 80 | + f := &factory{ |
| 81 | + db: params.DB, |
| 82 | + config: params.Config, |
| 83 | + logger: logger, |
| 84 | + metrics: metricsScope.SubScope("queue"), |
| 85 | + messageStore: messageStore, |
| 86 | + offsetStore: offsetStore, |
| 87 | + leaseStore: leaseStore, |
| 88 | + } |
| 89 | + |
| 90 | + return f, nil |
| 91 | +} |
| 92 | + |
| 93 | +// Publisher returns a Publisher instance (singleton) |
| 94 | +func (f *factory) Publisher() queue.Publisher { |
| 95 | + f.mu.Lock() |
| 96 | + defer f.mu.Unlock() |
| 97 | + |
| 98 | + if f.publisher == nil { |
| 99 | + f.publisher = &publisher{ |
| 100 | + config: f.config, |
| 101 | + logger: f.logger.Named("publisher"), |
| 102 | + metrics: f.metrics.SubScope("publisher"), |
| 103 | + messageStore: f.messageStore, |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + return f.publisher |
| 108 | +} |
| 109 | + |
| 110 | +// Subscriber returns a Subscriber instance (singleton) |
| 111 | +func (f *factory) Subscriber() queue.Subscriber { |
| 112 | + f.mu.Lock() |
| 113 | + defer f.mu.Unlock() |
| 114 | + |
| 115 | + if f.subscriber == nil { |
| 116 | + f.subscriber = NewSubscriber( |
| 117 | + f.config, |
| 118 | + f.logger.Named("subscriber"), |
| 119 | + f.metrics.SubScope("subscriber"), |
| 120 | + f.messageStore, |
| 121 | + f.offsetStore, |
| 122 | + f.leaseStore, |
| 123 | + ) |
| 124 | + } |
| 125 | + |
| 126 | + return f.subscriber |
| 127 | +} |
| 128 | + |
| 129 | +// Close shuts down the factory and all associated resources |
| 130 | +func (f *factory) Close() error { |
| 131 | + // Mark as closed and grab references under lock |
| 132 | + f.mu.Lock() |
| 133 | + if f.closed { |
| 134 | + f.mu.Unlock() |
| 135 | + return nil |
| 136 | + } |
| 137 | + f.closed = true |
| 138 | + sub := f.subscriber |
| 139 | + pub := f.publisher |
| 140 | + f.mu.Unlock() |
| 141 | + |
| 142 | + // Close subscriber and publisher outside the lock to avoid deadlocks |
| 143 | + var errs []error |
| 144 | + |
| 145 | + if sub != nil { |
| 146 | + if err := sub.Close(); err != nil { |
| 147 | + errs = append(errs, fmt.Errorf("subscriber close failed: %w", err)) |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + if pub != nil { |
| 152 | + if err := pub.Close(); err != nil { |
| 153 | + errs = append(errs, fmt.Errorf("publisher close failed: %w", err)) |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + // Return all errors if any occurred |
| 158 | + if len(errs) == 0 { |
| 159 | + return nil |
| 160 | + } |
| 161 | + |
| 162 | + // Combine all errors into a single descriptive message |
| 163 | + errMsg := "multiple close errors: " |
| 164 | + for i, err := range errs { |
| 165 | + if i > 0 { |
| 166 | + errMsg += "; " |
| 167 | + } |
| 168 | + errMsg += err.Error() |
| 169 | + } |
| 170 | + return fmt.Errorf(errMsg) |
| 171 | +} |
0 commit comments