func main() { c := rpcmq.NewClient("amqp://amqp_broker:5672", "rpc-queue", "rpc-client", "rpc-exchange", "fanout") if err := c.Init(); err != nil { log.Fatalf("Init: %v", err) } defer c.Shutdown() go func() { for { data := []byte("Hello gophers!") uuid, err := c.Call("toUpper", data, 0) if err != nil { log.Println("Call:", err) } log.Printf("Sent: toUpper(%v) (%v)\n", string(data), uuid) <-time.After(500 * time.Millisecond) } }() go func() { for r := range c.Results() { if r.Err != "" { log.Printf("Received error: %v (%v)", r.Err, r.UUID) continue } log.Printf("Received: %v (%v)\n", string(r.Data), r.UUID) } }() <-time.After(10 * time.Second) }
func main() { rpcmq.Log = log.New(os.Stderr, "client ", log.LstdFlags) c := rpcmq.NewClient("amqp://amqp_broker:5672", "rpc-queue", "rpc-client", "rpc-exchange", "direct") if err := c.Init(); err != nil { log.Fatalf("Init: %v", err) } defer c.Shutdown() rets := map[int]int{} for i := 0; i < iter; i++ { rets[i] = 0 } total := 0 for i := 0; i < iter; i++ { total += i } go func() { for i := 0; i < iter; i++ { var uuid string var err error data := []byte(strconv.Itoa(i)) for { uuid, err = c.Call("echo", data, 0) if err == nil { break } log.Println("Call:", err) time.Sleep(100 * time.Millisecond) } log.Printf("Sent: echo(%v) (%v)\n", string(data), uuid) time.Sleep(100 * time.Millisecond) } }() go func() { ctr := 0 for r := range c.Results() { if r.Err != "" { log.Printf("Received error: %v (%v)", r.Err, r.UUID) continue } n, err := strconv.Atoi(string(r.Data)) if err != nil { log.Fatalf("Atoi: %v", err) } log.Printf("Received: %v (%v)\n", string(r.Data), r.UUID) rets[n] = rets[n] + 1 ctr += n log.Println(rets) log.Printf("%v/%v", ctr, total) } }() select {} }
func (s *server) start() error { s.client = rpcmq.NewClient("amqp://"+s.config.Rpcmq.Host+":"+s.config.Rpcmq.Port, s.config.Rpcmq.MsgQueue, s.config.Rpcmq.ReplyQueue, s.config.Rpcmq.Exchange, s.config.Rpcmq.ExchangeType) s.client.DeliveryMode = rpcmq.Transient err := s.client.Init() if err != nil { log.Fatalf("Init rpcmq: %v", err) } defer s.client.Shutdown() s.supervisor = monmq.NewSupervisor("amqp://"+s.config.Monmq.Host+":"+s.config.Monmq.Port, s.config.Monmq.ReplyQueue, s.config.Monmq.Exchange) if err := s.supervisor.Init(); err != nil { log.Fatalf("Init monmq: %v", err) } defer s.supervisor.Shutdown() if s.config.DB.Type == "mysql" { dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4,utf8", s.config.DB.Username, s.config.DB.Password, s.config.DB.Host, s.config.DB.Port, s.config.DB.Name) s.pm = persistence.NewMySQLPersistenceManager(dsn) defer s.pm.Close() } else if s.config.DB.Type == "elasticsearch" { esUrl := fmt.Sprintf("http://%s:%s", s.config.DB.Host, s.config.DB.Port) s.pm = persistence.NewElasticPersistenceManager(esUrl, s.config.DB.Name) } else { log.Fatalf("Invalid DB Type %s. Check the config", s.config.DB.Type) } m := vestigo.NewRouter() logHandler := olog.NewLogHandler(s.logger, logLine) m.Post("/tasks", orujo.NewPipe( http.HandlerFunc(s.tasksHandler), orujo.M(logHandler)).ServeHTTP, ) m.Get("/status", orujo.NewPipe( http.HandlerFunc(s.getStatusHandler), orujo.M(logHandler)).ServeHTTP, ) m.Post("/status", orujo.NewPipe( http.HandlerFunc(s.setStatusHandler), orujo.M(logHandler)).ServeHTTP, ) m.Get("/ips/:ip", orujo.NewPipe( http.HandlerFunc(s.queryHandler), orujo.M(logHandler)).ServeHTTP, ) m.Get("/ips", orujo.NewPipe( http.HandlerFunc(s.queryHandler), orujo.M(logHandler)).ServeHTTP, ) fmt.Println("Listening on " + s.config.Local.Host + ":" + s.config.Local.Port + "...") log.Fatalln(http.ListenAndServe(s.config.Local.Host+":"+s.config.Local.Port, m)) return nil }
func (s *server) start() error { s.client = rpcmq.NewClient("amqp://"+s.config.Rpcmq.Host+":"+s.config.Rpcmq.Port, s.config.Rpcmq.MsgQueue, s.config.Rpcmq.ReplyQueue, s.config.Rpcmq.Exchange, s.config.Rpcmq.ExchangeType) s.client.DeliveryMode = rpcmq.Transient err := s.client.Init() if err != nil { log.Fatalf("Init rpcmq: %v", err) } defer s.client.Shutdown() s.supervisor = monmq.NewSupervisor("amqp://"+s.config.Monmq.Host+":"+s.config.Monmq.Port, s.config.Monmq.ReplyQueue, s.config.Monmq.Exchange) if err := s.supervisor.Init(); err != nil { log.Fatalf("Init monmq: %v", err) } defer s.supervisor.Shutdown() dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8", s.config.DB.Username, s.config.DB.Password, s.config.DB.Host, s.config.DB.Port, s.config.DB.Name) s.database, err = sql.Open("mysql", dsn) if err != nil { log.Fatal(err) } defer s.database.Close() _, err = s.database.Exec("CREATE TABLE IF NOT EXISTS banners (ip INT UNSIGNED, port INT UNSIGNED, service VARCHAR(50), content TEXT)") if err != nil { log.Fatal(err) } logHandler := olog.NewLogHandler(s.logger, logLine) http.Handle("/tasks", orujo.NewPipe( http.HandlerFunc(s.tasksHandler), orujo.M(logHandler)), ) http.Handle("/status", orujo.NewPipe( http.HandlerFunc(s.statusHandler), orujo.M(logHandler)), ) http.Handle("/ips", orujo.NewPipe( http.HandlerFunc(s.queryHandler), orujo.M(logHandler)), ) http.Handle("/ips/", orujo.NewPipe( http.HandlerFunc(s.queryHandler), orujo.M(logHandler)), ) fmt.Println("Listening on " + s.config.Local.Host + ":" + s.config.Local.Port + "...") log.Fatalln(http.ListenAndServe(s.config.Local.Host+":"+s.config.Local.Port, nil)) return nil }
func main() { certFile := os.Getenv("RPCMQ_CERT") keyFile := os.Getenv("RPCMQ_KEY") caFile := os.Getenv("RPCMQ_CA") cert, err := tls.LoadX509KeyPair(certFile, keyFile) if err != nil { log.Fatalf("LoadX509KeyPair: %v", err) } caCert, err := ioutil.ReadFile(caFile) if err != nil { log.Fatalf("ReadFile: %v", err) } caCertPool := x509.NewCertPool() caCertPool.AppendCertsFromPEM(caCert) tlsConfig := &tls.Config{ Certificates: []tls.Certificate{cert}, RootCAs: caCertPool, } c := rpcmq.NewClient("amqps://amqp_broker:5671", "rpc-queue", "rpc-client", "rpc-exchange", "direct") c.TLSConfig = tlsConfig if err := c.Init(); err != nil { log.Fatalf("Init: %v", err) } defer c.Shutdown() go func() { for { data := []byte("Hello gophers!") uuid, err := c.Call("toUpper", data, 0) if err != nil { log.Println("Call:", err) } log.Printf("Sent: toUpper(%v) (%v)\n", string(data), uuid) time.Sleep(500 * time.Millisecond) } }() go func() { for r := range c.Results() { if r.Err != "" { log.Printf("Received error: %v (%v)", r.Err, r.UUID) continue } log.Printf("Received: %v (%v)\n", string(r.Data), r.UUID) } }() time.Sleep(1 * time.Minute) }