Skip to content

Commit

Permalink
Update to restate 1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
jackkleeman committed Jul 6, 2024
1 parent e47b3f6 commit ed19fa2
Show file tree
Hide file tree
Showing 31 changed files with 2,218 additions and 3,308 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
[![Go Reference](https://pkg.go.dev/badge/github.com/restatedev/sdk-go.svg)](https://pkg.go.dev/github.com/restatedev/sdk-go)
[![Go](https://github.com/restatedev/sdk-go/actions/workflows/test.yaml/badge.svg)](https://github.com/restatedev/sdk-go/actions/workflows/test.yaml)

> This only works with Restate version 0.8
# Restate Go SDK

[Restate](https://restate.dev/) is a system for easily building resilient applications using *distributed durable async/await*. This repository contains the Restate SDK for writing services in **Golang**.
Expand Down
2 changes: 0 additions & 2 deletions proto/buf.yaml → buf.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
version: v1
deps:
- buf.build/restatedev/proto
breaking:
use:
- FILE
Expand Down
Binary file removed descriptor.binpb
Binary file not shown.
6 changes: 3 additions & 3 deletions example/checkout.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"

"github.com/google/uuid"
"github.com/restatedev/sdk-go"
restate "github.com/restatedev/sdk-go"
"github.com/rs/zerolog/log"
)

Expand Down Expand Up @@ -58,6 +58,6 @@ func payment(ctx restate.Context, request PaymentRequest) (response PaymentRespo
}

var (
Checkout = restate.NewUnKeyedRouter().
Handler("checkout", restate.NewUnKeyedHandler(payment))
Checkout = restate.NewServiceRouter().
Handler("checkout", restate.NewServiceHandler(payment))
)
18 changes: 10 additions & 8 deletions example/ticket_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
import (
"errors"

"github.com/restatedev/sdk-go"
restate "github.com/restatedev/sdk-go"
"github.com/rs/zerolog/log"
)

Expand All @@ -15,7 +15,7 @@ const (
TicketSold TicketStatus = 2
)

func reserve(ctx restate.Context, ticketId string, _ restate.Void) (bool, error) {
func reserve(ctx restate.ObjectContext, _ restate.Void) (bool, error) {
status, err := restate.GetAs[TicketStatus](ctx, "status")
if err != nil && !errors.Is(err, restate.ErrKeyNotFound) {
return false, err
Expand All @@ -28,7 +28,8 @@ func reserve(ctx restate.Context, ticketId string, _ restate.Void) (bool, error)
return false, nil
}

func unreserve(ctx restate.Context, ticketId string, _ restate.Void) (void restate.Void, err error) {
func unreserve(ctx restate.ObjectContext, _ restate.Void) (void restate.Void, err error) {
ticketId := ctx.Key()
log.Info().Str("ticket", ticketId).Msg("un-reserving ticket")
status, err := restate.GetAs[TicketStatus](ctx, "status")
if err != nil && !errors.Is(err, restate.ErrKeyNotFound) {
Expand All @@ -42,7 +43,8 @@ func unreserve(ctx restate.Context, ticketId string, _ restate.Void) (void resta
return void, nil
}

func markAsSold(ctx restate.Context, ticketId string, _ restate.Void) (void restate.Void, err error) {
func markAsSold(ctx restate.ObjectContext, _ restate.Void) (void restate.Void, err error) {
ticketId := ctx.Key()
log.Info().Str("ticket", ticketId).Msg("mark ticket as sold")

status, err := restate.GetAs[TicketStatus](ctx, "status")
Expand All @@ -58,8 +60,8 @@ func markAsSold(ctx restate.Context, ticketId string, _ restate.Void) (void rest
}

var (
TicketService = restate.NewKeyedRouter().
Handler("reserve", restate.NewKeyedHandler(reserve)).
Handler("unreserve", restate.NewKeyedHandler(unreserve)).
Handler("markAsSold", restate.NewKeyedHandler(markAsSold))
TicketService = restate.NewObjectRouter().
Handler("reserve", restate.NewObjectHandler(reserve)).
Handler("unreserve", restate.NewObjectHandler(unreserve)).
Handler("markAsSold", restate.NewObjectHandler(markAsSold))
)
32 changes: 17 additions & 15 deletions example/user_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import (
"slices"
"time"

"github.com/restatedev/sdk-go"
restate "github.com/restatedev/sdk-go"
"github.com/rs/zerolog/log"
)

func addTicket(ctx restate.Context, userId, ticketId string) (bool, error) {
func addTicket(ctx restate.ObjectContext, ticketId string) (bool, error) {
userId := ctx.Key()

var success bool
if err := ctx.Service(TicketServiceName).Method("reserve").Do(ticketId, userId, &success); err != nil {
if err := ctx.Object(TicketServiceName, ticketId).Method("reserve").Do(userId, &success); err != nil {
return false, err
}

Expand All @@ -33,14 +34,14 @@ func addTicket(ctx restate.Context, userId, ticketId string) (bool, error) {
return false, err
}

if err := ctx.Service(UserSessionServiceName).Method("expireTicket").Send(userId, ticketId, 15*time.Minute); err != nil {
if err := ctx.Object(UserSessionServiceName, ticketId).Method("expireTicket").Send(ticketId, 15*time.Minute); err != nil {
return false, err
}

return true, nil
}

func expireTicket(ctx restate.Context, _, ticketId string) (void restate.Void, err error) {
func expireTicket(ctx restate.ObjectContext, ticketId string) (void restate.Void, err error) {
tickets, err := restate.GetAs[[]string](ctx, "tickets")
if err != nil && !errors.Is(err, restate.ErrKeyNotFound) {
return void, err
Expand All @@ -62,10 +63,11 @@ func expireTicket(ctx restate.Context, _, ticketId string) (void restate.Void, e
return void, err
}

return void, ctx.Service(TicketServiceName).Method("unreserve").Send(ticketId, nil, 0)
return void, ctx.Object(TicketServiceName, ticketId).Method("unreserve").Send(nil, 0)
}

func checkout(ctx restate.Context, userId string, _ restate.Void) (bool, error) {
func checkout(ctx restate.ObjectContext, _ restate.Void) (bool, error) {
userId := ctx.Key()
tickets, err := restate.GetAs[[]string](ctx, "tickets")
if err != nil && !errors.Is(err, restate.ErrKeyNotFound) {
return false, err
Expand All @@ -78,17 +80,17 @@ func checkout(ctx restate.Context, userId string, _ restate.Void) (bool, error)
}

var response PaymentResponse
if err := ctx.Service(CheckoutServiceName).
if err := ctx.Object(CheckoutServiceName, "").
Method("checkout").
Do("", PaymentRequest{UserID: userId, Tickets: tickets}, &response); err != nil {
Do(PaymentRequest{UserID: userId, Tickets: tickets}, &response); err != nil {
return false, err
}

log.Info().Str("id", response.ID).Int("price", response.Price).Msg("payment details")

call := ctx.Service(TicketServiceName).Method("markAsSold")
for _, ticket := range tickets {
if err := call.Send(ticket, nil, 0); err != nil {
call := ctx.Object(ticket, TicketServiceName).Method("markAsSold")
if err := call.Send(nil, 0); err != nil {
return false, err
}
}
Expand All @@ -97,8 +99,8 @@ func checkout(ctx restate.Context, userId string, _ restate.Void) (bool, error)
}

var (
UserSession = restate.NewKeyedRouter().
Handler("addTicket", restate.NewKeyedHandler(addTicket)).
Handler("expireTicket", restate.NewKeyedHandler(expireTicket)).
Handler("checkout", restate.NewKeyedHandler(checkout))
UserSession = restate.NewObjectRouter().
Handler("addTicket", restate.NewObjectHandler(addTicket)).
Handler("expireTicket", restate.NewObjectHandler(expireTicket)).
Handler("checkout", restate.NewObjectHandler(checkout))
)
1 change: 0 additions & 1 deletion generate.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
package restate

//go:generate buf generate
//go:generate buf build --as-file-descriptor-set -o internal/dynrpc.binbp proto/dynrpc/dynrpc.proto
Loading

0 comments on commit ed19fa2

Please sign in to comment.