コード例 #1
0
func setupBenchServer() ([]string, error) {
	ch, err := testutils.NewServerChannel(testutils.NewOpts().
		SetServiceName(benchServerName).
		SetFramePool(tchannel.NewSyncFramePool()))
	if err != nil {
		return nil, err
	}
	fmt.Println(benchServerName, "started on", ch.PeerInfo().HostPort)

	server := thrift.NewServer(ch)
	server.Register(gen.NewTChanSecondServiceServer(benchSecondHandler{}))

	if !*useHyperbahn {
		return []string{ch.PeerInfo().HostPort}, nil
	}

	// Set up a Hyperbahn client and advertise it.
	nodes := strings.Split(*hyperbahnNodes, ",")
	config := hyperbahn.Configuration{InitialNodes: nodes}
	hc, err := hyperbahn.NewClient(ch, config, nil)
	if err := hc.Advertise(); err != nil {
		return nil, err
	}

	return nodes, nil
}
コード例 #2
0
ファイル: internal_server.go プロジェクト: uber/tchannel-go
// Advertise advertises with Hyperbahn.
func (s *internalServer) Advertise(hyperbahnHosts []string) error {
	config := hyperbahn.Configuration{InitialNodes: hyperbahnHosts}
	hc, err := hyperbahn.NewClient(s.ch, config, nil)
	if err != nil {
		panic("failed to setup Hyperbahn client: " + err.Error())
	}
	return hc.Advertise()
}
コード例 #3
0
ファイル: client.go プロジェクト: dansimau/ringpop-go
func main() {
	// Create a TChannel.
	ch, err := tchannel.NewChannel("keyvalue-client", nil)
	if err != nil {
		log.Fatalf("Failed to create tchannel: %v", err)
	}

	// Set up Hyperbahn client.
	config := hyperbahn.Configuration{InitialNodes: os.Args[1:]}
	if len(config.InitialNodes) == 0 {
		log.Fatalf("No Autobahn nodes to connect to given")
	}
	hyperbahn.NewClient(ch, config, nil)

	thriftClient := thrift.NewClient(ch, "keyvalue", nil)
	client := keyvalue.NewTChanKeyValueClient(thriftClient)
	adminClient := keyvalue.NewTChanAdminClient(thriftClient)

	// Read commands from the command line and execute them.
	scanner := bufio.NewScanner(os.Stdin)
	printHelp()
	fmt.Printf("> ")
	for scanner.Scan() {
		parts := strings.Split(scanner.Text(), " ")
		if parts[0] == "" {
			continue
		}
		switch parts[0] {
		case "help":
			printHelp()
		case "get":
			if len(parts) < 2 {
				printHelp()
				break
			}
			get(client, parts[1])
		case "set":
			if len(parts) < 3 {
				printHelp()
				break
			}
			set(client, parts[1], parts[2])
		case "user":
			if len(parts) < 2 {
				printHelp()
				break
			}
			curUser = parts[1]
		case "clearAll":
			clear(adminClient)
		default:
			log.Printf("Unsupported command %q\n", parts[0])
		}
		fmt.Print("> ")
	}
	scanner.Text()
}
コード例 #4
0
ファイル: hyperbahn_test.go プロジェクト: uber/tchannel-go
func newAdvertisedEchoServer(t *testing.T, name string, mockHB *mockhyperbahn.Mock, f func()) *tchannel.Channel {
	server := testutils.NewServer(t, &testutils.ChannelOpts{
		ServiceName: name,
	})
	testutils.RegisterEcho(server, f)

	hbClient, err := hyperbahn.NewClient(server, mockHB.Configuration(), nil)
	require.NoError(t, err, "Failed to set up Hyperbahn client")
	require.NoError(t, hbClient.Advertise(), "Advertise failed")

	return server
}
コード例 #5
0
// setupServer is the application code we are attempting to test.
func setupServer() error {
	ch, err := tchannel.NewChannel("myservice", nil)
	if err != nil {
		return err
	}

	if err := ch.ListenAndServe("127.0.0.1:0"); err != nil {
		return err
	}

	client, err := hyperbahn.NewClient(ch, config.hyperbahnConfig, nil)
	if err != nil {
		return err
	}

	return client.Advertise()
}
コード例 #6
0
ファイル: main.go プロジェクト: uber/tchannel-go
func main() {
	tchan, err := tchannel.NewChannel("go-echo-server", nil)
	if err != nil {
		log.Fatalf("Failed to create channel: %v", err)
	}

	listenIP, err := tchannel.ListenIP()
	if err != nil {
		log.Fatalf("Failed to get IP to listen on: %v", err)
	}

	l, err := net.Listen("tcp", listenIP.String()+":61543")
	if err != nil {
		log.Fatalf("Could not listen: %v", err)
	}
	log.Printf("Listening on %v", l.Addr())

	sc := tchan.GetSubChannel("go-echo-2")
	tchan.Register(raw.Wrap(handler{""}), "echo")
	sc.Register(raw.Wrap(handler{"subchannel:"}), "echo")
	tchan.Serve(l)

	if len(os.Args[1:]) == 0 {
		log.Fatalf("You must provide Hyperbahn nodes as arguments")
	}

	// advertise service with Hyperbahn.
	config := hyperbahn.Configuration{InitialNodes: os.Args[1:]}
	client, err := hyperbahn.NewClient(tchan, config, &hyperbahn.ClientOptions{
		Handler: eventHandler{},
		Timeout: time.Second,
	})
	if err != nil {
		log.Fatalf("hyperbahn.NewClient failed: %v", err)
	}
	if err := client.Advertise(sc); err != nil {
		log.Fatalf("Advertise failed: %v", err)
	}

	// Server will keep running till Ctrl-C.
	select {}
}
コード例 #7
0
ファイル: server.go プロジェクト: gosuper/tchannel-go
func main() {
	// Create a TChannel and register the Thrift handlers.
	ch, err := tchannel.NewChannel("keyvalue", nil)
	if err != nil {
		log.Fatalf("Failed to create tchannel: %v", err)
	}

	// Register both the KeyValue and Admin services.
	// We can register multiple Thrift services on a single Hyperbahn service.
	h := newKVHandler()
	server := thrift.NewServer(ch)
	server.Register(keyvalue.NewTChanKeyValueServer(h))
	server.Register(keyvalue.NewTChanAdminServer(h))
	pprof.Register(ch)

	// Listen for connections on the external interface so we can receive connections.
	ip, err := tchannel.ListenIP()
	if err != nil {
		log.Fatalf("Failed to find IP to Listen on: %v", err)
	}
	// We use port 0 which asks the OS to assign any available port.
	// Static port allocations are not necessary for services on Hyperbahn.
	ch.ListenAndServe(fmt.Sprintf("%v:%v", ip, 0))

	// Advertising registers this service instance with Hyperbahn so
	// that Hyperbahn can route requests for "keyvalue" to us.
	config := hyperbahn.Configuration{InitialNodes: os.Args[1:]}
	if len(config.InitialNodes) == 0 {
		log.Fatalf("No Autobahn nodes to advertise with")
	}
	client, err := hyperbahn.NewClient(ch, config, nil)
	if err != nil {
		log.Fatalf("hyperbahn.NewClient failed: %v", err)
	}
	if err := client.Advertise(); err != nil {
		log.Fatalf("Hyperbahn advertise failed: %v", err)
	}

	// The service is now started up, run it till we receive a ctrl-c.
	log.Printf("KeyValue service has started on %v", ch.PeerInfo().HostPort)
	select {}
}
コード例 #8
0
ファイル: tcheck.go プロジェクト: uber/tcheck
func main() {
	ch, err := tchannel.NewChannel("tcheck", nil)
	if err != nil {
		fmt.Println("failed to create tchannel:", err)
		os.Exit(1)
	}

	hostsFile := flag.String("hostsFile", "/etc/uber/hyperbahn/hosts.json", "hyperbahn hosts file")
	serviceName := flag.String("serviceName", "hyperbahn", "service name to check health of")
	peer := flag.String("peer", "", "peer to hit directly")

	flag.Parse()

	var config hyperbahn.Configuration
	if *peer != "" {
		config = hyperbahn.Configuration{InitialNodes: []string{*peer}}
	} else {
		config = hyperbahn.Configuration{InitialNodesFile: *hostsFile}
	}
	hyperbahn.NewClient(ch, config, nil)

	thriftClient := thrift.NewClient(ch, *serviceName, nil)
	client := meta.NewTChanMetaClient(thriftClient)

	ctx, cancel := thrift.NewContext(time.Second)
	defer cancel()

	val, err := client.Health(ctx)

	if err != nil {
		fmt.Printf("NOT OK %v\nError: %v\n", *serviceName, err)
		os.Exit(2)
	} else if val.Ok != true {
		fmt.Printf("NOT OK %v\n", *val.Message)
		os.Exit(3)
	} else {
		fmt.Printf("OK\n")
	}
}