clientsession

package
v4.0.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Jul 27, 2018 License: Apache-2.0 Imports: 4 Imported by: 0

README

clientsession

Client session with a high efficient and load balanced connection pool.

Feature

  • Non exclusive, shared connection pool
  • Making full use of the asynchronous communication advantages of each connection
  • Load balancing mechanism of traffic level
  • Real-time monitoring of connection status

Usage

import "github.com/henrylee2cn/teleport/mixer/clientsession"

Test
package clientsession_test

import (
	"testing"
	"time"

	tp "github.com/henrylee2cn/teleport"
	"github.com/henrylee2cn/teleport/mixer/clientsession"
)

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

type P struct{ tp.CallCtx }

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

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

	cli := clientsession.New(
		tp.NewPeer(tp.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++ {
			rerr := cli.Call("/p/divide", &Arg{
				A: i,
				B: 2,
			}, &result).Rerror()
			if rerr != nil {
				t.Log(rerr)
			} 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=TestCliSession

Documentation

Overview

Package clientsession is client session with a high efficient and load balanced connection pool.

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 CliSession

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

CliSession client session which is has connection pool

func New

func New(peer tp.Peer, addr string, sessMaxQuota int, sessMaxIdleDuration time.Duration, protoFunc ...socket.ProtoFunc) *CliSession

New creates a client session which is has connection pool.

func (*CliSession) Addr

func (c *CliSession) Addr() string

Addr returns the address.

func (*CliSession) AsyncCall

func (c *CliSession) AsyncCall(
	uri string,
	arg interface{},
	result interface{},
	callCmdChan chan<- tp.CallCmd,
	setting ...socket.PacketSetting,
) tp.CallCmd

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

func (*CliSession) Call

func (c *CliSession) Call(uri string, arg interface{}, result interface{}, setting ...socket.PacketSetting) tp.CallCmd

Call sends a packet 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 (*CliSession) Close

func (c *CliSession) Close()

Close closes the session.

func (*CliSession) Peer

func (c *CliSession) Peer() tp.Peer

Peer returns the peer.

func (*CliSession) Push

func (c *CliSession) Push(uri string, arg interface{}, setting ...socket.PacketSetting) *tp.Rerror

Push sends a packet, 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 (*CliSession) Stats

func (c *CliSession) 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