Example #1
0
func (a *Admin) Stop(in skynet.StopRequest) (out skynet.StopResponse, err error) {
	conn, err := net.Dial("tcp", a.Instance.Config.AdminAddr.String())
	if err != nil {
		return
	}
	rpcClient := bsonrpc.NewClient(conn)
	err = rpcClient.Call("Admin.Stop", in, &out)
	rpcClient.Close()
	return
}
Example #2
0
func doSomething(q *skylib.Query, do func(*rpc.Client, skylib.Logger)) {

	log := skylib.NewConsoleLogger(os.Stderr)
	for _, instance := range q.FindInstances() {
		conn, err := net.Dial("tcp", instance.Config.AdminAddr.String())
		if err != nil {
			log.Item(err)
			continue
		}
		rpcClient := bsonrpc.NewClient(conn)
		do(rpcClient, log)
		conn.Close()
	}
}
Example #3
0
func getConnectionFactory(s *skynet.ServiceInfo) (factory pools.Factory) {
	factory = func() (pools.Resource, error) {
		conn, err := net.Dial("tcp", s.Config.ServiceAddr.String())

		if err != nil {
			// TODO: handle failure here and attempt to connect to a different instance
			return nil, errors.New("Failed to connect to service: " + s.Config.ServiceAddr.String())
		}

		// get the service handshake
		var sh skynet.ServiceHandshake
		decoder := bsonrpc.NewDecoder(conn)

		err = decoder.Decode(&sh)
		if err != nil {
			conn.Close()
			return nil, err
		}

		ch := skynet.ClientHandshake{}
		encoder := bsonrpc.NewEncoder(conn)

		err = encoder.Encode(ch)
		if err != nil {
			conn.Close()
			return nil, err
		}

		if !sh.Registered {
			// this service has unregistered itself, look elsewhere
			conn.Close()
			return factory()
		}

		resource := ServiceResource{
			rpcClient: bsonrpc.NewClient(conn),
			service:   s,
			clientID:  sh.ClientID,
		}

		return resource, nil
	}
	return
}
Example #4
0
func (c *Client) GetServiceFromQuery(q *Query) (s *ServiceClient) {
	var conn net.Conn
	var err error

	s = &ServiceClient{
		Log:            c.Config.Log,
		connectionPool: pools.NewRoundRobin(c.Config.ConnectionPoolSize, c.Config.IdleTimeout),
		query:          q,
		instances:      make(map[string]service.Service, 0),
	}

	// Load initial list of instances
	results := s.query.FindInstances()

	if results != nil {
		for _, instance := range results {
			key := instance.Config.ServiceAddr.IPAddress + ":" + strconv.Itoa(instance.Config.ServiceAddr.Port)
			s.instances[key] = *instance
		}
	}

	go s.monitorInstances()

	var factory func() (pools.Resource, error)
	factory = func() (pools.Resource, error) {
		if len(s.instances) < 1 {

			return nil, errors.New("No services available that match your criteria")
		}

		var key string
		var instance service.Service

		// Connect to random instance
		index := (rand.Int() % len(s.instances))

		for k, v := range s.instances {
			if index == 0 {
				key = k
				instance = v
				break
			}
			index--
		}

		conn, err = net.Dial("tcp", instance.Config.ServiceAddr.String())

		if err != nil {
			// TODO: handle failure here and attempt to connect to a different instance
			return nil, errors.New("Failed to connect to service: " + instance.Config.ServiceAddr.String())
		}

		// get the service handshake
		var sh skynet.ServiceHandshake
		decoder := bsonrpc.NewDecoder(conn)
		err = decoder.Decode(&sh)
		if err != nil {
			conn.Close()
			return nil, err
		}

		ch := skynet.ClientHandshake{}
		encoder := bsonrpc.NewEncoder(conn)
		err = encoder.Encode(ch)
		if err != nil {
			conn.Close()
			return nil, err
		}

		if !sh.Registered {
			// this service has unregistered itself, look elsewhere
			conn.Close()
			delete(s.instances, key)
			return factory()
		}

		resource := ServiceResource{
			rpcClient: bsonrpc.NewClient(conn),
			service:   instance,
		}

		return resource, nil
	}

	s.connectionPool.Open(factory)

	return s
}