Esempio n. 1
0
func (p *service_pool) get_service(name ServiceType) (interface{}, error) {
	p.RLock()
	defer p.RUnlock()
	service := p.services[string(name)]
	if service == nil {
		return nil, ERROR_SERVICE_NOT_AVAILABLE
	}

	if len(service.clients) == 0 {
		return nil, ERROR_SERVICE_NOT_AVAILABLE
	}
	idx := int(atomic.AddUint32(&service.idx, 1))

	// add wrappers here ...
	switch name {
	case SERVICE_SNOWFLAKE:
		return proto.NewSnowflakeServiceClient(service.clients[idx%len(service.clients)].conn), nil
	case SERVICE_GEOIP:
		return proto.NewGeoIPServiceClient(service.clients[idx%len(service.clients)].conn), nil
	case SERVICE_WORDFILTER:
		return proto.NewWordFilterServiceClient(service.clients[idx%len(service.clients)].conn), nil
	case SERVICE_BGSAVE:
		return proto.NewBgSaveServiceClient(service.clients[idx%len(service.clients)].conn), nil
	}
	return nil, ERROR_SERVICE_NOT_AVAILABLE
}
Esempio n. 2
0
// provide a specific key for a service
// service must be stored like /backends/xxx_service/xxx_id
func (p *service_pool) get_service_with_id(service_name, id string) (interface{}, error) {
	p.RLock()
	defer p.RUnlock()
	name := DEFAULT_SERVICE_PATH + "/" + service_name
	service := p.services[name]
	if service == nil {
		return nil, ERROR_SERVICE_NOT_AVAILABLE
	}

	if len(service.clients) == 0 {
		return nil, ERROR_SERVICE_NOT_AVAILABLE
	}

	var conn *grpc.ClientConn
	fullpath := name + "/" + id
	for k := range service.clients {
		if service.clients[k].key == fullpath {
			conn = service.clients[k].conn
			break
		}
	}

	if conn == nil {
		return nil, ERROR_SERVICE_NOT_AVAILABLE
	}

	// add wrappers here ...
	switch ServiceType(name) {
	case SERVICE_SNOWFLAKE:
		return proto.NewSnowflakeServiceClient(conn), nil
	case SERVICE_GEOIP:
		return proto.NewGeoIPServiceClient(conn), nil
	case SERVICE_WORDFILTER:
		return proto.NewWordFilterServiceClient(conn), nil
	case SERVICE_BGSAVE:
		return proto.NewBgSaveServiceClient(conn), nil
	}
	return nil, ERROR_SERVICE_NOT_AVAILABLE
}