예제 #1
0
파일: client.go 프로젝트: yahame/skynet
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))

}
예제 #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)
	}
}
예제 #3
0
파일: fibclient.go 프로젝트: arjloo/skynet
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)
		}
	}
}
예제 #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)
	}
}
예제 #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
}
예제 #6
0
파일: remote.go 프로젝트: sdmajor/skynet
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
}
예제 #7
0
파일: admin.go 프로젝트: arjloo/skynet
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
}
예제 #8
0
파일: client.go 프로젝트: arjloo/skynet
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)
	}
}
예제 #9
0
파일: admin.go 프로젝트: arjloo/skynet
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)
		}
	}
}
예제 #10
0
파일: admin.go 프로젝트: arjloo/skynet
// 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)
	}
}
예제 #11
0
파일: fibclient.go 프로젝트: sdmajor/skynet
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)
		}
	}
}
예제 #12
0
파일: admin.go 프로젝트: arjloo/skynet
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)
		}
	}

}
예제 #13
0
파일: vagrant.go 프로젝트: sdmajor/skynet
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++
		}
	}
}