github.com/asynkron/protoactor-go@v0.0.0-20240308120642-ef91a6abee75/protobuf/protoc-gen-go-grain/test/reenter/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/reenter/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  	slog "log/slog"
    15  	time "time"
    16  )
    17  
    18  var xHelloFactory func() Hello
    19  
    20  // HelloFactory produces a Hello
    21  func HelloFactory(factory func() Hello) {
    22  	xHelloFactory = factory
    23  }
    24  
    25  // GetHelloGrainClient instantiates a new HelloGrainClient with given Identity
    26  func GetHelloGrainClient(c *cluster.Cluster, id string) *HelloGrainClient {
    27  	if c == nil {
    28  		panic(fmt.Errorf("nil cluster instance"))
    29  	}
    30  	if id == "" {
    31  		panic(fmt.Errorf("empty id"))
    32  	}
    33  	return &HelloGrainClient{Identity: id, cluster: c}
    34  }
    35  
    36  // GetHelloKind instantiates a new cluster.Kind for Hello
    37  func GetHelloKind(opts ...actor.PropsOption) *cluster.Kind {
    38  	props := actor.PropsFromProducer(func() actor.Actor {
    39  		return &HelloActor{
    40  			Timeout: 60 * time.Second,
    41  		}
    42  	}, opts...)
    43  	kind := cluster.NewKind("Hello", props)
    44  	return kind
    45  }
    46  
    47  // GetHelloKind instantiates a new cluster.Kind for Hello
    48  func NewHelloKind(factory func() Hello, timeout time.Duration, opts ...actor.PropsOption) *cluster.Kind {
    49  	xHelloFactory = factory
    50  	props := actor.PropsFromProducer(func() actor.Actor {
    51  		return &HelloActor{
    52  			Timeout: timeout,
    53  		}
    54  	}, opts...)
    55  	kind := cluster.NewKind("Hello", props)
    56  	return kind
    57  }
    58  
    59  // Hello interfaces the services available to the Hello
    60  type Hello interface {
    61  	Init(ctx cluster.GrainContext)
    62  	Terminate(ctx cluster.GrainContext)
    63  	ReceiveDefault(ctx cluster.GrainContext)
    64  	SayHello(req *SayHelloRequest, respond func(*SayHelloResponse), onError func(error), ctx cluster.GrainContext) error
    65  	Dowork(req *DoworkRequest, ctx cluster.GrainContext) (*DoworkResponse, 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 *SayHelloRequest, 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  // DoworkFuture return a future for the execution of Dowork on the cluster
    99  func (g *HelloGrainClient) DoworkFuture(r *DoworkRequest, opts ...cluster.GrainCallOption) (*actor.Future, error) {
   100  	bytes, err := proto.Marshal(r)
   101  	if err != nil {
   102  		return nil, err
   103  	}
   104  
   105  	reqMsg := &cluster.GrainRequest{MethodIndex: 1, MessageData: bytes}
   106  	f, err := g.cluster.RequestFuture(g.Identity, "Hello", reqMsg, opts...)
   107  	if err != nil {
   108  		return nil, fmt.Errorf("error request future: %w", err)
   109  	}
   110  
   111  	return f, nil
   112  }
   113  
   114  // Dowork requests the execution on to the cluster with CallOptions
   115  func (g *HelloGrainClient) Dowork(r *DoworkRequest, opts ...cluster.GrainCallOption) (*DoworkResponse, error) {
   116  	bytes, err := proto.Marshal(r)
   117  	if err != nil {
   118  		return nil, err
   119  	}
   120  	reqMsg := &cluster.GrainRequest{MethodIndex: 1, MessageData: bytes}
   121  	resp, err := g.cluster.Request(g.Identity, "Hello", reqMsg, opts...)
   122  	if err != nil {
   123  		return nil, fmt.Errorf("error request: %w", err)
   124  	}
   125  	switch msg := resp.(type) {
   126  	case *DoworkResponse:
   127  		return msg, nil
   128  	case *cluster.GrainErrorResponse:
   129  		if msg == nil {
   130  			return nil, nil
   131  		}
   132  		return nil, msg
   133  	default:
   134  		return nil, fmt.Errorf("unknown response type %T", resp)
   135  	}
   136  }
   137  
   138  // HelloActor represents the actor structure
   139  type HelloActor struct {
   140  	ctx     cluster.GrainContext
   141  	inner   Hello
   142  	Timeout time.Duration
   143  }
   144  
   145  // Receive ensures the lifecycle of the actor for the received message
   146  func (a *HelloActor) Receive(ctx actor.Context) {
   147  	switch msg := ctx.Message().(type) {
   148  	case *actor.Started: //pass
   149  	case *cluster.ClusterInit:
   150  		a.ctx = cluster.NewGrainContext(ctx, msg.Identity, msg.Cluster)
   151  		a.inner = xHelloFactory()
   152  		a.inner.Init(a.ctx)
   153  
   154  		if a.Timeout > 0 {
   155  			ctx.SetReceiveTimeout(a.Timeout)
   156  		}
   157  	case *actor.ReceiveTimeout:
   158  		ctx.Poison(ctx.Self())
   159  	case *actor.Stopped:
   160  		a.inner.Terminate(a.ctx)
   161  	case actor.AutoReceiveMessage: // pass
   162  	case actor.SystemMessage: // pass
   163  
   164  	case *cluster.GrainRequest:
   165  		switch msg.MethodIndex {
   166  		case 0:
   167  			req := &SayHelloRequest{}
   168  			err := proto.Unmarshal(msg.MessageData, req)
   169  			if err != nil {
   170  				ctx.Logger().Error("[Grain] SayHello(SayHelloRequest) proto.Unmarshal failed.", slog.Any("error", err))
   171  				resp := cluster.NewGrainErrorResponse(cluster.ErrorReason_INVALID_ARGUMENT, err.Error()).
   172  					WithMetadata(map[string]string{
   173  						"argument": req.String(),
   174  					})
   175  				ctx.Respond(resp)
   176  				return
   177  			}
   178  			err = a.inner.SayHello(req, respond[*SayHelloResponse](a.ctx), a.onError, a.ctx)
   179  			if err != nil {
   180  				resp := cluster.FromError(err)
   181  				ctx.Respond(resp)
   182  				return
   183  			}
   184  		case 1:
   185  			req := &DoworkRequest{}
   186  			err := proto.Unmarshal(msg.MessageData, req)
   187  			if err != nil {
   188  				ctx.Logger().Error("[Grain] Dowork(DoworkRequest) proto.Unmarshal failed.", slog.Any("error", err))
   189  				resp := cluster.NewGrainErrorResponse(cluster.ErrorReason_INVALID_ARGUMENT, err.Error()).
   190  					WithMetadata(map[string]string{
   191  						"argument": req.String(),
   192  					})
   193  				ctx.Respond(resp)
   194  				return
   195  			}
   196  
   197  			r0, err := a.inner.Dowork(req, a.ctx)
   198  			if err != nil {
   199  				resp := cluster.FromError(err)
   200  				ctx.Respond(resp)
   201  				return
   202  			}
   203  			ctx.Respond(r0)
   204  		}
   205  	default:
   206  		a.inner.ReceiveDefault(a.ctx)
   207  	}
   208  }
   209  
   210  // onError should be used in ctx.ReenterAfter
   211  // you can just return error in reenterable method for other errors
   212  func (a *HelloActor) onError(err error) {
   213  	resp := cluster.FromError(err)
   214  	a.ctx.Respond(resp)
   215  }
   216  
   217  func respond[T proto.Message](ctx cluster.GrainContext) func(T) {
   218  	return func(resp T) {
   219  		ctx.Respond(resp)
   220  	}
   221  }