func connectAs(s string) *as.Client { h, port, err := utee.ParseUrl(s) utee.Chk(err) ac, err := as.NewClient(h, port) for err != nil { log.Println("as connect err:", err, " retry 2 sec later") time.Sleep(time.Second * time.Duration(2)) ac, err = as.NewClient(h, port) } return ac }
// Connect to the database, and initial setup func connectToDatabase(host string, port int) { var err error // connect to the db if db, err = as.NewClient(host, port); err != nil { panic(err) } readPolicy = as.NewPolicy() writePolicy = as.NewWritePolicy(0, 0) scanPolicy = as.NewScanPolicy() // index on offer_id of bids, so we can find bids on a given offer if _, err := db.CreateIndex(writePolicy, "test", BIDS, "idx:br:1", "offer_id", as.NUMERIC); err != nil { fmt.Printf("Create Index Failed: %s\n", err.Error()) } // index on broker_id of bids, so we can find bids by a particular broker if _, err := db.CreateIndex(writePolicy, "test", BIDS, "idx:br:2", "broker_id", as.NUMERIC); err != nil { fmt.Printf("Create Index Failed: %s\n", err.Error()) } // index on broker_id of offers, so we can find offers by a particular broker if _, err := db.CreateIndex(writePolicy, "test", OFFERS, "idx:br:3", "broker_id", as.NUMERIC); err != nil { fmt.Printf("Create Index Failed: %s\n", err.Error()) } // index on ticker of prices if _, err := db.CreateIndex(writePolicy, "test", PRICES, "idx:br:4", "ticker", as.STRING); err != nil { fmt.Printf("Create Index Failed: %s\n", err.Error()) } }
func (db *DB) Dial() { c, err := as.NewClient(db.host, db.port) if err != nil { log.Fatal(err) } db.client = c }
func main() { CheckArgs("<clone|log>", "<url>") action := os.Args[1] url := os.Args[2] // Aerospike client to be used by the custom storage client, err := driver.NewClient("127.0.0.1", 3000) CheckIfError(err) // New instance of the custom aerospike storage, all the objects, // references and configuration is saved to aerospike s, err := aerospike.NewStorage(client, "test", url) CheckIfError(err) // A new repository instance using as storage the custom implementation r, err := git.NewRepository(s) CheckIfError(err) switch action { case "clone": clone(r, url) case "log": log(r) default: panic("unknown option") } }
func main() { runtime.GOMAXPROCS(runtime.NumCPU()) // defer client.Close() client, err = as.NewClient("172.31.53.227", 3000) fmt.Println(client) panicOnError(err) writePolicy = as.NewWritePolicy(0, 0) ch = make(chan Event, 1) key, err := as.NewKey("test", "totals", "test") panicOnError(err) // define some bins with data bins := as.BinMap{ "value": 100, } err = client.Put(nil, key, bins) panicOnError(err) time.AfterFunc(1*time.Second, func() { for i := 1; i < 24; i++ { fmt.Println("starting worker %d", i) go worker(ch) } }) http.HandleFunc("/vote", voteHandler) http.HandleFunc("/loaderio-35df9c4fffde902e3b0e3e0115816d82.html", validationHandler) http.ListenAndServe(":80", nil) }
func main() { tpl = template.Must(template.ParseGlob("templates/*.gohtml")) f, err := os.OpenFile("logfile.txt", os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0664) if err != nil { panic(err) } defer f.Close() log.SetOutput(f) client, err = as.NewClient("127.0.0.1", 3000) if err != nil { panic(err) } defer client.Close() router := httprouter.New() router.GET("/", index) router.GET("/login", loginPage) router.POST("/login", login) router.GET("/logout", logout) router.GET("/create", createPage) router.POST("/create", create) err = http.ListenAndServe(":9000", router) if err != nil { panic(err) } }
func main() { // flags host := flag.String("h", "127.0.0.1", "host") port := flag.Int("p", 3000, "port") del := flag.Bool("del", false, "delete?") flag.Parse() // connect to Aerospike if h, found := os.LookupEnv("AEROSPIKE_PORT_3000_TCP_ADDR"); found { *host = h } cl, err := asc.NewClient(*host, *port) panicOnErr(err) key, err := asc.NewKey("test", "aerospike", "key") panicOnErr(err) // define some bins with data bins := asc.BinMap{ "bin1": 42, "bin2": "elephant", "bin3": []interface{}{"Go", 2009}, } // write the bins wp := asc.NewWritePolicy(0, 0) wp.SendKey = true // also send the key itself err = cl.Put(wp, key, bins) panicOnErr(err) rec, err := cl.Get(nil, key) panicOnErr(err) _, _ = pp.Printf("key: %v\nbins: %v\n", *rec.Key, rec.Bins) // scan all data rs, err := cl.ScanAll(nil, "test", "aerospike") panicOnErr(err) defer rs.Close() for r := range rs.Results() { _, _ = pp.Println(*r.Record.Key, r.Record.Bins) } if *del { existed, err := cl.Delete(nil, key) panicOnErr(err) fmt.Printf("Record existed before delete? %v\n", existed) } }
// NewAerospike returns a new Aerospike client. func NewAerospike() (as *aerospike.Client, err error) { var target string if AerospikeFromConsulFlag.Get() { for retry := 0; retry < 15; retry++ { target, _, err = scale.DereferenceService("aerospike") if err == nil { break } if err == scale.ErrServiceNotFound || strings.Contains(err.Error(), "network is unreachable") || strings.Contains(err.Error(), "no such host") { time.Sleep(1 * time.Second) continue } return nil, err } if err != nil { return nil, err } } else { target = AerospikeFixedAddrFlag.Get() } ipPort := strings.Split(target, ":") port, err := strconv.Atoi(ipPort[1]) if err != nil { return nil, err } for retry := 0; retry < 15; retry++ { as, err = aerospike.NewClient(ipPort[0], port) if err == nil { return as, nil } if strings.Contains(err.Error(), "Failed to connect") { time.Sleep(1 * time.Second) continue } return nil, err } return nil, err }
// reads input flags and interprets the complex ones func init() { // use all cpus in the system for concurrency runtime.GOMAXPROCS(runtime.NumCPU()) log.SetOutput(os.Stdout) flag.Parse() if *showUsage { flag.Usage() os.Exit(0) } printParams() var err error Client, err = as.NewClient(*Host, *Port) if err != nil { PanicOnError(err) } }
func (a *Aerospike) gatherServer(hostport string, acc telegraf.Accumulator) error { host, port, err := net.SplitHostPort(hostport) if err != nil { return err } iport, err := strconv.Atoi(port) if err != nil { iport = 3000 } c, err := as.NewClient(host, iport) if err != nil { return err } defer c.Close() nodes := c.GetNodes() for _, n := range nodes { tags := map[string]string{ "aerospike_host": hostport, } fields := map[string]interface{}{ "node_name": n.GetName(), } stats, err := as.RequestNodeStats(n) if err != nil { return err } for k, v := range stats { val, err := parseValue(v) if err == nil { fields[strings.Replace(k, "-", "_", -1)] = val } else { log.Printf("skipping aerospike field %v with int64 overflow", k) } } acc.AddFields("aerospike_node", fields, tags, time.Now()) info, err := as.RequestNodeInfo(n, "namespaces") if err != nil { return err } namespaces := strings.Split(info["namespaces"], ";") for _, namespace := range namespaces { nTags := map[string]string{ "aerospike_host": hostport, } nTags["namespace"] = namespace nFields := map[string]interface{}{ "node_name": n.GetName(), } info, err := as.RequestNodeInfo(n, "namespace/"+namespace) if err != nil { continue } stats := strings.Split(info["namespace/"+namespace], ";") for _, stat := range stats { parts := strings.Split(stat, "=") if len(parts) < 2 { continue } val, err := parseValue(parts[1]) if err == nil { nFields[strings.Replace(parts[0], "-", "_", -1)] = val } else { log.Printf("skipping aerospike field %v with int64 overflow", parts[0]) } } acc.AddFields("aerospike_namespace", nFields, nTags, time.Now()) } } return nil }
/** below are public functions */ func ConnectAS(pHost string, pPort int) { var err error client, err = as.NewClient(pHost, pPort) panicOnError(err) }
func NewClient(address string, port int, namespace string, set string) Client { asClient, err := as.NewClient(address, port) panicOnError(err) return &client{asClient: asClient, namespace: namespace, set: set} }