Skip to content

Instantly share code, notes, and snippets.

@wirepair
Last active December 16, 2015 01:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wirepair/8cd9ffc3ee371a0d1749 to your computer and use it in GitHub Desktop.
Save wirepair/8cd9ffc3ee371a0d1749 to your computer and use it in GitHub Desktop.
chrome tab hang
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
)
var addr = ":9211"
// Defines the 'tab' or target for this chrome instance, can be multiple and background processes
// are included (not just visual tabs)
type TargetInfo struct {
Description string `json:"description"`
DevtoolsFrontendUrl string `json:"devtoolsFrontendUrl"`
FaviconUrl string `json:"faviconUrl"`
Id string `json:"id"`
Title string `json:"title"`
Type string `json:"type"`
Url string `json:"url"`
WebSocketDebuggerUrl string `json:"webSocketDebuggerUrl"`
}
func init() {
flag.StringVar(&addr, "addr", "localhost:9211", "host:port of a running chrome instance with --remote-debugging-port enabled")
}
func main() {
flag.Parse()
for i := 0; i < 10000; i++ {
t, err := NewTab()
if err != nil {
log.Fatalf("Error getting new tab: %s\n", err)
}
if err := CloseTab(t); err != nil {
log.Fatalf("error closing tab: %s\n", err)
}
}
}
func NewTab() (*TargetInfo, error) {
resp, err := http.Get(fmt.Sprintf("http://%s/json/new", addr))
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, errRead := ioutil.ReadAll(resp.Body)
if errRead != nil {
return nil, err
}
//log.Printf("got body: %s\n", body)
tabTarget := &TargetInfo{}
err = json.Unmarshal(body, &tabTarget)
if err != nil {
return nil, err
}
log.Printf("got target: %#v\n", tabTarget)
return tabTarget, nil
}
func CloseTab(target *TargetInfo) error {
fmt.Printf("closing tab: %s\n", target.Id)
resp, err := http.Get(fmt.Sprintf("http://%s/json/close/%s", addr, target.Id))
if err != nil {
return err
}
defer resp.Body.Close()
body, errRead := ioutil.ReadAll(resp.Body)
log.Printf("close body: %s\n", body)
return errRead
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment