github.com/asynkron/protoactor-go@v0.0.0-20240308120642-ef91a6abee75/protobuf/protoc-gen-go-grain/test/hello/hello_grain.pb.go (about)

     1  // Code generated by protoc-gen-grain. DO NOT EDIT.
     2  // versions:
     3  //  protoc-gen-grain v0.6.0
     4  //  protoc           v4.25.0
     5  // source: test/hello/hello.proto
     6  
     7  package hello
     8  
     9  import (
    10  	fmt "fmt"
    11  	actor "github.com/asynkron/protoactor-go/actor"
    12  	cluster "github.com/asynkron/protoactor-go/cluster"
    13  	proto "google.golang.org/protobuf/proto"
    14  	emptypb "google.golang.org/protobuf/types/known/emptypb"
    15  	slog "log/slog"
    16  	time "time"
    17  )
    18  
    19  var xHelloFactory func() Hello
    20  
    21  // HelloFactory produces a Hello
    22  func HelloFactory(factory func() Hello) {
    23  	xHelloFactory = factory
    24  }
    25  
    26  // GetHelloGrainClient instantiates a new HelloGrainClient with given Identity
    27  func GetHelloGrainClient(c *cluster.Cluster, id string) *HelloGrainClient {
    28  	if c == nil {
    29  		panic(fmt.Errorf("nil cluster instance"))
    30  	}
    31  	if id == "" {
    32  		panic(fmt.Errorf("empty id"))
    33  	}
    34  	return &HelloGrainClient{Identity: id, cluster: c}
    35  }
    36  
    37  // GetHelloKind instantiates a new cluster.Kind for Hello
    38  func GetHelloKind(opts ...actor.PropsOption) *cluster.Kind {
    39  	props := actor.PropsFromProducer(func() actor.Actor {
    40  		return &HelloActor{
    41  			Timeout: 60 * time.Second,
    42  		}
    43  	}, opts...)
    44  	kind := cluster.NewKind("Hello", props)
    45  	return kind
    46  }
    47  
    48  // GetHelloKind instantiates a new cluster.Kind for Hello
    49  func NewHelloKind(factory func() Hello, timeout time.Duration, opts ...actor.PropsOption) *cluster.Kind {
    50  	xHelloFactory = factory
    51  	props := actor.PropsFromProducer(func() actor.Actor {
    52  		return &HelloActor{
    53  			Timeout: timeout,
    54  		}
    55  	}, opts...)
    56  	kind := cluster.NewKind("Hello", props)
    57  	return kind
    58  }
    59  
    60  // Hello interfaces the services available to the Hello
    61  type Hello interface {
    62  	Init(ctx cluster.GrainContext)
    63  	Terminate(ctx cluster.GrainContext)
    64  	ReceiveDefault(ctx cluster.GrainContext)
    65  	SayHello(req *emptypb.Empty, ctx cluster.GrainContext) (*SayHelloResponse, error)
    66  }
    67  
    68  // HelloGrainClient holds the base data for the HelloGrain
    69  type HelloGrainClient struct {
    70  	Identity string
    71  	cluster  *cluster.Cluster
    72  }
    73  
    74  // SayHello requests the execution on to the cluster with CallOptions
    75  func (g *HelloGrainClient) SayHello(r *emptypb.Empty, opts ...cluster.GrainCallOption) (*SayHelloResponse, error) {
    76  	bytes, err := proto.Marshal(r)
    77  	if err != nil {
    78  		return nil, err
    79  	}
    80  	reqMsg := &cluster.GrainRequest{MethodIndex: 0, MessageData: bytes}
    81  	resp, err := g.cluster.Request(g.Identity, "Hello", reqMsg, opts...)
    82  	if err != nil {
    83  		return nil, fmt.Errorf("error request: %w", err)
    84  	}
    85  	switch msg := resp.(type) {
    86  	case *SayHelloResponse:
    87  		return msg, nil
    88  	case *cluster.GrainErrorResponse:
    89  		if msg == nil {
    90  			return nil, nil
    91  		}
    92  		return nil, msg
    93  	default:
    94  		return nil, fmt.Errorf("unknown response type %T", resp)
    95  	}
    96  }
    97  
    98  // HelloActor represents the actor structure
    99  type HelloActor struct {
   100  	ctx     cluster.GrainContext
   101  	inner   Hello
   102  	Timeout time.Duration
   103  }
   104  
   105  // Receive ensures the lifecycle of the actor for the received message
   106  func (a *HelloActor) Receive(ctx actor.Context) {
   107  	switch msg := ctx.Message().(type) {
   108  	case *actor.Started: //pass
   109  	case *cluster.ClusterInit:
   110  		a.ctx = cluster.NewGrainContext(ctx, msg.Identity, msg.Cluster)
   111  		a.inner = xHelloFactory()
   112  		a.inner.Init(a.ctx)
   113  
   114  		if a.Timeout > 0 {
   115  			ctx.SetReceiveTimeout(a.Timeout)
   116  		}
   117  	case *actor.ReceiveTimeout:
   118  		ctx.Poison(ctx.Self())
   119  	case *actor.Stopped:
   120  		a.inner.Terminate(a.ctx)
   121  	case actor.AutoReceiveMessage: // pass
   122  	case actor.SystemMessage: // pass
   123  
   124  	case *cluster.GrainRequest:
   125  		switch msg.MethodIndex {
   126  		case 0:
   127  			req := &emptypb.Empty{}
   128  			err := proto.Unmarshal(msg.MessageData, req)
   129  			if err != nil {
   130  				ctx.Logger().Error("[Grain] SayHello(emptypb.Empty) proto.Unmarshal failed.", slog.Any("error", err))
   131  				resp := cluster.NewGrainErrorResponse(cluster.ErrorReason_INVALID_ARGUMENT, err.Error()).
   132  					WithMetadata(map[string]string{
   133  						"argument": req.String(),
   134  					})
   135  				ctx.Respond(resp)
   136  				return
   137  			}
   138  
   139  			r0, err := a.inner.SayHello(req, a.ctx)
   140  			if err != nil {
   141  				resp := cluster.FromError(err)
   142  				ctx.Respond(resp)
   143  				return
   144  			}
   145  			ctx.Respond(r0)
   146  		}
   147  	default:
   148  		a.inner.ReceiveDefault(a.ctx)
   149  	}
   150  }
   151  
   152  // onError should be used in ctx.ReenterAfter
   153  // you can just return error in reenterable method for other errors
   154  func (a *HelloActor) onError(err error) {
   155  	resp := cluster.FromError(err)
   156  	a.ctx.Respond(resp)
   157  }
   158  
   159  func respond[T proto.Message](ctx cluster.GrainContext) func(T) {
   160  	return func(resp T) {
   161  		ctx.Respond(resp)
   162  	}
   163  }