-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Diego Balduini
authored and
Diego Balduini
committed
Apr 14, 2020
1 parent
468e2b6
commit 34a64e9
Showing
15 changed files
with
266 additions
and
241 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
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
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,61 @@ | ||
package fifo | ||
|
||
import ( | ||
"bytes" | ||
"encoding/base64" | ||
"encoding/gob" | ||
"hash/fnv" | ||
"io" | ||
"strings" | ||
) | ||
|
||
// Job holds data for creating jobs. | ||
// A job defines something that will be queued and dequeued. In HA mode, a job | ||
// will also be uniform partitioned on shards. | ||
type Job struct { | ||
// ID is the job unique id. Set by the server. | ||
ID string | ||
// PartitionKey is the partition id this job belongs to. Set by the server. | ||
PartitionKey int | ||
// PrimaryKey is the primary key. | ||
// It is used to guarantee the order of the requests and balance the work load. | ||
PrimaryKey string `json:"primary_key"` | ||
// CallbackURL is the service endpoint to send the payload back to. | ||
CallbackURL string `json:"callback_url"` | ||
// CorrelationID is a unique identifier attached to the request by the client that allow | ||
// reference to a particular transaction or event chain. | ||
CorrelationID string `json:"correlation_id"` | ||
// Payload with the content encoded as base64 that will be forwarded on the client on the callback. | ||
Payload string `json:"payload"` | ||
} | ||
|
||
// Hash returns hash code for the primary key | ||
func (j Job) Hash() int { | ||
h := fnv.New32a() | ||
h.Write([]byte(j.PrimaryKey)) | ||
return int(h.Sum32()) | ||
} | ||
|
||
func (j Job) NewPayloadReader() io.Reader { | ||
return strings.NewReader(j.Payload) | ||
} | ||
|
||
// Unpack the job from the queue after dequeue. | ||
func Unpack(pack string, job *Job) error { | ||
buf, err := base64.StdEncoding.DecodeString(pack) | ||
if err != nil { | ||
return err | ||
} | ||
d := gob.NewDecoder(bytes.NewBuffer(buf)) | ||
return d.Decode(job) | ||
} | ||
|
||
// Pack the job as base64 string to be enqueued | ||
func Pack(job Job) (string, error) { | ||
var buf bytes.Buffer | ||
e := gob.NewEncoder(&buf) | ||
if err := e.Encode(job); err != nil { | ||
return "", err | ||
} | ||
return base64.StdEncoding.EncodeToString(buf.Bytes()), nil | ||
} |
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
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 |
---|---|---|
|
@@ -4,4 +4,5 @@ go 1.14 | |
|
||
require ( | ||
github.com/go-redis/redis v6.15.7+incompatible | ||
github.com/satori/go.uuid v1.2.0 | ||
) |
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 |
---|---|---|
@@ -1,2 +1,4 @@ | ||
github.com/go-redis/redis v6.15.7+incompatible h1:3skhDh95XQMpnqeqNftPkQD9jL9e5e36z/1SUm6dy1U= | ||
github.com/go-redis/redis v6.15.7+incompatible/go.mod h1:NAIEuMOZ/fxfXJIrKDQDz8wamY7mA7PouImQ2Jvg6kA= | ||
github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww= | ||
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0= |
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
Oops, something went wrong.