-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
push retry_sender bytes_writer retry_write files
- Loading branch information
Showing
3 changed files
with
90 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package mq | ||
|
||
import "context" | ||
|
||
type BytesWriter struct { | ||
Send func(ctx context.Context, data []byte, attributes map[string]string) (string, error) | ||
} | ||
func NewBytesWriter(send func(ctx context.Context, data []byte, attributes map[string]string) (string, error)) *BytesWriter { | ||
return &BytesWriter{Send: send} | ||
} | ||
func (w *BytesWriter) Write(ctx context.Context, model interface{}) error { | ||
data := model.([]byte) | ||
_, err := w.Send(ctx, data, nil) | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package mq | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
) | ||
|
||
type RetryWriter struct { | ||
write func(ctx context.Context, model interface{}) error | ||
Retries []time.Duration | ||
Log func(context.Context, string) | ||
Goroutines bool | ||
} | ||
|
||
func NewWriterByConfig(write func(context.Context, interface{}) error, goroutines bool, log func(context.Context, string), c *RetryConfig) *RetryWriter { | ||
if c == nil { | ||
return &RetryWriter{write: write, Log: log, Goroutines: goroutines} | ||
} else { | ||
retries := DurationsFromValue(*c, "Retry", 20) | ||
if len(retries) == 0 { | ||
return &RetryWriter{write: write, Log: log, Goroutines: goroutines} | ||
} | ||
return &RetryWriter{write: write, Log: log, Retries: retries, Goroutines: goroutines} | ||
} | ||
} | ||
func NewWriter(write func(context.Context, interface{}) error, goroutines bool, log func(context.Context, string), retries ...time.Duration) *RetryWriter { | ||
return &RetryWriter{write: write, Log: log, Retries: retries, Goroutines: goroutines} | ||
} | ||
func (c *RetryWriter) Write(ctx context.Context, model interface{}) error { | ||
if !c.Goroutines { | ||
return WriteTo(ctx, c.write, model, c.Log, c.Retries...) | ||
} else { | ||
go WriteTo(ctx, c.write, model, c.Log, c.Retries...) | ||
return nil | ||
} | ||
} | ||
func WriteTo(ctx context.Context, write func(context.Context, interface{}) error, model interface{}, log func(context.Context, string), retries ...time.Duration) error { | ||
l := len(retries) | ||
if l == 0 { | ||
return write(ctx, model) | ||
} else { | ||
return WriteWithRetries(ctx, write, model, retries, log) | ||
} | ||
} | ||
|
||
func WriteWithRetries(ctx context.Context, write func(context.Context, interface{}) error, model interface{}, retries []time.Duration, log func(context.Context, string)) error { | ||
er1 := write(ctx, model) | ||
if er1 == nil { | ||
return er1 | ||
} | ||
i := 0 | ||
err := Retry(ctx, retries, func() (err error) { | ||
i = i + 1 | ||
er2 := write(ctx, model) | ||
|
||
if er2 == nil && log != nil { | ||
log(ctx, fmt.Sprintf("Write successfully after %d retries %s", i, model)) | ||
} | ||
return er2 | ||
}, log) | ||
if err != nil && log != nil { | ||
log(ctx, fmt.Sprintf("Failed to write after %d retries: %s. Error: %s.", len(retries), model, err.Error())) | ||
} | ||
return err | ||
} |