func Start(host string, options ...RemotingOption) { lis, err := net.Listen("tcp", host) if err != nil { log.Fatalf("failed to listen: %v", err) } config := defaultRemoteConfig() for _, option := range options { option(config) } host = lis.Addr().String() log.Printf("Host is %v", host) actor.ProcessRegistry.RegisterHostResolver(remoteHandler) actor.ProcessRegistry.Host = host props := actor. FromProducer(newEndpointManager(config)). WithMailbox(actor.NewBoundedMailbox(1000, 100000)) endpointManagerPID = actor.Spawn(props) s := grpc.NewServer(config.serverOptions...) messages.RegisterRemotingServer(s, &server{}) log.Printf("Starting GAM server on %v.", host) go s.Serve(lis) }
func main() { props := actor.FromProducer(NewBecomeActor) pid := actor.Spawn(props) pid.Tell(Hello{Who: "Roger"}) pid.Tell(Hello{Who: "Roger"}) console.ReadLine() }
func main() { props := actor.FromInstance(&HelloActor{}) actor := actor.Spawn(props) actor.Tell(Hello{Who: "Roger"}) //why wait? //Stop is a system message and is not processed through the user message mailbox //thus, it will be handled _before_ any user message //we only do this to show the correct order of events in the console time.Sleep(1 * time.Second) actor.Stop() console.ReadLine() }
func main() { act := func(context actor.Context) { switch context.Message().(type) { case myMessage: log.Printf("%v got message", context.Self()) } } props := actor.FromFunc(act).WithPoolRouter(actor.NewRoundRobinPool(10)) pid := actor.Spawn(props) for i := 0; i < 10; i++ { pid.Tell(myMessage{}) } console.ReadLine() }
func main() { decider := func(child *actor.PID, reason interface{}) actor.Directive { fmt.Println("handling failure for child") return actor.StopDirective } supervisor := actor.NewOneForOneStrategy(10, 1000, decider) props := actor. FromProducer(NewParentActor). WithSupervisor(supervisor) pid := actor.Spawn(props) pid.Tell(Hello{Who: "Roger"}) console.ReadLine() }
func (state *endpointManager) Receive(ctx actor.Context) { switch msg := ctx.Message().(type) { case actor.Started: state.connections = make(map[string]*actor.PID) log.Println("Started EndpointManager") case *messages.MessageEnvelope: pid, ok := state.connections[msg.Target.Host] if !ok { props := actor. FromProducer(newEndpointWriter(msg.Target.Host, state.config)). WithMailbox(newEndpointWriterMailbox(state.config.batchSize, 1000000)) pid = actor.Spawn(props) state.connections[msg.Target.Host] = pid } pid.Tell(msg) } }
func main() { runtime.GOMAXPROCS(runtime.NumCPU()) var wgStop sync.WaitGroup var wgStart sync.WaitGroup messageCount := 1000000 remoting.Start("127.0.0.1:8081") props := actor. FromProducer(newLocalActor(&wgStart, &wgStop, messageCount)). WithMailbox(actor.NewBoundedMailbox(1000, 10000)) pid := actor.Spawn(props) message := &messages.Ping{Sender: pid} remote := actor.NewPID("127.0.0.1:8080", "remote") remote.Tell(&messages.StartRemote{Sender: pid}) wgStart.Wait() start := time.Now() log.Println("Starting to send") for i := 0; i < messageCount; i++ { remote.Tell(message) } wgStop.Wait() elapsed := time.Since(start) log.Printf("Elapsed %s", elapsed) x := int(float32(messageCount*2) / (float32(elapsed) / float32(time.Second))) log.Printf("Msg per sec %v", x) // f, err := os.Create("memprofile") // if err != nil { // log.Fatal(err) // } // pprof.WriteHeapProfile(f) // f.Close() }
func main() { runtime.GOMAXPROCS(runtime.NumCPU()) remoting.Start("127.0.0.1:0") server := actor.NewPID("127.0.0.1:8080", "chatserver") //spawn our chat client inline props := actor.FromFunc(func(context actor.Context) { switch msg := context.Message().(type) { case *messages.Connected: log.Println(msg.Message) case *messages.SayResponse: log.Printf("%v: %v", msg.UserName, msg.Message) case *messages.NickResponse: log.Printf("%v is now known as %v", msg.OldUserName, msg.NewUserName) } }) client := actor.Spawn(props) server.Tell(&messages.Connect{ Sender: client, }) nick := "Roger" cons := console.NewConsole(func(text string) { server.Tell(&messages.SayRequest{ UserName: nick, Message: text, }) }) //write /nick NAME to change your chat username cons.Command("/nick", func(newNick string) { server.Tell(&messages.NickRequest{ OldUserName: nick, NewUserName: newNick, }) }) cons.Run() }
func main() { props := actor.FromInstance(&HelloActor{}) pid := actor.Spawn(props) pid.Tell(Hello{Who: "Roger"}) console.ReadLine() }