metered

package
v0.0.0-...-dbfbf28 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 13, 2019 License: MIT Imports: 13 Imported by: 0

README

Go Custom Web Server Demo

A demo on custom Go web server

GoDoc

Documentation

Overview

Package metered provides a metered web server which makes statistics on handled requests per minute.

Index

Examples

Constants

View Source
const (
	// StatURL is used for handling HTTP request statistic inquiry.
	StatURL = "/stat"

	// HistoryFilename is the Request History Filename
	HistoryFilename = "req.his"
)

Variables

This section is empty.

Functions

func NewWebServer

func NewWebServer(mux *http.ServeMux) *webServer

NewWebServer returns a metered web server given the http.ServeMux to handle web requests.

Example
package main

import (
	"fmt"
	"net/http"

	"github.com/antonyho/go-project-demo/pkg/webserver/metered"
)

func main() {
	mux := http.NewServeMux()
	mux.HandleFunc("/", func(resp http.ResponseWriter, req *http.Request) {
		fmt.Fprintf(resp, "hello")
	})
	meteredWebServer := metered.NewWebServer(mux)
	go meteredWebServer.Listen(":8080")
}
Output:

Types

type RequestInfo

type RequestInfo struct {
	URL  string `json:"url"`
	Time int64  `json:"request_time"`
}

RequestInfo stores the basic information of an HTTP request. URL is the request URL. Request time is the server's local time down to nano-second precision in Unix epoch.

type RequestInfoPool

type RequestInfoPool struct {
	// contains filtered or unexported fields
}

RequestInfoPool is a sychronised pool which stores RequestInfo. It is thread safe.

func NewRequestInfoPool

func NewRequestInfoPool() *RequestInfoPool

NewRequestInfoPool returns a RequestInfoPool.

func (*RequestInfoPool) Add

func (p *RequestInfoPool) Add(requestInfos ...RequestInfo)

Add any number of RequestInfo into the pool.

func (*RequestInfoPool) ClearRecord

func (p *RequestInfoPool) ClearRecord(before time.Time)

ClearRecord any RequestInfo from the pool which is earlier than before.

func (*RequestInfoPool) Count

func (p *RequestInfoPool) Count() int

Count the number of RequestInfo in the pool.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL