Giter Club home page Giter Club logo

dm-resolver's Introduction

dm-resolver

GoDoc codebeat badge Go Report Card codecov

dm-resolver is a light way resolver library based on IP lookup and inspired by the default gRPC resolver.

Usage

The library can be used as a resolver for the gRPC go client or can be used to keep up to date your HTTP connection pool up to date.

dm-resolver is also used here as a library in the h2-proxy to help to update the domain and refresh the http2 connection pool.

Motivation

Trying to balance gRPC traffic properly in Kubernetes I came across a problem with the gRPC DNS resolver + Docker Alpine image + Kubernetes.

The problem laid in the balancing after changes in the initial mapping of the IPs against the domain. the gRPC client was actually balancing correctly the request, however, at the time Kubernetes scale up the pods or assign new IP for some reason to the pods the DNS resolver never updated the connection state hence stopped to balance correctly the request.

For instance:

  • the initial lookup returned for the domain test.kubernetes 2 IPs (10.0.0.0 and 11.0.0.0)
  • the gRPC client started to work correctly and balance (round-robin algorithm) the request properly
  • after scaling up the new set of IPs was something like 3 IPs (10.0.0.0, 11.0.0.0 and 12.0.0.0)
  • the gRPC client never sent a new request to the new IP 12.0.0.0

Usage for gRPC

package main

import (
    dmresolver "github.com/cperez08/dm-resolver/pkg/resolver"
    "google.golang.org/grpc"
    "google.golang.org/grpc/balancer/roundrobin"
    "google.golang.org/grpc/metadata"
    "google.golang.org/grpc/resolver"
)

var (
	scheme      = "my-scheme-name"
	host        = "service-address.com"
	port        = "50051"
	refreshRate = time.Duration(15) // will be parsed in seconds
)

func main() { 
    address := fmt.Sprintf("%s:///%s", scheme, host+":"+port)
    conn, err := grpc.Dial(address, grpc.WithInsecure(), grpc.WithBalancerName(roundrobin.Name))
    if err != nil {
        log.Fatalf("did not connect: %v", err)
    }

    defer conn.Close()
    cli = mypkg.NewPkgServiceClient(conn)

    cli.Call(ctx, &myRequest{})

}

func init(){

    // scheme is the custom dns name
    // host is the ip or domain name
    // port number
    // true indicates if the resolver needs to watch for changes - only aplicable for domains
    // 50 indicates the value in seconds the resolver look for the changes in the domain.
    resolver.Register(dmresolver.NewDomainResolverBuilder(scheme, host, port, true, &refreshRate))
}

Usage outside gRPC

import "github.com/cperez08/dm-resolver/pkg/resolver"

func main(){

    listener := make(chan bool)
    // host that wants to be resolved (domain or IP)
    // port to connecto to
    // true indicates if the wants the resolver to watch for domain changes
    // time.Duration(50) in case the previous parameter is true, is the refresh rate (new domain lookup)
    refreshRate := time.Duration(50)
    // listener listen for changes in the IPs, in case there is no change in the initial set of ips nothing is triggered
    r = resolver.NewResolver(host, port, true, &refreshRate, listener)
    // StartResolver resolves the domain  the firstime and starts the domain watcher if enabled and if the address is not an IP
    r.StartResolver()

    // for knowing the current Addresses stored  by the resolver
    r.Addresses // return a list of string in the format host:port

    // for stopping the watcher
    r.Close()
}

Disclaimer: the issue commented above occurred on linux alpine and ubuntu bionic in Kubernetes

dm-resolver's People

Contributors

cperez08 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

vaddenz

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.