github.com/qiuhoude/go-web@v0.0.0-20220223060959-ab545e78f20d/prepare/23_proto_actor/remoterouting/server/main.go (about)

     1  package main
     2  
     3  import (
     4  	"flag"
     5  	"runtime"
     6  
     7  	"log"
     8  
     9  	console "github.com/AsynkronIT/goconsole"
    10  	"github.com/AsynkronIT/protoactor-go/actor"
    11  	"github.com/AsynkronIT/protoactor-go/examples/remoterouting/messages"
    12  	"github.com/AsynkronIT/protoactor-go/mailbox"
    13  	"github.com/AsynkronIT/protoactor-go/remote"
    14  )
    15  
    16  var (
    17  	flagBind = flag.String("bind", "localhost:8100", "Bind to address")
    18  	flagName = flag.String("name", "node1", "Name")
    19  )
    20  
    21  type remoteActor struct {
    22  	name  string
    23  	count int
    24  }
    25  
    26  func (a *remoteActor) Receive(context actor.Context) {
    27  	switch context.Message().(type) {
    28  	case *messages.Ping:
    29  		context.Respond(&messages.Pong{})
    30  	}
    31  }
    32  
    33  func newRemoteActor(name string) actor.Producer {
    34  	return func() actor.Actor {
    35  		return &remoteActor{
    36  			name: name,
    37  		}
    38  	}
    39  }
    40  
    41  func newRemote(bind, name string) {
    42  	remote.Start(bind)
    43  
    44  	context := actor.EmptyRootContext
    45  	props := actor.
    46  		PropsFromProducer(newRemoteActor(name)).
    47  		WithMailbox(mailbox.Bounded(10000))
    48  
    49  	context.SpawnNamed(props, "remote")
    50  
    51  	log.Println(name, "Ready")
    52  }
    53  
    54  func main() {
    55  	runtime.GOMAXPROCS(runtime.NumCPU())
    56  	runtime.GC()
    57  
    58  	flag.Parse()
    59  
    60  	newRemote(*flagBind, *flagName)
    61  
    62  	console.ReadLine()
    63  }