示例#1
0
func aboutPage(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)
	fmt.Fprintf(w, "<h1>%v</h1>", appengine.DefaultVersionHostname(c))

	token, expire, _ := appengine.AccessToken(c, "test")
	fmt.Fprintf(w, "<p>AccessToken: %v %v", token, expire)

	fmt.Fprintf(w, "<p>AppID: %v", appengine.AppID(c))
	fmt.Fprintf(w, "<p>FQAppID: %v", c.FullyQualifiedAppID())
	fmt.Fprintf(w, "<p>Go version: %v", runtime.Version())
	fmt.Fprintf(w, "<p>Datacenter: %v", appengine.Datacenter())
	fmt.Fprintf(w, "<p>InstanceID: %v", appengine.InstanceID())
	fmt.Fprintf(w, "<p>IsDevAppServer: %v", appengine.IsDevAppServer())
	fmt.Fprintf(w, "<p>RequestID: %v", appengine.RequestID(c))
	fmt.Fprintf(w, "<p>ServerSoftware: %v", appengine.ServerSoftware())

	sa, _ := appengine.ServiceAccount(c)
	fmt.Fprintf(w, "<p>ServiceAccount: %v", sa)

	keyname, signed, _ := appengine.SignBytes(c, []byte("test"))
	fmt.Fprintf(w, "<p>SignBytes: %v %v", keyname, signed)
	fmt.Fprintf(w, "<p>VersionID: %v", appengine.VersionID(c))

	fmt.Fprintf(w, "<p>Request: %v", r)
	r2 := c.Request()
	fmt.Fprintf(w, "<p>Context Request type/value: %T %v", r2, r2)
}
示例#2
0
文件: app.go 项目: markjen/gcptest
func handler(w http.ResponseWriter, r *http.Request) {
	start := time.Now()

	c := appengine.NewContext(r)

	requestCount := atomic.AddInt64(&requestCounter, 1)
	c.Infof("Request count: %d", requestCount)

	// Default delay value to 60 seconds
	var delay int64 = 60
	delayStr := r.FormValue("delay")
	if delayStr != "" {
		var err error
		delay, err = strconv.ParseInt(delayStr, 10, 64)
		if err != nil {
			c.Warningf("Bad delay request parameter: %s", delayStr)
		}
	}
	if delay < 0 {
		c.Warningf("Negative delay requested: %d, clamping to 0", delay)
		delay = 0
	}

	c.Infof("Waiting %d", delay)

	time.Sleep(time.Second * time.Duration(delay))
	finish := time.Now()

	workerExec := &model.WorkerExec{
		Started:       start,
		Finished:      finish,
		InstanceID:    appengine.InstanceID(),
		RequestNumber: requestCount}
	key, err := model.SaveWorkerExec(c, workerExec)
	if err != nil {
		c.Errorf("Could not save worker exec (%+v): %+v", workerExec, err)
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	c.Infof("Finished, writing data: %+v (ID: %s)", workerExec, key.StringID())

	w.WriteHeader(http.StatusOK)
	w.Write([]byte("Done!"))
}
示例#3
0
// Update DB - happens at a user request
// - could be done via a specific URL.
func update(c appengine.Context) {
	var err error
	c.Infof("Updating DB on instance " + appengine.InstanceID())
	client := createClient(c, time.Second*30)
	resp, err := client.Get(dbUrl)
	if err != nil {
		c.Warningf("Error downloading:%s", err.Error())
		return
	}
	defer resp.Body.Close()
	err = oui.Update(db, resp.Body)

	if err != nil {
		c.Warningf("Error parsing:%s", err.Error())
		return
	}
	t := time.Now().Add(time.Hour * 24)
	UpdateAt = &t
	c.Infof("Updated database...")
}
示例#4
0
// Inital loading of DB.
func start(c appengine.Context) error {
	var err error

	loadWait = sync.NewCond(&mu)
	c.Infof("Loading db on instance " + appengine.InstanceID())
	client := createClient(c, time.Second*30)
	resp, err := client.Get(dbUrl)
	if err != nil {
		c.Criticalf("Error downloading:%s", err.Error())
		return err
	}
	defer resp.Body.Close()
	db, err = oui.Open(resp.Body)

	if err != nil {
		c.Criticalf("Error parsing:%s", err.Error())
		return err
	}
	t := time.Now().Add(time.Hour * 24)
	UpdateAt = &t
	c.Infof("Loaded, now serving...")
	loadWait.Broadcast()
	return nil
}
示例#5
0
// Instance returns the app engine instance id, or the hostname on dev.
// This is the default for miniprofiler.MachineName.
func Instance() string {
	if i := appengine.InstanceID(); i != "" && !appengine.IsDevAppServer() {
		return i[len(i)-8:]
	}
	return miniprofiler.Hostname()
}
// Instance returns the app engine instance id, or the hostname on dev.
// This is the default for miniprofiler.MachineName.
func Instance() string {
	if i := appengine.InstanceID(); i != "" {
		return i[len(i)-8:]
	}
	return miniprofiler.Hostname()
}
示例#7
0
func InstanceID() string                       { return appengine.InstanceID() }
示例#8
0
func Get(w http.ResponseWriter, r *http.Request, m map[string]interface{}) *Instance {

	c := appengine.NewContext(r)

	startFunc := time.Now()

	if !ii.LastUpdated.IsZero() {

		age := startFunc.Sub(ii.LastUpdated)

		if age < 200*time.Millisecond {
			c.Infof("instance info update too recently: %v, skipping.\n", age)
			return ii
		}

		if age < 1*time.Hour {
			if len(ii.Hostname) > 2 {
				return ii
			}

		}

		c.Infof("instance info update too old: %v, recomputing.\n", age)
	}

	ii.ModuleName = appengine.ModuleName(c)
	ii.InstanceID = appengine.InstanceID()
	ii.VersionFull = appengine.VersionID(c)

	majorMinor := strings.Split(ii.VersionFull, ".")
	if len(majorMinor) != 2 {
		panic("we need a version string of format X.Y")
	}

	ii.VersionMajor = majorMinor[0]
	ii.VersionMinor = majorMinor[1]

	var err = errors.New("dummy creation error message")

	ii.NumInstances, err = module.NumInstances(c, ii.ModuleName, ii.VersionFull)
	if err != nil {
		// this never works with version full
		// we do not log this - but try version major
		err = nil

		if !util_appengine.IsLocalEnviron() {
			ii.NumInstances, err = module.NumInstances(c, ii.ModuleName, ii.VersionMajor)
			util_err.Err_http(w, r, err, true, "get num instances works only live and without autoscale")
		}

	}

	// in auto scaling, google reports "zero" - which can not be true
	// we assume at least 1
	if ii.NumInstances == 0 {
		ii.NumInstances = 1
	}

	// http://[0-2].1.default.libertarian-islands.appspot.com/instance-info

	ii.Hostname, err = appengine.ModuleHostname(c, ii.ModuleName,
		ii.VersionMajor, "")
	util_err.Err_http(w, r, err, false)

	if !util_appengine.IsLocalEnviron() {
		ii.HostnameInst0, err = appengine.ModuleHostname(c, ii.ModuleName,
			ii.VersionMajor, "0")
		if err != nil && (err.Error() == autoScalingErr1 || err.Error() == autoScalingErr2) {
			c.Infof("inst 0: " + autoScalingErrMsg)
			err = nil
		}
		util_err.Err_http(w, r, err, true)

		ii.HostnameInst1, err = appengine.ModuleHostname(c, ii.ModuleName,
			ii.VersionMajor, "1")
		if err != nil && (err.Error() == autoScalingErr1 || err.Error() == autoScalingErr2) {
			c.Infof("inst 1: " + autoScalingErrMsg)
			err = nil
		}
		util_err.Err_http(w, r, err, true)

		ii.HostnameMod02, err = appengine.ModuleHostname(c, "mod02", "", "")
		util_err.Err_http(w, r, err, true)

	}

	ii.LastUpdated = time.Now()

	c.Infof("collectInfo() completed, %v.%v.%v.%v, took %v",
		util.Ellipsoider(ii.InstanceID, 4), ii.VersionMajor, ii.ModuleName,
		ii.Hostname,
		ii.LastUpdated.Sub(startFunc))

	return ii
}
示例#9
0
文件: app.go 项目: markjen/gcptest
func handler(w http.ResponseWriter, r *http.Request) {
	start := time.Now()

	c := appengine.NewContext(r)
	c.Infof("Instance ID: %s", appengine.InstanceID())

	requestCount := atomic.AddInt64(&requestCounter, 1)
	c.Infof("Request count: %d", requestCount)

	// How much garbage to generate per cycle in MBs. Default to 16MB.
	var size int64 = 1 << 4
	sizeStr := r.FormValue("size")
	if sizeStr != "" {
		parsedSize, err := strconv.ParseInt(sizeStr, 10, 64)
		if err != nil {
			c.Errorf("Bad size request parameter: %s", sizeStr)
			w.WriteHeader(http.StatusBadRequest)
			return
		}
		if parsedSize < 0 {
			c.Errorf("Negative size requested: %d", parsedSize)
			w.WriteHeader(http.StatusBadRequest)
			return
		}
		size = parsedSize
	}

	// How much garbage to generate per cycle in KBs. Default to 128MB.
	loops := 3
	loopsStr := r.FormValue("loops")
	if loopsStr != "" {
		parsedLoops, err := strconv.ParseInt(loopsStr, 10, 64)
		if err != nil {
			c.Errorf("Bad loops request parameter: %s", loopsStr)
			w.WriteHeader(http.StatusBadRequest)
			return
		}
		if parsedLoops <= 0 {
			c.Errorf("Negative or zero loops requested: %d", parsedLoops)
			w.WriteHeader(http.StatusBadRequest)
			return
		}
		loops = int(parsedLoops)
	}

	m := new(runtime.MemStats)
	printMemStats := func(desc string) {
		runtime.ReadMemStats(m)
		c.Infof("Alloc: %d, TotalAlloc: %d, Sys: %d (%s)", m.Alloc, m.TotalAlloc, m.Sys, desc)
	}

	printMemStats("Before test, before GC")
	runtime.GC()
	printMemStats("Before test, after GC")

	var x []byte
	for i := 0; i < loops; i++ {
		printMemStats(fmt.Sprintf("Begin loop %d", i+1))
		x = make([]byte, size*1024*1024)
		c.Infof("x size: %d", len(x))
		printMemStats("Memory eaten")
		x = nil
		runtime.GC()
		printMemStats("Memory released and after GC")
	}

	c.Infof("Finished request in %s", time.Now().Sub(start))

	w.Write([]byte("ok"))
}
示例#10
0
文件: app.go 项目: markjen/gcptest
func ackHandler(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)
	c.Infof("Instance ID: %s", appengine.InstanceID())
}