httpprofiling

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2024 License: CC0-1.0 Imports: 11 Imported by: 0

README

httpprofiling

HTTP profiling package

Quick Start

Installation $ go get github.com/diki-haryadi/govega/profiling/httpprofiling

Usage

Options:

Option Description
WithReadTimeout Timeout for the server to read the request data.

Default: 5m
WithWriteTimeout Timeout for the server to write response.

Default: 5m
WithPort The port to start the http profiler.

Default: 8432
WithRouter Use custom router for http profiler
WithManualStart If set to true, http profiler will not start right away on init.
The caller will need to manually start the profiler by calling Start function

Example:

Simple init http profiler (unless there is specific usecase, this should be enough most of the time)

package main

import (
	"context"

	"github.com/diki-haryadi/govega/profiling/httpprofiling"
)

func main() {
	if _, err := httpprofiling.InitProfiler(); err != nil {
		panic(err)
	}

	// do stuff
}

If you need to gracefully shutdown the http profiler

package main

import (
	"context"
	"os"
	"os/signal"
	"syscall"

	"github.com/diki-haryadi/govega/log"
	"github.com/diki-haryadi/govega/profiling/httpprofiling"
)

func main() {
	profiler, err := httpprofiling.InitProfiler()
	if err != nil {
		panic(err)
	}

	// do stuff

	ch := make(chan os.Signal, 1)
	signal.Notify(ch, os.Interrupt, syscall.SIGTERM)

	<- ch

	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer cancel()

	if err := profiler.Stop(ctx); err != nil {
		log.WithError(err).Errorln("failed on stoping profiler")
	}
}

Manually start the http profiler

package main

import (
	"context"
	"os"
	"os/signal"
	"syscall"

	"github.com/diki-haryadi/govega/log"
	"github.com/diki-haryadi/govega/profiling/httpprofiling"
)

func main() {
	profiler, err := httpprofiling.InitProfiler(httpprofiling.WithManualStart(true))
	if err != nil {
		panic(err)
	}

	// do stuff

	if err := profiler.Start(); err != nil {
		panic(err)
	}

	// do another stuff

	ch := make(chan os.Signal, 1)
	signal.Notify(ch, os.Interrupt, syscall.SIGTERM)

	<- ch

	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer cancel()

	// if needed to gracefully stop the http profiler
	if err := profiler.Stop(ctx); err != nil {
		log.WithError(err).Errorln("failed on stoping profiler")
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Option

type Option func(c *config)

func WithManualStart

func WithManualStart(manualStart bool) Option

WithManualStart, indicate whether or not to automatically start the server on init If set to true, the caller need to manually start the server by calling `Start` function

Default: false

func WithPort

func WithPort(port int) Option

WithPort, set the http profiler port

Default: 8432

func WithReadTimeout

func WithReadTimeout(timeout time.Duration) Option

WithReadTimeout, set http profiler timeout to read the request.

Default: 5 minutes

func WithRouter

func WithRouter(r *router.MyRouter) Option

WithRouter, set the default router which is used by the http profiler

func WithWriteTimeout

func WithWriteTimeout(timeout time.Duration) Option

WithReadTimeout, set http profiler timeout to write the response.

Default: 5 minutes

type Profiler

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

func InitProfiler

func InitProfiler(opts ...Option) (*Profiler, error)

InitProfiler, init http profiler and automatically start the http profiler by default To prevent automatically start the http profiler server use `WithManualStart(true)`

func (*Profiler) Start

func (p *Profiler) Start() error

Start, start the http profiler server this function will not do anything if already started

func (*Profiler) Stop

func (p *Profiler) Stop(ctx context.Context) error

Stop, stop the http profiler server this function will not do anything if already stopped

Jump to

Keyboard shortcuts

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