memhttp

package
v1.16.1 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2024 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Example
package main

import (
	"fmt"
	"io"
	"net/http"

	"connectrpc.com/connect/internal/memhttp"
)

func main() {
	hello := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
		_, _ = io.WriteString(w, "Hello, world!")
	})
	srv := memhttp.NewServer(hello)
	defer srv.Close()
	res, err := srv.Client().Get(srv.URL())
	if err != nil {
		panic(err)
	}
	defer res.Body.Close()
	fmt.Println(res.Status)
}
Output:

200 OK

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Option

type Option interface {
	// contains filtered or unexported methods
}

An Option configures a Server.

func WithCleanupTimeout

func WithCleanupTimeout(d time.Duration) Option

WithCleanupTimeout customizes the default five-second timeout for the server's Cleanup method.

func WithErrorLog

func WithErrorLog(l *log.Logger) Option

WithErrorLog sets http.Server.ErrorLog.

func WithOptions

func WithOptions(opts ...Option) Option

WithOptions composes multiple Options into one.

type Server

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

Server is a net/http server that uses in-memory pipes instead of TCP. By default, it supports http/2 via h2c. It otherwise uses the same configuration as the zero value of http.Server.

func NewServer

func NewServer(handler http.Handler, opts ...Option) *Server

NewServer creates a new Server that uses the given handler. Configuration options may be provided via [Option]s.

func (*Server) Cleanup

func (s *Server) Cleanup() error

Cleanup calls shutdown with a background context set with the cleanup timeout. The default timeout duration is 5 seconds.

func (*Server) Client

func (s *Server) Client() *http.Client

Client returns an http.Client configured to use in-memory pipes rather than TCP and speak HTTP/2. It is configured to use the same http2.Transport as [Transport].

Callers may reconfigure the returned client without affecting other clients.

Example
package main

import (
	"fmt"
	"io"
	"net/http"
	"time"

	"connectrpc.com/connect/internal/memhttp"
)

func main() {
	hello := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
		_, _ = io.WriteString(w, "Hello, world!")
	})
	srv := memhttp.NewServer(hello)
	defer srv.Close()
	client := srv.Client()
	client.Timeout = 10 * time.Second
	res, err := client.Get(srv.URL())
	if err != nil {
		panic(err)
	}
	defer res.Body.Close()
	fmt.Println(res.Status)
}
Output:

200 OK

func (*Server) Close

func (s *Server) Close() error

Close closes the server's listener. It does not wait for connections to finish.

func (*Server) RegisterOnShutdown

func (s *Server) RegisterOnShutdown(f func())

RegisterOnShutdown registers a function to call on Shutdown. See http.Server.RegisterOnShutdown for details.

func (*Server) Shutdown

func (s *Server) Shutdown(ctx context.Context) error

Shutdown gracefully shuts down the server, without interrupting any active connections. See http.Server.Shutdown for details.

Example
package main

import (
	"context"
	"fmt"
	"io"
	"net/http"
	"time"

	"connectrpc.com/connect/internal/memhttp"
)

func main() {
	hello := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
		_, _ = io.WriteString(w, "Hello, world!")
	})
	srv := memhttp.NewServer(hello)
	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer cancel()
	if err := srv.Shutdown(ctx); err != nil {
		panic(err)
	}
	fmt.Println("Server has shut down")
}
Output:

Server has shut down

func (*Server) Transport

func (s *Server) Transport() *http2.Transport

Transport returns a http2.Transport configured to use in-memory pipes rather than TCP and speak both HTTP/1.1 and HTTP/2.

Callers may reconfigure the returned transport without affecting other transports.

func (*Server) TransportHTTP1

func (s *Server) TransportHTTP1() *http.Transport

TransportHTTP1 returns a http.Transport configured to use in-memory pipes rather than TCP and speak HTTP/1.1.

Callers may reconfigure the returned transport without affecting other transports.

func (*Server) URL

func (s *Server) URL() string

URL returns the server's URL.

func (*Server) Wait

func (s *Server) Wait() error

Wait blocks until the server exits, then returns an error if not a http.ErrServerClosed error.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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