Code Monkey home page Code Monkey logo

pkg's Introduction

pkg

This project github.com/FlyingOnion/pkg provides some interesting go packages.

  • bytes provides a Buffer that could be used as a substitution for official bytes.Buffer, with a lot of chaining Write methods and If, ElseIf, Else, For and Break;
  • log is a radical logging package without any f methods; instead, it improves the efficiency of logging;
  • context provides context with multiple parents, ChainingContext and ResetContext;
  • sqlwrapper is a sql wrapper with some useful ORM features. See Readme-CN or Readme-EN for more details;

bytes

TL;DR

import "github.com/FlyingOnion/pkg/bytes"

func getAddr(useHttps bool, host string, port int) string {
    var b bytes.Buffer
    return b.
        If(useHttps).WriteString("https://").Else().WriteString("http://").End().
        WriteString(host).
        If(port != 80).WriteString(":").WriteInt(port).End().
        String()
}

context

ChainingContext is a powerful tool to control the time spent on each task.

ctx, cancel := context.WithFractions(parent, 0.3, 0.2, 0.2)
defer cancel()
doTask1(ctx.Next()) // use 30% of time to do task1
doTask2(ctx.Next()) // use 20% of time to do task2
doTask3(ctx.Next()) // use 20% of time to do task3
doTask4(ctx.Next()) // use a sentinel context to do task4

ResetContext includes a timer and it could be reset. If the time is up, the context will be canceled.

func process(conn net.Conn) {
    ctx, cancel := context.WithReset(5 * time.Second)
    defer cancel()
    go func() {
        <-ctx.Done()
        conn.Close()
    }()
    var buf bytes.Buffer
    for {
        n, err := io.Copy(&buf, conn)
        if err != nil {
            break
        }
        ctx.Reset(5 * time.Second)
        // do something
    }
}

// server
l, _ := net.Listen("tcp", ":8080")
defer l.Close()

for {
    conn, _ := l.Accept()
    process(conn)
}

pkg's People

Contributors

flyingonion avatar

Watchers

 avatar

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.