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 ... pa := idx % len(service.clients) switch name { case SERVICE_SNOWFLAKE: return proto.NewSnowflakeServiceClient(service.clients[pa].conn), nil case SERVICE_GEOIP: return proto.NewGeoIPServiceClient(service.clients[pa].conn), nil case SERVICE_WORDFILTER: return proto.NewWordFilterServiceClient(service.clients[pa].conn), nil case SERVICE_BGSAVE: return proto.NewBgSaveServiceClient(service.clients[pa].conn), nil case SERVICE_AUTH: return proto.NewAuthServiceClient(service.clients[pa].conn), nil case SERVICE_CHAT: return proto.NewChatServiceClient(service.clients[pa].conn), nil case SERVICE_GAME: return proto.NewGameServiceClient(service.clients[pa].conn), nil } return nil, ERROR_SERVICE_NOT_AVAILABLE }
func (p *service_pool) get_all_service(name ServiceType) (map[string]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 } // all services conns := make(map[string]interface{}) for _, v := range service.clients { k := v.key switch name { case SERVICE_SNOWFLAKE: conns[k] = proto.NewSnowflakeServiceClient(v.conn) case SERVICE_GEOIP: conns[k] = proto.NewGeoIPServiceClient(v.conn) case SERVICE_WORDFILTER: conns[k] = proto.NewWordFilterServiceClient(v.conn) case SERVICE_BGSAVE: conns[k] = proto.NewBgSaveServiceClient(v.conn) case SERVICE_AUTH: conns[k] = proto.NewAuthServiceClient(v.conn) case SERVICE_CHAT: conns[k] = proto.NewChatServiceClient(v.conn) case SERVICE_GAME: conns[k] = proto.NewGameServiceClient(v.conn) } } return conns, nil }
// 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(name ServiceType, id string) (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 } var conn *grpc.ClientConn fullpath := string(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 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 case SERVICE_AUTH: return proto.NewAuthServiceClient(conn), nil case SERVICE_CHAT: return proto.NewChatServiceClient(conn), nil case SERVICE_GAME: return proto.NewGameServiceClient(conn), nil } return nil, ERROR_SERVICE_NOT_AVAILABLE }