Skip to content

Instantly share code, notes, and snippets.

View rishabh-gupta2's full-sized avatar
💭
At work

Rishabh Gupta rishabh-gupta2

💭
At work
View GitHub Profile
@rishabh-gupta2
rishabh-gupta2 / outbox_store.go
Last active March 30, 2021 11:51
Store interface
// Store provides an interface for persisting and reading jobs.
type Store interface {
// FindPendingJobsToProcess reads new jobs to be processed,
// and marks them as processing atomically.
FindPendingJobsToProcess(ctx context.Context, jobBatchSize int32, jobProcessingTimeout time.Duration) ([]*Job, error)
// FindFailedJobsToProcess reads failed jobs to be processed again,
// and marks them as processing atomically.
FindFailedJobsToProcess(ctx context.Context, jobBatchSize int32) ([]*Job, error)
@rishabh-gupta2
rishabh-gupta2 / outbox_job_handler.go
Created March 30, 2021 11:54
JobHandler interface
// JobHandler is anything that can be sent using outboxer.
type JobHandler interface {
// Name returns a unique name for each handler.
// Unique name for each handler is a constraint,
// otherwise it will fail at the time of registration.
Name() string
// Handle is responsible for implementation-specific handling of jobs.
// Implementations need to ensure atomicity and graceful handling
// of out-of-order events.
@rishabh-gupta2
rishabh-gupta2 / outbox_outboxer.go
Created March 30, 2021 11:57
Outboxer interface
type Outboxer interface {
// Send expects an active gorm transaction alongwith
// a registered JobHandler and creates a new outbox job.
Send(context.Context, *gorm.DB, JobHandler) error
// Start initiates fetcher and processor goroutines to pick up and
// process persisted outbox jobs.
Start(context.Context)
// Stop gracefully terminates all the outbox goroutines.
@rishabh-gupta2
rishabh-gupta2 / outbox_encoder.go
Created March 30, 2021 11:58
Encoder interface
// Encoder holds the details on JobHandler which are registered with Outboxer.
// and is responsible for serialization and deserialization of JobHandler.
type Encoder interface {
// Encode serialize any registered interface to a string.
Encode(handler interface{}) (string, error)
// Decode deserialize a valid encoded string to its interface.
Decode(content string) (interface{}, error)
// Register registers interfaces which will be serialized.
@rishabh-gupta2
rishabh-gupta2 / outbox_job.go
Created March 30, 2021 12:00
Outbox Job struct
type Job struct {
Id string `json:"id"`
CreatedAt int64 `json:"created_at"`
UpdatedAt int64 `json:"updated_at"`
HandlerName string `json:"handler_name"`
Status JobStatus `json:"status" sql:"DEFAULT:pending"`
HandlerSerialized string `json:"handler_serialized"`
NextAttemptAt int64 `json:"next_attempt_at"`
NumAttempts int `json:"num_attempts"`
}
// Config holds configs for outboxer.
type Config struct {
// ProcessorPoolSize is the number of processor goroutines
// which will Process outboxed jobs.
ProcessorPoolSize int
// JobProcessingTimeoutMs is the maximum time a job is expected to take
// time to process. After which it will be picked again for processing.
JobProcessingTimeoutMs int32

Keybase proof

I hereby claim:

  • I am rishabh-gupta2 on github.
  • I am rishabhgupta2 (https://keybase.io/rishabhgupta2) on keybase.
  • I have a public key ASBs3BJ5ukrmij39wb9afa2sQz-KP1sEDxTuUIPAJQiRIgo

To claim this, I am signing this object: