lro

package
v0.0.0-...-e560ebb Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 13, 2021 License: BSD-3-Clause Imports: 15 Imported by: 0

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

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Wait

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 New

func New() *Manager

New returns a new Manager which must be closed after use.

func (*Manager) Close

func (m *Manager) Close()

Close will close the Manager.

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

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

func (m *Manager) SetError(name string, opErr *status.Status) error

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

func (m *Manager) SetResult(name string, resp proto.Message) error

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

WaitOperation returns once the longrunning.Operation is done or timeout.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL