Commit 8583369d authored by Loïck Bonniot's avatar Loïck Bonniot
Browse files

[c][net] Set default timeout for connection

parent 2b63e8df
Pipeline #624 passed with stage
...@@ -4,7 +4,6 @@ import ( ...@@ -4,7 +4,6 @@ import (
"crypto/sha512" "crypto/sha512"
"io/ioutil" "io/ioutil"
"path/filepath" "path/filepath"
"time"
"dfss/dfssc/common" "dfss/dfssc/common"
"dfss/dfssc/security" "dfss/dfssc/security"
...@@ -80,7 +79,7 @@ func (m *CreateManager) sendRequest() (*api.ErrorCode, error) { ...@@ -80,7 +79,7 @@ func (m *CreateManager) sendRequest() (*api.ErrorCode, error) {
} }
client := api.NewPlatformClient(conn) client := api.NewPlatformClient(conn)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) ctx, cancel := context.WithTimeout(context.Background(), net.DefaultTimeout)
defer cancel() defer cancel()
response, err := client.PostContract(ctx, request) response, err := client.PostContract(ctx, request)
if err != nil { if err != nil {
......
...@@ -2,7 +2,6 @@ package sign ...@@ -2,7 +2,6 @@ package sign
import ( import (
"io/ioutil" "io/ioutil"
"time"
"dfss/dfssc/common" "dfss/dfssc/common"
"dfss/dfssc/security" "dfss/dfssc/security"
...@@ -28,7 +27,7 @@ func FetchContract(fileCA, fileCert, fileKey, addrPort, passphrase, uuid, path s ...@@ -28,7 +27,7 @@ func FetchContract(fileCA, fileCert, fileKey, addrPort, passphrase, uuid, path s
Uuid: uuid, Uuid: uuid,
} }
client := api.NewPlatformClient(conn) client := api.NewPlatformClient(conn)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) ctx, cancel := context.WithTimeout(context.Background(), net.DefaultTimeout)
defer cancel() defer cancel()
response, err := client.GetContract(ctx, request) response, err := client.GetContract(ctx, request)
if err != nil { if err != nil {
......
...@@ -8,6 +8,7 @@ import ( ...@@ -8,6 +8,7 @@ import (
"dfss/dfssc/common" "dfss/dfssc/common"
"dfss/dfssc/security" "dfss/dfssc/security"
pb "dfss/dfssp/api" pb "dfss/dfssp/api"
"dfss/net"
"errors" "errors"
"golang.org/x/net/context" "golang.org/x/net/context"
"google.golang.org/grpc" "google.golang.org/grpc"
...@@ -98,7 +99,7 @@ func (m *AuthManager) sendRequest() (*pb.RegisteredUser, error) { ...@@ -98,7 +99,7 @@ func (m *AuthManager) sendRequest() (*pb.RegisteredUser, error) {
} }
// Stop the context if it takes too long for the platform to answer // Stop the context if it takes too long for the platform to answer
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) ctx, cancel := context.WithTimeout(context.Background(), net.DefaultTimeout)
defer cancel() defer cancel()
response, err := client.Auth(ctx, request) response, err := client.Auth(ctx, request)
if err != nil { if err != nil {
......
...@@ -8,6 +8,7 @@ import ( ...@@ -8,6 +8,7 @@ import (
"dfss/dfssc/common" "dfss/dfssc/common"
"dfss/dfssc/security" "dfss/dfssc/security"
pb "dfss/dfssp/api" pb "dfss/dfssp/api"
"dfss/net"
"golang.org/x/net/context" "golang.org/x/net/context"
"google.golang.org/grpc" "google.golang.org/grpc"
) )
...@@ -150,7 +151,7 @@ func (m *RegisterManager) sendRequest(certRequest string) (*pb.ErrorCode, error) ...@@ -150,7 +151,7 @@ func (m *RegisterManager) sendRequest(certRequest string) (*pb.ErrorCode, error)
} }
// Stop the context if it takes too long for the platform to answer // Stop the context if it takes too long for the platform to answer
ctx, cancel := context.WithTimeout(context.TODO(), 10*time.Second) ctx, cancel := context.WithTimeout(context.Background(), net.DefaultTimeout)
defer cancel() defer cancel()
response, err := client.Register(ctx, request) response, err := client.Register(ctx, request)
if err != nil { if err != nil {
......
...@@ -13,6 +13,9 @@ import ( ...@@ -13,6 +13,9 @@ import (
"google.golang.org/grpc/credentials" "google.golang.org/grpc/credentials"
) )
// DefaultTimeout should be used when a non-critical timeout is used in the application.
const DefaultTimeout = 30 * time.Second
// Connect to a peer. // Connect to a peer.
// //
// Given parameters cert/key/ca are PEM-encoded array of bytes. // Given parameters cert/key/ca are PEM-encoded array of bytes.
...@@ -43,6 +46,7 @@ func Connect(addrPort string, cert *x509.Certificate, key *rsa.PrivateKey, ca *x ...@@ -43,6 +46,7 @@ func Connect(addrPort string, cert *x509.Certificate, key *rsa.PrivateKey, ca *x
return grpc.Dial( return grpc.Dial(
addrPort, addrPort,
grpc.WithTransportCredentials(&tlsCreds{config: conf}), grpc.WithTransportCredentials(&tlsCreds{config: conf}),
grpc.WithTimeout(DefaultTimeout),
) )
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment