-
Notifications
You must be signed in to change notification settings - Fork 475
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add grpc hook * add retry/backoff params make streaming RPC call * Update cmd/tusd/cli/flags.go Co-Authored-By: Márk Sági-Kazár <sagikazarmark@users.noreply.github.com> * move one time grpc configuration to `Setup` * remove stream grpc Co-authored-by: Márk Sági-Kazár <sagikazarmark@users.noreply.github.com>
- Loading branch information
1 parent
9c0e0c8
commit 8ef7648
Showing
8 changed files
with
717 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package hooks | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
grpc_retry "github.com/grpc-ecosystem/go-grpc-middleware/retry" | ||
"github.com/tus/tusd/pkg/handler" | ||
pb "github.com/tus/tusd/pkg/proto/v1" | ||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/status" | ||
) | ||
|
||
type GrpcHook struct { | ||
Endpoint string | ||
MaxRetries int | ||
Backoff int | ||
Client pb.HookServiceClient | ||
} | ||
|
||
func (g GrpcHook) Setup() error { | ||
opts := []grpc_retry.CallOption{ | ||
grpc_retry.WithBackoff(grpc_retry.BackoffLinear(time.Duration(g.Backoff) * time.Second)), | ||
grpc_retry.WithMax(uint(g.MaxRetries)), | ||
} | ||
grpcOpts := []grpc.DialOption{ | ||
grpc.WithInsecure(), | ||
grpc.WithUnaryInterceptor(grpc_retry.UnaryClientInterceptor(opts...)), | ||
} | ||
conn, err := grpc.Dial(g.Endpoint, grpcOpts...) | ||
if err != nil { | ||
return err | ||
} | ||
g.Client = pb.NewHookServiceClient(conn) | ||
return nil | ||
} | ||
|
||
func (g GrpcHook) InvokeHook(typ HookType, info handler.HookEvent, captureOutput bool) ([]byte, int, error) { | ||
ctx := context.Background() | ||
req := &pb.SendRequest{Hook: marshal(info)} | ||
resp, err := g.Client.Send(ctx, req) | ||
if err != nil { | ||
if e, ok := status.FromError(err); ok { | ||
return nil, int(e.Code()), err | ||
} | ||
return nil, 2, err | ||
} | ||
if captureOutput { | ||
return resp.Response.GetValue(), 0, err | ||
} | ||
return nil, 0, err | ||
} | ||
|
||
func marshal(info handler.HookEvent) *pb.Hook { | ||
return &pb.Hook{ | ||
Upload: &pb.Upload{ | ||
Id: info.Upload.ID, | ||
Size: info.Upload.Size, | ||
SizeIsDeferred: info.Upload.SizeIsDeferred, | ||
Offset: info.Upload.Offset, | ||
MetaData: info.Upload.MetaData, | ||
IsPartial: info.Upload.IsPartial, | ||
IsFinal: info.Upload.IsFinal, | ||
PartialUploads: info.Upload.PartialUploads, | ||
Storage: info.Upload.Storage, | ||
}, | ||
HttpRequest: &pb.HTTPRequest{ | ||
Method: info.HTTPRequest.Method, | ||
Uri: info.HTTPRequest.URI, | ||
RemoteAddr: info.HTTPRequest.RemoteAddr, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
syntax = "proto3"; | ||
package v1; | ||
|
||
import "google/protobuf/any.proto"; | ||
|
||
// Uploaded data | ||
message Upload { | ||
// Unique integer identifier of the uploaded file | ||
string id = 1; | ||
// Total file size in bytes specified in the NewUpload call | ||
int64 Size = 2; | ||
// Indicates whether the total file size is deferred until later | ||
bool SizeIsDeferred = 3; | ||
// Offset in bytes (zero-based) | ||
int64 Offset = 4; | ||
map<string, string> metaData = 5; | ||
// Indicates that this is a partial upload which will later be used to form | ||
// a final upload by concatenation. Partial uploads should not be processed | ||
// when they are finished since they are only incomplete chunks of files. | ||
bool isPartial = 6; | ||
// Indicates that this is a final upload | ||
bool isFinal = 7; | ||
// If the upload is a final one (see IsFinal) this will be a non-empty | ||
// ordered slice containing the ids of the uploads of which the final upload | ||
// will consist after concatenation. | ||
repeated string partialUploads = 8; | ||
// Storage contains information about where the data storage saves the upload, | ||
// for example a file path. The available values vary depending on what data | ||
// store is used. This map may also be nil. | ||
map <string, string> storage = 9; | ||
} | ||
|
||
message HTTPRequest { | ||
// Method is the HTTP method, e.g. POST or PATCH | ||
string method = 1; | ||
// URI is the full HTTP request URI, e.g. /files/fooo | ||
string uri = 2; | ||
// RemoteAddr contains the network address that sent the request | ||
string remoteAddr = 3; | ||
} | ||
|
||
// Hook's data | ||
message Hook { | ||
// Upload contains information about the upload that caused this hook | ||
// to be fired. | ||
Upload upload = 1; | ||
// HTTPRequest contains details about the HTTP request that reached | ||
// tusd. | ||
HTTPRequest httpRequest = 2; | ||
} | ||
|
||
// Request data to send hook | ||
message SendRequest { | ||
// The hook data | ||
Hook hook = 1; | ||
} | ||
|
||
// Response that contains data for sended hook | ||
message SendResponse { | ||
// The response of the hook. | ||
google.protobuf.Any response = 1; | ||
} | ||
|
||
// The hook service definition. | ||
service HookService { | ||
// Sends a hook | ||
rpc Send (SendRequest) returns (SendResponse) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.