Documentation ¶
Overview ¶
Package lro provides a universal implementation of longrunning.OperationsServer, and helper functions for dealing with long-running operations.
Example ¶
package main import ( "context" "net" "infra/libs/lro" "go.chromium.org/chromiumos/config/go/api/test/tls" "go.chromium.org/chromiumos/config/go/api/test/tls/dependencies/longrunning" "google.golang.org/grpc" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" ) type exampleServer struct { tls.UnimplementedCommonServer *lro.Manager } func (s *exampleServer) Serve(l net.Listener) error { s.Manager = lro.New() defer s.Manager.Close() server := grpc.NewServer() tls.RegisterCommonServer(server, s) longrunning.RegisterOperationsServer(server, s.Manager) return server.Serve(l) } func (s *exampleServer) ProvisionDut(ctx context.Context, req *tls.ProvisionDutRequest) (*longrunning.Operation, error) { op := s.Manager.NewOperation() go s.provision(ctx, req, op.Name) return op, nil } func (s *exampleServer) provision(ctx context.Context, req *tls.ProvisionDutRequest, op string) { if req.GetName() != "some host" { s.Manager.SetError(op, status.Newf(codes.NotFound, "Unknown DUT %s", req.GetName())) return } s.Manager.SetResult(op, &tls.ProvisionDutResponse{}) } func main() { l, err := net.Listen("tcp", ":0") if err != nil { panic(err) } s := exampleServer{} if err := s.Serve(l); err != nil { panic(err) } }
Output:
Index ¶
- func Wait(ctx context.Context, client longrunning.OperationsClient, name string, ...) (*longrunning.Operation, error)
- type Manager
- func (m *Manager) Close()
- func (m *Manager) DeleteOperation(ctx context.Context, req *longrunning.DeleteOperationRequest) (*empty.Empty, error)
- func (m *Manager) GetOperation(ctx context.Context, req *longrunning.GetOperationRequest) (*longrunning.Operation, error)
- func (m *Manager) NewOperation() *longrunning.Operation
- func (m *Manager) SetError(name string, opErr *status.Status) error
- func (m *Manager) SetResult(name string, resp proto.Message) error
- func (m *Manager) WaitOperation(ctx context.Context, req *longrunning.WaitOperationRequest) (*longrunning.Operation, error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Wait ¶
func Wait(ctx context.Context, client longrunning.OperationsClient, name string, opts ...grpc.CallOption) (*longrunning.Operation, error)
Wait waits until the long-running operation specified by the provided operation name is done. If the operation is already done, it returns immediately. Unlike OperationsClient's WaitOperation(), it only returns on context timeout or completion of the operation.
Types ¶
type Manager ¶
type Manager struct { // Provide stubs for unimplemented methods longrunning.UnimplementedOperationsServer // contains filtered or unexported fields }
Manager keeps track of longrunning operations and serves operations related requests. Manager implements longrunning.OperationsServer. Manager is safe to use concurrently. Finished operations are expired after 30 days.
func (*Manager) DeleteOperation ¶
func (m *Manager) DeleteOperation(ctx context.Context, req *longrunning.DeleteOperationRequest) (*empty.Empty, error)
DeleteOperation deletes the longrunning.Operation if managed.
func (*Manager) GetOperation ¶
func (m *Manager) GetOperation(ctx context.Context, req *longrunning.GetOperationRequest) (*longrunning.Operation, error)
GetOperation returns the longrunning.Operation if managed.
func (*Manager) NewOperation ¶
func (m *Manager) NewOperation() *longrunning.Operation
NewOperation returns a new longrunning.Operation managed by Manager. The caller should return this directly from the gRPC method without modifying it or inspecting it, except to read the Name field.
func (*Manager) SetError ¶
SetError sets the operation with the given name to done with Operation error. After calling this method, the caller must not mutate or read the passed-in argument as the manager must ensure safe concurrent access.
func (*Manager) SetResult ¶
SetResult sets the operation with the given name to done with Operation response. After calling this method, the caller must not mutate or read the passed-in argument as the manager must ensure safe concurrent access.
func (*Manager) WaitOperation ¶
func (m *Manager) WaitOperation(ctx context.Context, req *longrunning.WaitOperationRequest) (*longrunning.Operation, error)
WaitOperation returns once the longrunning.Operation is done or timeout.