From 90c3d18c626b51fb053e0495a1cabca5d4e3118b Mon Sep 17 00:00:00 2001 From: Cedric Fung Date: Mon, 25 Jun 2018 14:23:26 +0900 Subject: [PATCH] setup basic spanner as persistence layer --- exchange.go | 12 ++++++++---- main.go | 24 +++++++++++++++++++++--- mixin.go | 2 +- persistence/action.go | 20 ++++++++++++++++++++ persistence/context.go | 22 ++++++++++++++++++++++ persistence/order.go | 1 + persistence/trade.go | 1 + 7 files changed, 74 insertions(+), 8 deletions(-) create mode 100644 persistence/action.go create mode 100644 persistence/context.go create mode 100644 persistence/order.go create mode 100644 persistence/trade.go diff --git a/exchange.go b/exchange.go index 70573128..7d18564b 100644 --- a/exchange.go +++ b/exchange.go @@ -26,15 +26,15 @@ func NewExchange() *Exchange { func (ex *Exchange) Run(ctx context.Context) { go ex.PollMixinMessages(ctx) - ex.PollMixinNetwork(ctx) + go ex.PollMixinNetwork(ctx) + ex.PollOrderActions(ctx) } -func (ex *Exchange) PollMixinMessages(ctx context.Context) { - bot.Loop(ctx, ex, config.ClientId, config.SessionId, config.SessionKey) +func (ex *Exchange) PollOrderActions(ctx context.Context) { } func (ex *Exchange) PollMixinNetwork(ctx context.Context) { - checkpoint, limit := persistence.ReadLatestAction(ctx).UTC(), 500 + checkpoint, limit := persistence.ReadActionCheckpoint(ctx).UTC(), 500 for { snapshots, err := ex.requestMixinNetwork(ctx, checkpoint, limit) if err != nil { @@ -58,6 +58,10 @@ func (ex *Exchange) PollMixinNetwork(ctx context.Context) { } } +func (ex *Exchange) PollMixinMessages(ctx context.Context) { + bot.Loop(ctx, ex, config.ClientId, config.SessionId, config.SessionKey) +} + func (ex *Exchange) OnMessage(ctx context.Context, mc *bot.MessageContext, msg bot.MessageView, userId string) error { log.Println(msg, userId) return nil diff --git a/main.go b/main.go index ed64bccf..72104a15 100644 --- a/main.go +++ b/main.go @@ -1,8 +1,26 @@ package main -import "context" +import ( + "context" + "log" + "time" + + "cloud.google.com/go/spanner" + "github.com/MixinMessenger/ocean.one/config" + "github.com/MixinMessenger/ocean.one/persistence" +) func main() { - ex := NewExchange() - ex.Run(context.Background()) + ctx := context.Background() + client, err := spanner.NewClientWithConfig(ctx, config.GoogleCloudSpanner, spanner.ClientConfig{NumChannels: 4, + SessionPoolConfig: spanner.SessionPoolConfig{ + HealthCheckInterval: 5 * time.Second, + }, + }) + if err != nil { + log.Panicln(err) + } + + ctx = persistence.SetupSpanner(ctx, client) + NewExchange().Run(ctx) } diff --git a/mixin.go b/mixin.go index 613ff691..53168df0 100644 --- a/mixin.go +++ b/mixin.go @@ -62,7 +62,7 @@ func (ex *Exchange) processSnapshot(ctx context.Context, s *Snapshot) error { return ex.refundSnapshot(ctx, s) } if action.O.String() != uuid.Nil.String() { - return persistence.CancelOrder(ctx, action.O.String()) + return persistence.CancelOrder(ctx, action.O.String(), s.CreatedAt) } amount := number.FromString(s.Amount).RoundFloor(8) diff --git a/persistence/action.go b/persistence/action.go new file mode 100644 index 00000000..db5e4302 --- /dev/null +++ b/persistence/action.go @@ -0,0 +1,20 @@ +package persistence + +import ( + "context" + "time" + + "github.com/MixinMessenger/go-number" +) + +func ReadActionCheckpoint(ctx context.Context) time.Time { + return time.Now() +} + +func CreateOrder(ctx context.Context, userId, traceId string, side, quote, base string, amount, price number.Decimal, createdAt time.Time) error { + return nil +} + +func CancelOrder(ctx context.Context, orderId string, createdAt time.Time) error { + return nil +} diff --git a/persistence/context.go b/persistence/context.go new file mode 100644 index 00000000..8f9b6d1f --- /dev/null +++ b/persistence/context.go @@ -0,0 +1,22 @@ +package persistence + +import ( + "context" + + "cloud.google.com/go/spanner" +) + +type contextValueKey int + +const ( + keySpanner contextValueKey = 1 +) + +func SetupSpanner(ctx context.Context, client *spanner.Client) context.Context { + return context.WithValue(ctx, keySpanner, client) +} + +func Spanner(ctx context.Context) *spanner.Client { + v, _ := ctx.Value(keySpanner).(*spanner.Client) + return v +} diff --git a/persistence/order.go b/persistence/order.go new file mode 100644 index 00000000..dc7cf831 --- /dev/null +++ b/persistence/order.go @@ -0,0 +1 @@ +package persistence diff --git a/persistence/trade.go b/persistence/trade.go new file mode 100644 index 00000000..dc7cf831 --- /dev/null +++ b/persistence/trade.go @@ -0,0 +1 @@ +package persistence