Skip to content

Commit

Permalink
setup basic spanner as persistence layer
Browse files Browse the repository at this point in the history
  • Loading branch information
cedricfung committed Jun 25, 2018
1 parent 87f8b48 commit 90c3d18
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 8 deletions.
12 changes: 8 additions & 4 deletions exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
24 changes: 21 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
@@ -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)
}
2 changes: 1 addition & 1 deletion mixin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
20 changes: 20 additions & 0 deletions persistence/action.go
Original file line number Diff line number Diff line change
@@ -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
}
22 changes: 22 additions & 0 deletions persistence/context.go
Original file line number Diff line number Diff line change
@@ -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
}
1 change: 1 addition & 0 deletions persistence/order.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package persistence
1 change: 1 addition & 0 deletions persistence/trade.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package persistence

0 comments on commit 90c3d18

Please sign in to comment.