本文整理汇总了Golang中github.com/bitly/nsq/util.ApiRequest函数的典型用法代码示例。如果您正苦于以下问题:Golang ApiRequest函数的具体用法?Golang ApiRequest怎么用?Golang ApiRequest使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了ApiRequest函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: GetLookupdTopicChannels
// GetLookupdTopicChannels returns a []string containing a union of the channels
// from all the given lookupd for the given topic
func GetLookupdTopicChannels(topic string, lookupdHTTPAddrs []string) ([]string, error) {
success := false
allChannels := make([]string, 0)
var lock sync.Mutex
var wg sync.WaitGroup
for _, addr := range lookupdHTTPAddrs {
wg.Add(1)
endpoint := fmt.Sprintf("http://%s/channels?topic=%s", addr, url.QueryEscape(topic))
log.Printf("LOOKUPD: querying %s", endpoint)
go func(endpoint string) {
data, err := util.ApiRequest(endpoint)
lock.Lock()
defer lock.Unlock()
defer wg.Done()
if err != nil {
log.Printf("ERROR: lookupd %s - %s", endpoint, err.Error())
return
}
success = true
// {"data":{"channels":["test"]}}
channels, _ := data.Get("channels").StringArray()
allChannels = util.StringUnion(allChannels, channels)
}(endpoint)
}
wg.Wait()
sort.Strings(allChannels)
if success == false {
return nil, errors.New("unable to query any lookupd")
}
return allChannels, nil
}
示例2: deleteTopicHandler
func (s *httpServer) deleteTopicHandler(w http.ResponseWriter, req *http.Request) {
if req.Method != "POST" {
log.Printf("ERROR: invalid %s to POST only method", req.Method)
http.Error(w, "INVALID_REQUEST", 500)
return
}
reqParams := &util.PostParams{req}
topicName, err := reqParams.Get("topic")
if err != nil {
http.Error(w, "MISSING_ARG_TOPIC", 500)
return
}
rd, _ := reqParams.Get("rd")
if !strings.HasPrefix(rd, "/") {
rd = "/"
}
// for topic removal, you need to get all the producers *first*
producers := s.getProducers(topicName)
// remove the topic from all the lookupds
for _, addr := range s.context.nsqadmin.options.NSQLookupdHTTPAddresses {
endpoint := fmt.Sprintf("http://%s/delete_topic?topic=%s", addr, url.QueryEscape(topicName))
log.Printf("LOOKUPD: querying %s", endpoint)
_, err := util.ApiRequest(endpoint)
if err != nil {
log.Printf("ERROR: lookupd %s - %s", endpoint, err.Error())
continue
}
}
// now remove the topic from all the producers
for _, addr := range producers {
endpoint := fmt.Sprintf("http://%s/delete_topic?topic=%s", addr, url.QueryEscape(topicName))
log.Printf("NSQD: querying %s", endpoint)
_, err := util.ApiRequest(endpoint)
if err != nil {
log.Printf("ERROR: nsqd %s - %s", endpoint, err.Error())
continue
}
}
s.notifyAdminAction("delete_topic", topicName, "", "", req)
http.Redirect(w, req, rd, 302)
}
示例3: tombstoneTopicProducerHandler
func (s *httpServer) tombstoneTopicProducerHandler(w http.ResponseWriter, req *http.Request) {
if req.Method != "POST" {
log.Printf("ERROR: invalid %s to POST only method", req.Method)
http.Error(w, "INVALID_REQUEST", 500)
return
}
reqParams := &util.PostParams{req}
topicName, err := reqParams.Get("topic")
if err != nil {
http.Error(w, "MISSING_ARG_TOPIC", 500)
return
}
node, err := reqParams.Get("node")
if err != nil {
http.Error(w, "MISSING_ARG_NODE", 500)
return
}
rd, _ := reqParams.Get("rd")
if !strings.HasPrefix(rd, "/") {
rd = "/"
}
// tombstone the topic on all the lookupds
for _, addr := range s.context.nsqadmin.options.NSQLookupdHTTPAddresses {
endpoint := fmt.Sprintf("http://%s/tombstone_topic_producer?topic=%s&node=%s",
addr, url.QueryEscape(topicName), url.QueryEscape(node))
log.Printf("LOOKUPD: querying %s", endpoint)
_, err := util.ApiRequest(endpoint)
if err != nil {
log.Printf("ERROR: lookupd %s - %s", endpoint, err.Error())
}
}
// delete the topic on the producer
endpoint := fmt.Sprintf("http://%s/delete_topic?topic=%s", node, url.QueryEscape(topicName))
log.Printf("NSQD: querying %s", endpoint)
_, err = util.ApiRequest(endpoint)
if err != nil {
log.Printf("ERROR: nsqd %s - %s", endpoint, err.Error())
}
s.notifyAdminAction("tombstone_topic_producer", topicName, "", node, req)
http.Redirect(w, req, rd, 302)
}
示例4: GetNSQDTopics
// GetNSQDTopics returns a []string containing all the topics
// produced by the given nsqd
func GetNSQDTopics(nsqdHTTPAddrs []string) ([]string, error) {
topics := make([]string, 0)
var lock sync.Mutex
var wg sync.WaitGroup
success := false
for _, addr := range nsqdHTTPAddrs {
wg.Add(1)
endpoint := fmt.Sprintf("http://%s/stats?format=json", addr)
log.Printf("NSQD: querying %s", endpoint)
go func(endpoint string) {
data, err := util.ApiRequest(endpoint)
lock.Lock()
defer lock.Unlock()
defer wg.Done()
if err != nil {
log.Printf("ERROR: lookupd %s - %s", endpoint, err.Error())
return
}
success = true
topicList, _ := data.Get("topics").Array()
for i := range topicList {
topicInfo := data.Get("topics").GetIndex(i)
topics = util.StringAdd(topics, topicInfo.Get("topic_name").MustString())
}
}(endpoint)
}
wg.Wait()
sort.Strings(topics)
if success == false {
return nil, errors.New("unable to query any nsqd")
}
return topics, nil
}
示例5: emptyTopicHandler
func (s *httpServer) emptyTopicHandler(w http.ResponseWriter, req *http.Request) {
if req.Method != "POST" {
log.Printf("ERROR: invalid %s to POST only method", req.Method)
http.Error(w, "INVALID_REQUEST", 500)
return
}
reqParams := &util.PostParams{req}
topicName, err := reqParams.Get("topic")
if err != nil {
http.Error(w, "MISSING_ARG_TOPIC", 500)
return
}
producers := s.getProducers(topicName)
for _, addr := range producers {
endpoint := fmt.Sprintf("http://%s/empty_topic?topic=%s", addr, url.QueryEscape(topicName))
log.Printf("NSQD: calling %s", endpoint)
_, err := util.ApiRequest(endpoint)
if err != nil {
log.Printf("ERROR: nsqd %s - %s", endpoint, err.Error())
continue
}
}
s.notifyAdminAction("empty_topic", topicName, "", "", req)
http.Redirect(w, req, fmt.Sprintf("/topic/%s", url.QueryEscape(topicName)), 302)
}
示例6: TestTombstonedNodes
func TestTombstonedNodes(t *testing.T) {
log.SetOutput(ioutil.Discard)
defer log.SetOutput(os.Stdout)
tcpAddr, httpAddr, nsqlookupd := mustStartLookupd()
defer nsqlookupd.Exit()
nsqlookupd.inactiveProducerTimeout = 50 * time.Millisecond
lookupdHTTPAddrs := []string{fmt.Sprintf("%s", httpAddr)}
topicName := "inactive_nodes"
conn := mustConnectLookupd(t, tcpAddr)
identify(t, conn, "ip.address", 5000, 5555, "fake-version")
nsq.Register(topicName, "channel1").Write(conn)
_, err := nsq.ReadResponse(conn)
assert.Equal(t, err, nil)
producers, _ := lookuputil.GetLookupdProducers(lookupdHTTPAddrs)
assert.Equal(t, len(producers), 1)
assert.Equal(t, len(producers[0].Topics), 1)
assert.Equal(t, producers[0].Topics[0].Topic, topicName)
assert.Equal(t, producers[0].Topics[0].Tombstoned, false)
endpoint := fmt.Sprintf("http://%s/tombstone_topic_producer?topic=%s&node=%s", httpAddr, topicName, "ip.address:5555")
_, err = util.ApiRequest(endpoint)
assert.Equal(t, err, nil)
producers, _ = lookuputil.GetLookupdProducers(lookupdHTTPAddrs)
assert.Equal(t, len(producers), 1)
assert.Equal(t, len(producers[0].Topics), 1)
assert.Equal(t, producers[0].Topics[0].Topic, topicName)
assert.Equal(t, producers[0].Topics[0].Tombstoned, true)
}
示例7: pauseChannelHandler
func (s *httpServer) pauseChannelHandler(w http.ResponseWriter, req *http.Request) {
if req.Method != "POST" {
log.Printf("ERROR: invalid %s to POST only method", req.Method)
http.Error(w, "INVALID_REQUEST", 500)
return
}
reqParams := &util.PostParams{req}
topicName, channelName, err := util.GetTopicChannelArgs(reqParams)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
producers := s.getProducers(topicName)
for _, addr := range producers {
endpoint := fmt.Sprintf("http://%s%s?topic=%s&channel=%s",
addr, req.URL.Path, url.QueryEscape(topicName), url.QueryEscape(channelName))
log.Printf("NSQD: calling %s", endpoint)
_, err := util.ApiRequest(endpoint)
if err != nil {
log.Printf("ERROR: nsqd %s - %s", endpoint, err.Error())
continue
}
}
s.notifyAdminAction(strings.TrimLeft(req.URL.Path, "/"), topicName, channelName, "", req)
http.Redirect(w, req, fmt.Sprintf("/topic/%s/%s", url.QueryEscape(topicName), url.QueryEscape(channelName)), 302)
}
示例8: CreateTopicChannel
// CreateTopicChannel creates the given topic and channel on the provided
// nsqd instance
func CreateTopicChannel(topic, channel, nsqdAddr string) (err error) {
endpoint := fmt.Sprintf("http://%s/create_topic?topic=%s", nsqdAddr, url.QueryEscape(topic))
_, err = util.ApiRequest(endpoint)
if err != nil {
log.Printf("ERROR: Could not create topic %s: %s", topic, err)
return
}
endpoint = fmt.Sprintf("http://%s/create_channel?topic=%s&channel=%s", nsqdAddr, url.QueryEscape(topic), url.QueryEscape(channel))
_, err = util.ApiRequest(endpoint)
if err != nil {
log.Printf("ERROR: Could not create channel %s: %s", channel, err)
return
}
return
}
示例9: deleteChannelHandler
func (s *httpServer) deleteChannelHandler(w http.ResponseWriter, req *http.Request) {
if req.Method != "POST" {
log.Printf("ERROR: invalid %s to POST only method", req.Method)
http.Error(w, "INVALID_REQUEST", 500)
return
}
reqParams := &util.PostParams{req}
topicName, channelName, err := util.GetTopicChannelArgs(reqParams)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
rd, _ := reqParams.Get("rd")
if !strings.HasPrefix(rd, "/") {
rd = fmt.Sprintf("/topic/%s", url.QueryEscape(topicName))
}
for _, addr := range s.context.nsqadmin.options.NSQLookupdHTTPAddresses {
endpoint := fmt.Sprintf("http://%s/delete_channel?topic=%s&channel=%s",
addr, url.QueryEscape(topicName), url.QueryEscape(channelName))
log.Printf("LOOKUPD: querying %s", endpoint)
_, err := util.ApiRequest(endpoint)
if err != nil {
log.Printf("ERROR: lookupd %s - %s", endpoint, err.Error())
continue
}
}
producers := s.getProducers(topicName)
for _, addr := range producers {
endpoint := fmt.Sprintf("http://%s/delete_channel?topic=%s&channel=%s",
addr, url.QueryEscape(topicName), url.QueryEscape(channelName))
log.Printf("NSQD: querying %s", endpoint)
_, err := util.ApiRequest(endpoint)
if err != nil {
log.Printf("ERROR: nsqd %s - %s", endpoint, err.Error())
continue
}
}
s.notifyAdminAction("delete_channel", topicName, channelName, "", req)
http.Redirect(w, req, rd, 302)
}
示例10: TestTombstoneRecover
func TestTombstoneRecover(t *testing.T) {
log.SetOutput(ioutil.Discard)
defer log.SetOutput(os.Stdout)
options := NewNSQLookupdOptions()
options.TombstoneLifetime = 50 * time.Millisecond
tcpAddr, httpAddr, nsqlookupd := mustStartLookupd(options)
defer nsqlookupd.Exit()
topicName := "tombstone_recover"
topicName2 := topicName + "2"
conn := mustConnectLookupd(t, tcpAddr)
identify(t, conn, "ip.address", 5000, 5555, "fake-version")
nsq.Register(topicName, "channel1").WriteTo(conn)
_, err := nsq.ReadResponse(conn)
assert.Equal(t, err, nil)
nsq.Register(topicName2, "channel2").WriteTo(conn)
_, err = nsq.ReadResponse(conn)
assert.Equal(t, err, nil)
endpoint := fmt.Sprintf("http://%s/tombstone_topic_producer?topic=%s&node=%s", httpAddr, topicName, "ip.address:5555")
_, err = util.ApiRequest(endpoint)
assert.Equal(t, err, nil)
endpoint = fmt.Sprintf("http://%s/lookup?topic=%s", httpAddr, topicName)
data, err := util.ApiRequest(endpoint)
assert.Equal(t, err, nil)
producers, _ := data.Get("producers").Array()
assert.Equal(t, len(producers), 0)
endpoint = fmt.Sprintf("http://%s/lookup?topic=%s", httpAddr, topicName2)
data, err = util.ApiRequest(endpoint)
assert.Equal(t, err, nil)
producers, _ = data.Get("producers").Array()
assert.Equal(t, len(producers), 1)
time.Sleep(55 * time.Millisecond)
endpoint = fmt.Sprintf("http://%s/lookup?topic=%s", httpAddr, topicName)
data, err = util.ApiRequest(endpoint)
assert.Equal(t, err, nil)
producers, _ = data.Get("producers").Array()
assert.Equal(t, len(producers), 1)
}
示例11: DeleteTopic
// DeleteTopic deletes the given topic from the provided nsqlookupd instance
func DeleteTopic(topic, nsqlookupdHttpAddr string) (err error) {
endpoint := fmt.Sprintf("http://%s/delete_topic?topic=%s", nsqlookupdHttpAddr, url.QueryEscape(topic))
_, err = util.ApiRequest(endpoint)
if err != nil {
log.Printf("ERROR: Could not delete topic %s: %s", topic, err)
}
return
}
示例12: TestChannelUnregister
func TestChannelUnregister(t *testing.T) {
log.SetOutput(ioutil.Discard)
defer log.SetOutput(os.Stdout)
tcpAddr, httpAddr, nsqlookupd := mustStartLookupd(NewNSQLookupdOptions())
defer nsqlookupd.Exit()
topics := nsqlookupd.DB.FindRegistrations("topic", "*", "*")
assert.Equal(t, len(topics), 0)
topicName := "channel_unregister"
conn := mustConnectLookupd(t, tcpAddr)
tcpPort := 5000
httpPort := 5555
identify(t, conn, "ip.address", tcpPort, httpPort, "fake-version")
nsq.Register(topicName, "ch1").WriteTo(conn)
v, err := nsq.ReadResponse(conn)
assert.Equal(t, err, nil)
assert.Equal(t, v, []byte("OK"))
topics = nsqlookupd.DB.FindRegistrations("topic", topicName, "")
assert.Equal(t, len(topics), 1)
channels := nsqlookupd.DB.FindRegistrations("channel", topicName, "*")
assert.Equal(t, len(channels), 1)
nsq.UnRegister(topicName, "ch1").WriteTo(conn)
v, err = nsq.ReadResponse(conn)
assert.Equal(t, err, nil)
assert.Equal(t, v, []byte("OK"))
topics = nsqlookupd.DB.FindRegistrations("topic", topicName, "")
assert.Equal(t, len(topics), 1)
// we should still have mention of the topic even though there is no producer
// (ie. we haven't *deleted* the channel, just unregistered as a producer)
channels = nsqlookupd.DB.FindRegistrations("channel", topicName, "*")
assert.Equal(t, len(channels), 1)
endpoint := fmt.Sprintf("http://%s/lookup?topic=%s", httpAddr, topicName)
data, err := util.ApiRequest(endpoint)
assert.Equal(t, err, nil)
returnedProducers, err := data.Get("producers").Array()
assert.Equal(t, err, nil)
assert.Equal(t, len(returnedProducers), 1)
}
示例13: TestClientAttributes
func TestClientAttributes(t *testing.T) {
log.SetOutput(ioutil.Discard)
defer log.SetOutput(os.Stdout)
userAgent := "Test User Agent"
options := NewNSQDOptions()
options.Verbose = true
options.SnappyEnabled = true
tcpAddr, httpAddr, nsqd := mustStartNSQD(options)
defer nsqd.Exit()
conn, err := mustConnectNSQD(tcpAddr)
assert.Equal(t, err, nil)
data := identify(t, conn, map[string]interface{}{
"snappy": true,
"user_agent": userAgent,
}, nsq.FrameTypeResponse)
resp := struct {
Snappy bool `json:"snappy"`
UserAgent string `json:"user_agent"`
}{}
err = json.Unmarshal(data, &resp)
assert.Equal(t, err, nil)
assert.Equal(t, resp.Snappy, true)
r := snappystream.NewReader(conn, snappystream.SkipVerifyChecksum)
w := snappystream.NewWriter(conn)
topicName := "test_client_attributes" + strconv.Itoa(int(time.Now().Unix()))
sub(t, readWriter{r, w}, topicName, "ch")
// need to give nsqd a chance to sub the client
time.Sleep(50 * time.Millisecond)
testUrl := fmt.Sprintf("http://127.0.0.1:%d/stats?format=json", httpAddr.Port)
statsData, err := util.ApiRequest(testUrl)
if err != nil {
t.Fatalf(err.Error())
}
client := statsData.Get("topics").GetIndex(0).Get("channels").GetIndex(0).Get("clients").GetIndex(0)
assert.Equal(t, client.Get("user_agent").MustString(), userAgent)
assert.Equal(t, client.Get("snappy").MustBool(), true)
}
示例14: TestClientAttributes
func TestClientAttributes(t *testing.T) {
log.SetOutput(ioutil.Discard)
defer log.SetOutput(os.Stdout)
userAgent := "Test User Agent"
*verbose = true
options := NewNSQDOptions()
options.SnappyEnabled = true
tcpAddr, httpAddr, nsqd := mustStartNSQD(options)
defer nsqd.Exit()
conn, err := mustConnectNSQD(tcpAddr)
assert.Equal(t, err, nil)
data := identifyFeatureNegotiation(t, conn, map[string]interface{}{"snappy": true, "user_agent": userAgent})
r := struct {
Snappy bool `json:"snappy"`
UserAgent string `json:"user_agent"`
}{}
err = json.Unmarshal(data, &r)
assert.Equal(t, err, nil)
assert.Equal(t, r.Snappy, true)
compressConn := snappystream.NewReader(conn, snappystream.SkipVerifyChecksum)
w := snappystream.NewWriter(conn)
rw := readWriter{compressConn, w}
topicName := "test_client_attributes" + strconv.Itoa(int(time.Now().Unix()))
sub(t, rw, topicName, "ch")
err = nsq.Ready(1).Write(rw)
assert.Equal(t, err, nil)
testUrl := fmt.Sprintf("http://127.0.0.1:%d/stats?format=json", httpAddr.Port)
statsData, err := util.ApiRequest(testUrl)
if err != nil {
t.Fatalf(err.Error())
}
client := statsData.Get("topics").GetIndex(0).Get("channels").GetIndex(0).Get("clients").GetIndex(0)
assert.Equal(t, client.Get("user_agent").MustString(), userAgent)
assert.Equal(t, client.Get("snappy").MustBool(), true)
}
示例15: GetLookupdTopicProducers
// GetLookupdTopicProducers returns a []string of the broadcast_address:http_port of all the
// producers for a given topic by unioning the results returned from the given lookupd
func GetLookupdTopicProducers(topic string, lookupdHTTPAddrs []string) ([]string, error) {
success := false
allSources := make([]string, 0)
var lock sync.Mutex
var wg sync.WaitGroup
for _, addr := range lookupdHTTPAddrs {
wg.Add(1)
endpoint := fmt.Sprintf("http://%s/lookup?topic=%s", addr, url.QueryEscape(topic))
log.Printf("LOOKUPD: querying %s", endpoint)
go func(endpoint string) {
data, err := util.ApiRequest(endpoint)
lock.Lock()
defer lock.Unlock()
defer wg.Done()
if err != nil {
log.Printf("ERROR: lookupd %s - %s", endpoint, err.Error())
return
}
success = true
producers := data.Get("producers")
producersArray, _ := producers.Array()
for i := range producersArray {
producer := producers.GetIndex(i)
address := producer.Get("address").MustString() //TODO: remove for 1.0
broadcastAddress := producer.Get("broadcast_address").MustString()
if broadcastAddress == "" {
broadcastAddress = address
}
httpPort := producer.Get("http_port").MustInt()
key := fmt.Sprintf("%s:%d", broadcastAddress, httpPort)
allSources = util.StringAdd(allSources, key)
}
}(endpoint)
}
wg.Wait()
if success == false {
return nil, errors.New("unable to query any lookupd")
}
return allSources, nil
}