less

package module
v0.0.0-...-3d82958 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2022 License: MIT Imports: 2 Imported by: 1

README

Less

GoDoc Go Report Card

English | 中文

Introduction

Less is a light-weight and strong-extensibility network framework for Golang

Feature
  • Supports use different network library as transport layer(including Non-Blocking I/O network library,such as GnetNetpoll),and provides Golang TCP network by default
  • Provides common codec and supports customize message codec
  • Pipelined Middleware
  • flexible Router design
  • Provides Non-Copy API for reading and writing
  • Provides standard Logger interface
  • Integrates keepalive implementation
TODO
  • Client

Install

$ go get -u github.com/emove/less

Quick Start

package main

import (
	"context"
	"fmt"

	"github.com/emove/less"
	"github.com/emove/less/server"
)

func main() {
	// creates a less server
	// adds OnChannel hook and OnChannelClosed hook
	// adds a router
	srv := server.NewServer(":8080",
		server.WithOnChannel(OnChannelHook),
		server.WithOnChannelClosed(OnChannelClosedHook),
		server.WithRouter(router))

	// serving the network
	srv.Run()

	select {}
}

var IDGenerator uint32

type channelCtxKey struct{}

// ChannelContext custom channel context, used to identify channel
type ChannelContext struct {
	ID uint32
	Ch less.Channel
}

// OnChannelHook identifies each channel and print it
func OnChannelHook(ctx context.Context, ch less.Channel) (context.Context, error) {
	IDGenerator++
	fmt.Printf("new channel, id: %d, remote addr: %s\n", IDGenerator, ch.RemoteAddr().String())
	return context.WithValue(ctx, &channelCtxKey{}, &ChannelContext{ID: IDGenerator, Ch: ch}), nil
}

// OnChannelClosedHook prints channel id when channel closed
func OnChannelClosedHook(ctx context.Context, ch less.Channel, err error) {
	cc := ctx.Value(&channelCtxKey{}).(*ChannelContext)
	fmt.Printf("channel closed, id: %d, remote addr: %s ", cc.ID, ch.RemoteAddr().String())
	if err != nil {
		fmt.Printf("due to err: %v", err)
	}
	fmt.Println()
}

// router returns a handler to handle inbound message, it always return echoHandler
func router(ctx context.Context, channel less.Channel, msg interface{}) (less.Handler, error) {
	return echoHandler, nil
}

// echoHandler logic handler
func echoHandler(ctx context.Context, ch less.Channel, msg interface{}) error {
	cc := ctx.Value(&channelCtxKey{}).(*ChannelContext)
	fmt.Printf("receive msg from channel, id: %d, remote: %s, msg: %v\n", cc.ID, ch.RemoteAddr().String(), msg)
	return nil
}
More-Example

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Channel

type Channel interface {
	// Context returns custom context if set, or returns context.Background.
	Context() context.Context

	// RemoteAddr returns the remote network address, same as net.Conn#RemoteAddr.
	RemoteAddr() net.Addr

	// LocalAddr returns the local network address, same as net.Conn#LocalAddr.
	LocalAddr() net.Addr

	// Write writes the message to channel and fires outbound middleware.
	Write(msg interface{}) error

	// IsActive returns false only when the channel closed.
	IsActive() bool

	// Close closing the channel after inbound and outbound event done.
	Close(ctx context.Context, err error) error

	// CloseReader closes the channel reader then the channel is unable to receive any message.
	CloseReader()

	// CloseWriter close the channel writer then the channel is unable to send any message.
	CloseWriter()

	// Readable returns the channel readable or not
	Readable() bool

	// Writeable returns the channel writeable or not
	Writeable() bool

	// AddOnChannelClosed adds OnChannelClosed hooks for this channel.
	AddOnChannelClosed(onChannelClosed ...OnChannelClosed)

	// AddInboundMiddleware adds inbound Middleware for this channel.
	AddInboundMiddleware(mw ...Middleware)

	// AddOutboundMiddleware adds outbound Middleware for this channel.
	AddOutboundMiddleware(mw ...Middleware)
}

Channel defines the behaviors of channel.

type Handler

type Handler func(ctx context.Context, ch Channel, message interface{}) error

Handler defines the handler invoked by Middleware.

type Interceptor

type Interceptor func(message interface{}) bool

Interceptor defines the interceptor used to intercept message.

type Middleware

type Middleware func(handler Handler) Handler

Middleware is transport Middleware.

func Chain

func Chain(ms ...Middleware) Middleware

Chain returns a Middleware that specifies the chained handler for transport.

type OnChannel

type OnChannel func(ctx context.Context, ch Channel) (context.Context, error)

OnChannel is a hook which will be invoked when received a network connect request.

type OnChannelClosed

type OnChannelClosed func(ctx context.Context, ch Channel, err error)

OnChannelClosed is a hook which will be invoked when channel closed.

Jump to

Keyboard shortcuts

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