Ejemplo n.º 1
0
func main() {
	config, _ := skynet.GetClientConfig()

	var err error
	config.Log = skynet.NewConsoleLogger("TestServiceClient", os.Stderr)

	client := client.NewClient(config)

	// This will not fail if no services currently exist, as connections are created on demand
	// this saves from chicken and egg issues with dependencies between services
	service := client.GetService("TestService", "", "", "") // any version, any region, any host

	// This on the other hand will fail if it can't find a service to connect to
	in := map[string]interface{}{
		"data": "Upcase me!!",
	}
	out := map[string]interface{}{}
	err = service.Send(nil, "Upcase", in, &out)

	if err != nil {
		fmt.Println(err)
		return
	}

	fmt.Println(out["data"].(string))

}
Ejemplo n.º 2
0
func main() {
	config, _ := skynet.GetClientConfigFromFlags()
	client := client.NewClient(config)

	service := client.GetService("Sleeper", "", "", "")

	service.SetTimeout(1*time.Second, 10*time.Second)

	req := sleeper.Request{
		Message:  "Hello!",
		Duration: 5 * time.Second,
	}
	resp := sleeper.Response{}

	start := time.Now()

	err := service.Send(nil, "Sleep", req, &resp)

	duration := time.Now().Sub(start).Nanoseconds()

	fmt.Printf("request took %dns\n", duration)

	if err != nil {
		fmt.Println(err)
	} else {
		fmt.Printf("%s -> %s\n", req.Message, resp.Message)
	}
}
Ejemplo n.º 3
0
func main() {
	config, args := skynet.GetClientConfig()
	client := client.NewClient(config)

	service := client.GetService("Fibonacci", "", "", "")

	if len(args) == 0 {
		fmt.Printf("Usage: %s <positive number>*\n", args[0])
		return
	}

	for _, arg := range args[1:] {
		index, err := strconv.Atoi(arg)
		if err != nil {
			panic(err)
		}
		req := fibonacci.Request{
			Index: index,
		}
		resp := fibonacci.Response{}
		err = service.Send(nil, "Index", req, &resp)
		if err != nil {
			fmt.Println(err)
		} else {
			fmt.Printf("%d -> %d\n", index, resp.Value)
		}
	}
}
Ejemplo n.º 4
0
func main() {
	config := &skynet.ClientConfig{
		DoozerConfig: &skynet.DoozerConfig{},
	}

	flagset := flag.NewFlagSet("sleepclient", flag.ContinueOnError)

	skynet.FlagsForClient(config, flagset)

	req := sleeper.Request{
		Message: "Hello!",
	}

	var (
		retry  time.Duration
		giveup time.Duration
	)

	flagset.DurationVar(&req.Duration, "sleepfor", 5*time.Second, "how long to sleep")
	flagset.BoolVar(&req.ExitWhenDone, "exit", false, "have the service call os.Exit(0) when finished sleeping")
	flagset.BoolVar(&req.PanicWhenDone, "panic", false, "have the service panic when finished sleeping")
	flagset.BoolVar(&req.UnregisterWhenDone, "unregister", false, "have the service unregister when finished sleeping")
	flagset.BoolVar(&req.UnregisterHalfwayThrough, "unregister-halfway", false, "have the service unregister half-way through the sleep")
	flagset.DurationVar(&retry, "retry", time.Second, "how long to wait before trying again")
	flagset.DurationVar(&giveup, "giveup", 5*time.Second, "how long to wait before giving up")

	flagset.Parse(os.Args[1:])

	config.MaxConnectionsToInstance = 5

	client := client.NewClient(config)

	service := client.GetService("Sleeper", "", "", "")

	service.SetTimeout(retry, giveup)

	resp := sleeper.Response{}

	start := time.Now()

	err := service.Send(nil, "Sleep", req, &resp)

	duration := time.Now().Sub(start).Nanoseconds()

	fmt.Printf("request took %dns\n", duration)

	if err != nil {
		fmt.Println(err)
	} else {
		fmt.Printf("%s -> %s\n", req.Message, resp.Message)
	}
}
Ejemplo n.º 5
0
func NewFibonacci() (f *Fibonacci) {
	f = new(Fibonacci)

	f.cconfig, _ = skynet.GetClientConfigFromFlags()
	f.client = client.NewClient(f.cconfig)

	f.cache = map[int]chan uint64{
		0: make(chan uint64, 1),
		1: make(chan uint64, 1),
	}
	f.cache[0] <- 0
	f.cache[1] <- 1

	return
}
Ejemplo n.º 6
0
func getDaemonServiceClient(q *client.Query) (c *client.Client, service *client.ServiceClient) {
	config, _ := skynet.GetClientConfigFromFlags(os.Args...)

	config.Log = skynet.NewConsoleLogger(os.Stderr)

	c = client.NewClient(config)

	registered := true
	query := &client.Query{
		DoozerConn: c.DoozerConn,
		Service:    "SkynetDaemon",
		//Host:       "127.0.0.1",
		Registered: &registered,
	}
	service = c.GetServiceFromQuery(query)
	return
}
Ejemplo n.º 7
0
func getDaemonServiceClientForHost(dc *skynet.DoozerConfig, host string) *client.ServiceClient {
	config := &skynet.ClientConfig{
		DoozerConfig: dc,
	}

	c := client.NewClient(config)
	registered := true
	query := &skynet.Query{
		DoozerConn: c.DoozerConn,
		Service:    "SkynetDaemon",
		Host:       host,
		Registered: &registered,
	}

	s := c.GetServiceFromQuery(query)
	return s
}
Ejemplo n.º 8
0
func main() {
	config, _ := skynet.GetClientConfig()
	client := client.NewClient(config)

	service := client.GetService("TutorialService", "1", "Development", "")

	req := &TutorialRequest{
		Value: 1,
	}

	resp := &TutorialResponse{}

	err := service.Send(nil, "AddOne", req, resp)

	if err != nil {
		fmt.Println(err)
	} else {
		fmt.Println(resp.Value)
	}
}
Ejemplo n.º 9
0
func Stop(q *skynet.Query) {
	cl := client.NewClient(&config)

	for _, instance := range filterDaemon(q.FindInstances()) {
		cdaemon := daemon.GetDaemonForService(cl, instance)

		in := daemon.StopSubServiceRequest{UUID: instance.Config.UUID}
		out, err := cdaemon.StopSubService(in)

		if err != nil {
			if strings.HasPrefix(err.Error(), "No such service UUID") {
				// no daemon on the service's machine, shut it down directly
				AdminStop(q)
			} else {
				fmt.Println(err)
			}
		} else {
			stopTemplate.Execute(os.Stdout, out)
		}
	}
}
Ejemplo n.º 10
0
// TODO: this should be smarter about which hosts it deploys to
func Deploy(q *skynet.Query, path string, args ...string) {
	cl := client.NewClient(&config)

	fmt.Println("deploying " + path + " " + strings.Join(args, ""))

	for _, host := range q.FindHosts() {
		cdaemon := daemon.GetDaemonForHost(cl, host)

		in := daemon.DeployRequest{
			ServicePath: path,
			Args:        shellquote.Join(args...),
		}
		out, err := cdaemon.Deploy(in)

		if err != nil {
			fmt.Println(err)
			return
		}

		deployTemplate.Execute(os.Stdout, out)
	}
}
Ejemplo n.º 11
0
func main() {
	config, args := skynet.GetClientConfigFromFlags()
	client := client.NewClient(config)

	service := client.GetService("Fibonacci", "", "", "")

	for _, arg := range args[1:] {
		index, err := strconv.Atoi(arg)
		if err != nil {
			panic(err)
		}
		req := fibonacci.Request{
			Index: index,
		}
		resp := fibonacci.Response{}
		err = service.Send(nil, "Index", req, &resp)
		if err != nil {
			fmt.Println(err)
		} else {
			fmt.Printf("%d -> %d\n", index, resp.Value)
		}
	}
}
Ejemplo n.º 12
0
func Restart(q *skynet.Query) {
	cl := client.NewClient(&config)

	for _, instance := range filterDaemon(q.FindInstances()) {
		cdaemon := daemon.GetDaemonForService(cl, instance)

		in := daemon.RestartSubServiceRequest{UUID: instance.Config.UUID}
		out, err := cdaemon.RestartSubService(in)

		if err != nil {
			if strings.HasPrefix(err.Error(), "No such service UUID") {
				// Commented out for now, we need to determine if we want to try to restart an unmanaged instance, and support it
				// no daemon on the service's machine, shut it down directly
				//AdminStop(q)
			} else {
				fmt.Println(err)
			}
		} else {
			restartTemplate.Execute(os.Stdout, out)
		}
	}

}
Ejemplo n.º 13
0
func main() {
	flag.Parse()

	doozerConfig := &skynet.DoozerConfig{
		Uri:          *doozer,
		AutoDiscover: true,
	}

	clientConfig := &skynet.ClientConfig{
		DoozerConfig:       doozerConfig,
		ConnectionPoolSize: *requests,
		IdleTimeout:        (2 * time.Minute),
	}

	c := make(chan os.Signal, 1)
	quitChan := make(chan bool, 1)
	requestChan := make(chan string, *requests*3)
	workerQuitChan := make(chan bool, 1)
	workerWaitGroup := new(sync.WaitGroup)

	go watchSignals(c, quitChan)

	skynetClient := client.NewClient(clientConfig)
	testserviceClient = skynetClient.GetService("TestService", "", "", "")
	fibserviceClient = skynetClient.GetService("Fibonacci", "", "", "")

	startTime := time.Now().UnixNano()
	fmt.Printf("Starting %d Workers\n", *requests)
	for i := 0; i < *requests; i++ {
		go worker(requestChan, workerWaitGroup, workerQuitChan)
	}

	requestNum := 0

	for {
		select {
		case <-quitChan:
			for i := 0; i < *requests; i++ {
				workerQuitChan <- true
			}

			workerWaitGroup.Wait()
			stopTime := time.Now().UnixNano()

			successful, _ := strconv.Atoi(successfulRequests.String())
			total, _ := strconv.Atoi(totalRequests.String())

			failed := total - successful

			percentSuccess := int(float64(successful) / float64(total) * 100)
			percentFailed := int(float64(failed) / float64(total) * 100)

			runtime := (stopTime - startTime) / 1000000
			rqps := float64(total) / (float64(runtime) / 1000)

			fmt.Println("============================================================================")
			fmt.Printf("Completed in %d Milliseconds, %f Requests/s\n", runtime, rqps)
			fmt.Printf("\nTotal Requests: %d, Successful: %d (%d%%), Failed: %d (%d%%)\n\n", total, successful, percentSuccess, failed, percentFailed)
			return
		default:

			if requestNum%2 == 0 {
				requestChan <- "testservice"
			} else {
				requestChan <- "fibservice"
			}

			requestNum++
		}
	}
}