Skip to content

Commit 4935b97

Browse files
committed
Add context to client/server APIs and timeout configurations
1 parent 84066ca commit 4935b97

15 files changed

Lines changed: 137 additions & 49 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func main() {
8585

8686
proto.RegisterSumServer(server.Server(), &sumServer{})
8787

88-
if err = server.Serve(); err != nil {
88+
if err = server.Serve(context.Background()); err != nil {
8989
panic(err)
9090
}
9191
}
@@ -128,7 +128,7 @@ func main() {
128128
panic(err)
129129
}
130130

131-
client, err := rpcp.NewClient("myServerName")
131+
client, err := rpcp.NewClient(context.Background(), "myServerName")
132132
if err != nil {
133133
panic(err)
134134
}

examples/attributes/client/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func main() {
6161
}
6262
}()
6363

64-
client, err := rpcp.NewClient("myServerName", rpcplatform.ClientOptions.MaxActiveServers(2))
64+
client, err := rpcp.NewClient(context.Background(), "myServerName", rpcplatform.ClientOptions.MaxActiveServers(2))
6565
if err != nil {
6666
panic(err)
6767
}

examples/attributes/server/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func main() {
7272

7373
proto.RegisterSumServer(server.Server(), &sumServer{})
7474

75-
if err = server.Serve(); err != nil {
75+
if err = server.Serve(context.Background()); err != nil {
7676
panic(err)
7777
}
7878
}

examples/opentelemetry/client/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func main() {
6464
panic(err)
6565
}
6666

67-
client, err := rpcp.NewClient("myServerName")
67+
client, err := rpcp.NewClient(context.Background(), "myServerName")
6868
if err != nil {
6969
panic(err)
7070
}

examples/opentelemetry/server/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func main() {
8585

8686
proto.RegisterSumServer(server.Server(), &sumServer{})
8787

88-
if err = server.Serve(); err != nil {
88+
if err = server.Serve(context.Background()); err != nil {
8989
panic(err)
9090
}
9191
}

examples/quickstart/client/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func main() {
4848
panic(err)
4949
}
5050

51-
client, err := rpcp.NewClient("myServerName")
51+
client, err := rpcp.NewClient(context.Background(), "myServerName")
5252
if err != nil {
5353
panic(err)
5454
}

examples/quickstart/server/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func main() {
6969

7070
proto.RegisterSumServer(server.Server(), &sumServer{})
7171

72-
if err = server.Serve(); err != nil {
72+
if err = server.Serve(context.Background()); err != nil {
7373
panic(err)
7474
}
7575
}

internal/config/client.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,19 @@
1717
package config
1818

1919
import (
20+
"time"
21+
2022
"google.golang.org/grpc"
2123
)
2224

2325
func NewClient() *Client {
24-
return &Client{}
26+
return &Client{
27+
EtcdClientTimeout: 5 * time.Second,
28+
}
2529
}
2630

2731
type Client struct {
28-
MaxActiveServers int
29-
GRPCOptions []grpc.DialOption
32+
MaxActiveServers int
33+
EtcdClientTimeout time.Duration
34+
GRPCOptions []grpc.DialOption
3035
}

internal/config/server.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,25 @@
1717
package config
1818

1919
import (
20+
"time"
21+
2022
"github.com/nexcode/rpcplatform/internal/attributes"
2123
"google.golang.org/grpc"
2224
)
2325

2426
func NewServer() *Server {
2527
return &Server{
26-
Attributes: attributes.New(),
28+
EtcdClientTimeout: 5 * time.Second,
29+
EtcdLeaseTimeout: 5 * time.Second,
30+
Attributes: attributes.New(),
2731
}
2832
}
2933

3034
type Server struct {
31-
PublicAddr string
32-
Attributes *attributes.Attributes
33-
GRPCOptions []grpc.ServerOption
35+
PublicAddr string
36+
StopTimeout time.Duration
37+
EtcdClientTimeout time.Duration
38+
EtcdLeaseTimeout time.Duration
39+
Attributes *attributes.Attributes
40+
GRPCOptions []grpc.ServerOption
3441
}

internal/gears/сontextеimeout.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package gears
2+
3+
import (
4+
"context"
5+
"time"
6+
)
7+
8+
func ContextTimeout(ctx context.Context, timeout time.Duration) (context.Context, context.CancelFunc) {
9+
if timeout > 0 {
10+
return context.WithTimeout(ctx, timeout)
11+
}
12+
13+
return ctx, func() {}
14+
}

0 commit comments

Comments
 (0)