Skip to content

Commit

Permalink
fixup! Kucoin: Add Subscription batching
Browse files Browse the repository at this point in the history
  • Loading branch information
gbjk committed Aug 5, 2024
1 parent 1e271b4 commit df1a74d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
29 changes: 18 additions & 11 deletions exchanges/subscription/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ const (
)

var (
errInvalidAssetExpandPairs = errors.New("subscription template containing PairSeparator with must contain either specific Asset or AssetSeparator")
errAssetRecords = errors.New("subscription template did not generate the expected number of asset records")
errPairRecords = errors.New("subscription template did not generate the expected number of pair records")
errTooManyBatchSize = errors.New("too many BatchSize directives")
errAssetTemplateWithoutAll = errors.New("sub.Asset must be set to All if AssetSeparator is used in Channel template")
errNoTemplateContent = errors.New("subscription template did not generate content")
errInvalidTemplate = errors.New("GetSubscriptionTemplate did not return a template")
errInvalidAssetExpandPairs = errors.New("subscription template containing PairSeparator with must contain either specific Asset or AssetSeparator")
errAssetRecords = errors.New("subscription template did not generate the expected number of asset records")
errPairRecords = errors.New("subscription template did not generate the expected number of pair records")
errTooManyBatchSizePerAsset = errors.New("more than one BatchSize directive inside an AssetSeparator")
errAssetTemplateWithoutAll = errors.New("sub.Asset must be set to All if AssetSeparator is used in Channel template")
errNoTemplateContent = errors.New("subscription template did not generate content")
errInvalidTemplate = errors.New("GetSubscriptionTemplate did not return a template")
)

type tplCtx struct {
Expand Down Expand Up @@ -148,15 +148,21 @@ func expandTemplate(e iExchange, s *Subscription, ap assetPairs, assets asset.It
a := assets[i]
pairs := subCtx.AssetPairs[a]

batchSize := len(pairs) // Default to all pairs in one batch
/* Batching:
- We start by assuming we'll get 1 batch sized to contain all pairs. Maybe a comma-separated list, or just the asset name
- If a BatchSize directive is found, we expect it to come right at the end, and be followed by the batch size as a number
- We'll then split into N batches of that size
- If no batchSize was declared, but we saw a PairSeparator, then we expect to see one line per pair, so batchSize is 1
*/
batchSize := len(pairs)
if b := strings.Split(assetChannels, subCtx.BatchSize); len(b) > 2 {
return nil, fmt.Errorf("%w for %s", errTooManyBatchSize, a)
} else if len(b) == 2 { // If there's a batch size indicator we batch by that
return nil, fmt.Errorf("%w for %s", errTooManyBatchSizePerAsset, a)
} else if len(b) == 2 {
assetChannels = b[0]
if batchSize, err = strconv.Atoi(strings.TrimSpace(b[1])); err != nil {
return nil, fmt.Errorf("%s: %w", s, common.GetTypeAssertError("int", b[1], "batchSize"))
}
} else if xpandPairs { // expanding pairs but not batching so batch size is 1
} else if xpandPairs {
batchSize = 1
}

Expand All @@ -172,6 +178,7 @@ func expandTemplate(e iExchange, s *Subscription, ap assetPairs, assets asset.It
pairLines := strings.Split(assetChannels, subCtx.PairSeparator)

if s.Asset != asset.Empty && len(pairLines) != len(batches) {
// The number of lines we get generated must match the number of pair batches we expect
return nil, fmt.Errorf("%w for %s: Got %d; Expected %d", errPairRecords, a, len(pairLines), len(batches))
}

Expand Down
2 changes: 1 addition & 1 deletion exchanges/subscription/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func TestExpandTemplates(t *testing.T) {
assert.ErrorIs(t, err, errPairRecords, "Should error correctly when invalid number of pair entries")

_, err = List{{Channel: "error4", Asset: asset.Spot}}.ExpandTemplates(e)
assert.ErrorIs(t, err, errTooManyBatchSize, "Should error correctly when too many BatchSize directives")
assert.ErrorIs(t, err, errTooManyBatchSizePerAsset, "Should error correctly when too many BatchSize directives")

_, err = List{{Channel: "error5", Asset: asset.Spot}}.ExpandTemplates(e)
assert.ErrorIs(t, err, common.ErrTypeAssertFailure, "Should error correctly when batch size isn't an int")
Expand Down

0 comments on commit df1a74d

Please sign in to comment.