Skip to content

Commit

Permalink
context cancellation experiment
Browse files Browse the repository at this point in the history
  • Loading branch information
Tommy Lindgren committed Sep 20, 2022
1 parent f85532f commit acb08bd
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 25 deletions.
37 changes: 25 additions & 12 deletions examples/drpc/client/main.go
Expand Up @@ -6,6 +6,7 @@ package main
import (
"context"
"fmt"
"log"
"net"
"time"

Expand Down Expand Up @@ -37,19 +38,31 @@ func Main(ctx context.Context) error {
// make a drpc proto-specific client
client := pb.NewDRPCCookieMonsterClient(conn)

// set a deadline for the operation
ctx, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()
for _, d := range []time.Duration{1, 3} {
select {
case <-conn.Closed():
log.Print("closed")
default:
log.Print("not closed")
}
func() {
// set a deadline for the operation
ctx, cancel := context.WithTimeout(ctx, d*time.Second)
defer cancel()

// run the RPC
crumbs, err := client.EatCookie(ctx, &pb.Cookie{
Type: pb.Cookie_Oatmeal,
})
if err != nil {
return err
// run the RPC
crumbs, err := client.EatCookie(ctx, &pb.Cookie{
Type: pb.Cookie_Oatmeal,
})
if err != nil {
log.Println(err)
return
}

// check the results
fmt.Println(crumbs.Cookie.Type.String())
}()
}

// check the results
_, err = fmt.Println(crumbs.Cookie.Type.String())
return err
return nil
}
2 changes: 2 additions & 0 deletions examples/drpc/server/main.go
Expand Up @@ -6,6 +6,7 @@ package main
import (
"context"
"net"
"time"

"storj.io/drpc/drpcmux"
"storj.io/drpc/drpcserver"
Expand All @@ -20,6 +21,7 @@ type CookieMonsterServer struct {

// EatCookie turns a cookie into crumbs.
func (s *CookieMonsterServer) EatCookie(ctx context.Context, cookie *pb.Cookie) (*pb.Crumbs, error) {
time.Sleep(2 * time.Second)
return &pb.Crumbs{
Cookie: cookie,
}, nil
Expand Down
34 changes: 21 additions & 13 deletions examples/grpc/client/main.go
Expand Up @@ -6,6 +6,7 @@ package main
import (
"context"
"fmt"
"log"
"time"

"google.golang.org/grpc"
Expand All @@ -31,19 +32,26 @@ func Main(ctx context.Context) error {
// make a grpc proto-specific client
client := pb.NewCookieMonsterClient(conn)

// set a deadline for the operation
ctx, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()

// run the RPC
crumbs, err := client.EatCookie(ctx, &pb.Cookie{
Type: pb.Cookie_Oatmeal,
})
if err != nil {
return err
for _, d := range []time.Duration{1, 3} {
log.Println(conn.GetState())
func() {
// set a deadline for the operation
ctx, cancel := context.WithTimeout(ctx, d*time.Second)
defer cancel()

// run the RPC
crumbs, err := client.EatCookie(ctx, &pb.Cookie{
Type: pb.Cookie_Oatmeal,
})
if err != nil {
log.Println(err)
return
}

// check the results
_, err = fmt.Println(crumbs.Cookie.Type.String())
}()
}

// check the results
_, err = fmt.Println(crumbs.Cookie.Type.String())
return err
return nil
}
2 changes: 2 additions & 0 deletions examples/grpc/server/main.go
Expand Up @@ -6,6 +6,7 @@ package main
import (
"context"
"net"
"time"

"google.golang.org/grpc"

Expand All @@ -19,6 +20,7 @@ type CookieMonsterServer struct {

// EatCookie turns a cookie into crumbs.
func (s *CookieMonsterServer) EatCookie(ctx context.Context, cookie *pb.Cookie) (*pb.Crumbs, error) {
time.Sleep(2 * time.Second)
return &pb.Crumbs{
Cookie: cookie,
}, nil
Expand Down

0 comments on commit acb08bd

Please sign in to comment.