multiclient

package
v6.3.5 Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2021 License: Apache-2.0 Imports: 3 Imported by: 0

README

multiclient

Higher throughput client connection pool when transferring large messages (such as downloading files).

Feature

  • Non exclusive, shared connection pool
  • Making full use of the asynchronous communication advantages of each connection
  • Higher throughput when transferring large messages(≥1MB)
  • Preempt more network bandwidth in a shared network environment
  • Load balancing mechanism of traffic level
  • Real-time monitoring of connection status

Usage

import "github.com/henrylee2cn/erpc/v6/mixer/multiclient"

Test
package multiclient_test

import (
	"testing"
	"time"

	"github.com/henrylee2cn/erpc/v6"
	"github.com/henrylee2cn/erpc/v6/mixer/multiclient"
)

type Arg struct {
	A int
	B int `param:"<range:1:>"`
}

type P struct{ erpc.CallCtx }

func (p *P) Divide(arg *Arg) (int, *erpc.Status) {
	return arg.A / arg.B, nil
}

func TestMultiClient(t *testing.T) {
	srv := erpc.NewPeer(erpc.PeerConfig{
		ListenPort: 9090,
	})
	srv.RouteCall(new(P))
	go srv.ListenAndServe()
	time.Sleep(time.Second)

	cli := multiclient.New(
		erpc.NewPeer(erpc.PeerConfig{}),
		":9090",
		100,
		time.Second*5,
	)
	go func() {
		for {
			t.Logf("%+v", cli.Stats())
			time.Sleep(time.Millisecond * 500)
		}
	}()
	go func() {
		var result int
		for i := 0; ; i++ {
			stat := cli.Call("/p/divide", &Arg{
				A: i,
				B: 2,
			}, &result).Status()
			if !stat.OK() {
				t.Log(stat)
			} else {
				t.Logf("%d/2=%v", i, result)
			}
			time.Sleep(time.Millisecond * 500)
		}
	}()
	time.Sleep(time.Second * 6)
	cli.Close()
	time.Sleep(time.Second * 3)
}

test command:

go test -v -run=TestMultiClient

Documentation

Overview

Package multiclient is a higher throughput client connection pool when transferring large messages (such as downloading files).

Copyright 2018 HenryLee. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MultiClient

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

MultiClient client session which is has connection pool

func New

func New(peer erpc.Peer, addr string, sessMaxQuota int, sessMaxIdleDuration time.Duration, protoFunc ...erpc.ProtoFunc) *MultiClient

New creates a client session which is has connection pool.

func (*MultiClient) Addr

func (c *MultiClient) Addr() string

Addr returns the address.

func (*MultiClient) AsyncCall

func (c *MultiClient) AsyncCall(
	uri string,
	arg interface{},
	result interface{},
	callCmdChan chan<- erpc.CallCmd,
	setting ...erpc.MessageSetting,
) erpc.CallCmd

AsyncCall sends a message and receives reply asynchronously. If the arg is []byte or *[]byte type, it can automatically fill in the body codec name.

func (*MultiClient) Call

func (c *MultiClient) Call(uri string, arg interface{}, result interface{}, setting ...erpc.MessageSetting) erpc.CallCmd

Call sends a message and receives reply. NOTE: If the arg is []byte or *[]byte type, it can automatically fill in the body codec name; If the session is a client role and PeerConfig.RedialTimes>0, it is automatically re-called once after a failure.

func (*MultiClient) Close

func (c *MultiClient) Close()

Close closes the session.

func (*MultiClient) Peer

func (c *MultiClient) Peer() erpc.Peer

Peer returns the peer.

func (*MultiClient) Push

func (c *MultiClient) Push(uri string, arg interface{}, setting ...erpc.MessageSetting) *erpc.Status

Push sends a message, but do not receives reply. NOTE: If the arg is []byte or *[]byte type, it can automatically fill in the body codec name; If the session is a client role and PeerConfig.RedialTimes>0, it is automatically re-called once after a failure.

func (*MultiClient) Stats

func (c *MultiClient) Stats() pool.WorkshopStats

Stats returns the current session pool stats.

Jump to

Keyboard shortcuts

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