MCPcopy
hub / github.com/dgraph-io/dgraph / GetDgraphClient

Function GetDgraphClient

x/x.go:1069–1141  ·  view source on GitHub ↗

GetDgraphClient creates a Dgraph client based on the following options in the configuration: --slash_grpc_endpoint specifies the grpc endpoint for slash. It takes precedence over --alpha and TLS --alpha specifies a comma separated list of endpoints to connect to --tls "ca-cert=; client-cert=; client

(conf *viper.Viper, login bool)

Source from the content-addressed store, hash-verified

1067// --retries specifies how many times we should retry the connection to each endpoint upon failures
1068// --user and --password specify the credentials we should use to login with the server
1069func GetDgraphClient(conf *viper.Viper, login bool) (*dgo.Dgraph, CloseFunc) {
1070 var alphas string
1071 if conf.GetString("slash_grpc_endpoint") != "" {
1072 alphas = conf.GetString("slash_grpc_endpoint")
1073 } else {
1074 alphas = conf.GetString("alpha")
1075 }
1076
1077 if len(alphas) == 0 {
1078 glog.Fatalf("The --alpha option must be set in order to connect to Dgraph")
1079 }
1080
1081 fmt.Printf("\nRunning transaction with dgraph endpoint: %v\n", alphas)
1082 tlsCfg, err := LoadClientTLSConfig(conf)
1083 Checkf(err, "While loading TLS configuration")
1084
1085 ds := strings.Split(alphas, ",")
1086 var conns []*grpc.ClientConn
1087 var clients []api.DgraphClient
1088
1089 retries := 1
1090 if conf.IsSet("retries") {
1091 retries = conf.GetInt("retries")
1092 if retries < 1 {
1093 retries = 1
1094 }
1095 }
1096
1097 dialOpts := []grpc.DialOption{}
1098 if conf.GetString("slash_grpc_endpoint") != "" && conf.IsSet("auth_token") {
1099 dialOpts = append(dialOpts, WithAuthorizationCredentials(conf.GetString("auth_token")))
1100 }
1101
1102 for _, d := range ds {
1103 var conn *grpc.ClientConn
1104 for range retries {
1105 conn, err = SetupConnection(d, tlsCfg, false, dialOpts...)
1106 if err == nil {
1107 break
1108 }
1109 fmt.Printf("While trying to setup connection: %v. Retrying...\n", err)
1110 time.Sleep(time.Second)
1111 }
1112 if conn == nil {
1113 Fatalf("Could not setup connection after %d retries", retries)
1114 }
1115
1116 conns = append(conns, conn)
1117 dc := api.NewDgraphClient(conn)
1118 clients = append(clients, dc)
1119 }
1120
1121 dg := dgo.NewDgraphClient(clients...)
1122 creds := z.NewSuperFlag(conf.GetString("creds"))
1123 user := creds.GetString("user")
1124 if login && len(user) > 0 {
1125 err = GetPassAndLogin(dg, &CredOpt{
1126 UserID: user,

Callers 7

runFunction · 0.92
runFunction · 0.92
upgradePersitentQueryFunction · 0.92
upgradeCORSFunction · 0.92
upgradeAclTypeNamesFunction · 0.92
upgradeACLRulesFunction · 0.92
getClientWithAdminCtxFunction · 0.92

Calls 9

LoadClientTLSConfigFunction · 0.85
CheckfFunction · 0.85
SetupConnectionFunction · 0.85
FatalfFunction · 0.85
GetPassAndLoginFunction · 0.85
FatalfMethod · 0.80
WarningfMethod · 0.80
CloseMethod · 0.65

Tested by

no test coverage detected