client

package
v0.0.0-...-9750751 Latest Latest
Warning

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

Go to latest
Published: Jun 21, 2024 License: Apache-2.0 Imports: 14 Imported by: 0

README

License

Juno Golang SDK

Sample Code

package main

import (
	"crypto/tls"
	"fmt"
	"time"

	"github.com/paypal/junodb/pkg/client"
	cal "github.com/paypal/junodb/pkg/logging/cal/config"
	"github.com/paypal/junodb/pkg/util"
)

// addr is a Juno server endpoint in the form "ip:port".
// getTLSConfig is a func to get *tls.Config.
func createClient(addr string, getTLSConfig func() *tls.Config) (client.IClient, error) {

	cfg := client.Config{
		Appname:           "example",
		Namespace:         "example_namespace",
		DefaultTimeToLive: 60, // seconds
		ConnectTimeout:    util.Duration{1000 * time.Millisecond},
		ResponseTimeout:   util.Duration{500 * time.Millisecond},
	}

	cfg.Server.Addr = addr
	cfg.Server.SSLEnabled = true // Set to true if addr has an SSL port.

	client, err := client.NewWithTLS(cfg, getTLSConfig)
	return client, err
}

// Show metadata.
func showInfo(ctx client.IContext) {
	fmt.Printf("v=%d ct=%d ttl=%d\n", ctx.GetVersion(), ctx.GetCreationTime(),
	    ctx.GetTimeToLive())
}

func basicAPI(cli client.IClient) {
	key := []byte("test_key")
	val := []byte("test_payload")
	ctx, err := cli.Create(key, val)
	if err != nil {
		// log error
	}

	// Update val slice before call Update
	ctx, err = cli.Update(key, val)
	if err == nil {
		showInfo(ctx)	
	} else if err != client.ErrNoKey {
		// log error
	}

	_, err = cli.Set(key, val)
	if err != nil {
		// log error
	}

	val, _, err = cli.Get(key)
	if err != nil && err != client.ErrNoKey {
		// log error
	}

	err = cli.Destroy(key)
	if err != nil {
		// log error
	}
}

// Extend TTL if the value of WithTTL is greater than the current.
func basicAPIwithTTL(cli client.IClient) {

	key := []byte("test_key")
	val := []byte("test_Payload")
	ctx, err := cli.Create(key, val, client.WithTTL(uint32(100)))
	if err == nil {
		showInfo(ctx)
	}
	
	// Update val slice before call Update
	ctx, err = cli.Update(key, val, client.WithTTL(uint32(150)))
	if err == nil {
		showInfo(ctx)
	}

	ctx, err = cli.Set(key, val, client.WithTTL(uint32(200)))
	if err == nil {
		showInfo(ctx)
	}
	
	val, ctx, err = cli.Get(key, client.WithTTL(uint32(500)))
	if err == nil {
		showInfo(ctx)
	}

	err = cli.Destroy(key)
	if err != nil {
		// log error
	}
}

// Test conditional update based on record version.
func condUpdate(cli client.IClient) error {
	key := []byte("new_key")
	val := []byte("new_payload")

	ctx, err := cli.Create(key, val)
	if err != nil {
		return err
	}

	ctx, err = cli.Update(key, val)
	if err != nil {
		return err
	}

	// Update succeeds if current record version is equal to ctx.GetVersion().
	// After the update, record version is incremented.
	_, err = cli.Update(key, val, client.WithCond(ctx))
	if err != nil {
		return err
	}

	// Expect ErrConditionViolation
	// because current record version is not equal to ctx.GetVersion().
	_, err = cli.Update(key, val, client.WithCond(ctx))
	if err != client.ErrConditionViolation {
		return err
	}

	err = cli.Destroy(key)
	if err != nil {
		return err
	}
	return nil
}

func main() {
	// Init variables
	var addr string                      // = ...
	var getTLSConfig func() *tls.Config  // = ...

	// A client object should be created only once per unique addr.
	cli, err := createClient(addr, getTLSConfig)
	if err != nil {
		// log error
		return
	}

	basicAPI(cli)
	basicAPIwithTTL(cli)
	if err := condUpdate(cli); err != nil {
		// log error
	}
}

Documentation

Overview

package client implements Juno client API.

Possible returned errors.

Common Errors
* nil
* ErrBadMsg
* ErrBadParam
* ErrBusy
* ErrConnect
* ErrInternal
* ErrNoStorage
* ErrResponseTimeout

Create
* Common Errors
* ErrRecordLocked
* ErrWriteFailure
* ErrUniqueKeyViolation

Get
* Common Errors
* ErrNoKey  // Normal if key has not been created or has expired.
* ErrTTLExtendFailure

Update
* Common Errors
* ErrConditionViolation
* ErrRecordLocked
* ErrWriteFailure

Set
* Common Errors
* ErrRecordLocked
* ErrWriteFailure

Destroy
* Common Errors
* ErrRecordLocked
* ErrWriteFailure

Package client provides interfaces and implementations for communicating with a Juno server.

Package client handles the configuration for a Juno client.

client is a package that handles various error situations in the Juno application.

Package client provides functionalities for client configurations.

Example (Config)
package main

import ()

func main() {
}
Output:

Example (NewClient)
package main

import (
	"fmt"

	"github.com/paypal/junodb/pkg/client"
)

func main() {
	// create a Juno client talking to 127.0.0.1:8080 with
	//   namespace: exampleNS, and
	//   applicaiton name: exampleApp
	if cli, err := client.NewClient("127.0.0.1:8080", "exampleNS", "exampleApp"); err == nil {
		cli.Get([]byte("aKey"))
	} else {
		fmt.Println(err)
	}
}
Output:

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrConnect         error
	ErrResponseTimeout error

	ErrNoKey              error
	ErrUniqueKeyViolation error
	ErrBadParam           error
	ErrConditionViolation error

	ErrBadMsg           error // Error when a bad message is encountered.
	ErrNoStorage        error // Error when no storage is available.
	ErrRecordLocked     error // Error when a record is locked.
	ErrTTLExtendFailure error // Error when TTL extension fails.
	ErrBusy             error // Error when the server is busy.

	ErrWriteFailure   error // Error when a write operation fails.
	ErrInternal       error // Error when an internal problem occurs.
	ErrOpNotSupported error // Error when the operation is not supported.
)

Error variables for different scenarios in the application.

Functions

This section is empty.

Types

type Config

type Config struct {
	Server    io.ServiceEndpoint
	Appname   string
	Namespace string

	DefaultTimeToLive int
	ConnPoolSize      int
	ConnectTimeout    Duration
	ResponseTimeout   Duration
	BypassLTM         bool
	Cal               cal.Config
}

Config holds the configuration values for the Juno client.

type Duration

type Duration = util.Duration

Duration is a type alias for util.Duration.

type IClient

type IClient interface {
	Create(key []byte, value []byte, opts ...IOption) (IContext, error)
	Get(key []byte, opts ...IOption) ([]byte, IContext, error)
	Update(key []byte, value []byte, opts ...IOption) (IContext, error)
	Set(key []byte, value []byte, opts ...IOption) (IContext, error)
	Destroy(key []byte, opts ...IOption) (err error)

	UDFGet(key []byte, fname []byte, params []byte, opts ...IOption) ([]byte, IContext, error)
	UDFSet(key []byte, fname []byte, params []byte, opts ...IOption) (IContext, error)
}

func New

func New(conf Config) (IClient, error)

func NewWithTLS

func NewWithTLS(conf Config, getTLSConfig func() *tls.Config) (IClient, error)

type IContext

type IContext interface {
	GetVersion() uint32
	GetCreationTime() uint32
	GetTimeToLive() uint32
	PrettyPrint(w io.Writer)
}

type IOption

type IOption func(data interface{})

func WithCond

func WithCond(context IContext) IOption

WithCond function returns an IOption that sets a context.

func WithCorrelationId

func WithCorrelationId(id string) IOption

WithCorrelationId function returns an IOption that sets a correlationId.

func WithTTL

func WithTTL(ttl uint32) IOption

WithTTL function returns an IOption that sets a TTL value.

Jump to

Keyboard shortcuts

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