Skip to content

Commit

Permalink
viper
Browse files Browse the repository at this point in the history
  • Loading branch information
daoyuly committed Mar 9, 2024
1 parent 5973037 commit e0d5052
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 0 deletions.
4 changes: 4 additions & 0 deletions doc/Viper.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# [viper](https://github.com/spf13/viper)
Viper是完整的go应用程序配置解决方案。
Viper is a complete configuration solution for Go application. It is designed to work within an application,
and can handle all types configuration needs and formats.
22 changes: 22 additions & 0 deletions profile/go/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package profile

import (
"log"
"math/big"
"time"
)

func Duration(invocation time.Time, name string) {
elapsed := time.Since(invocation)
log.Printf("%s lasted %s", name, elapsed)
}

func main() {
defer Duration(time.Now(), "p1")
x := big.NewInt(2)
y := big.NewInt(1)
for one := big.NewInt(1); x.Sign() > 0; x.Sub(x, one) {
y.Mul(x, y)
}
log.Println(x.Set(y))
}
18 changes: 18 additions & 0 deletions profile/js/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
(function demo(){
function profile() {
window.performance.mark('mark_1')
console.log(`window.performance.mark('mark_1')`, performance.getEntriesByName('mark_1'))

window.performance.mark('mark_2')
console.log(`window.performance.mark('mark_1')`, performance.getEntriesByName('mark_2'))

window.performance.mark('mark_1')
console.log(`window.performance.mark('mark_1')`, performance.getEntriesByName('mark_1'))

performance.getEntriesByName('mark_1')


}

profile()
})()
34 changes: 34 additions & 0 deletions proxy/go/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package proxy_pattern

import "fmt"

// IObject to use proxy and to object they must implement same methods
type IObject interface {
ObjDo(action string)
}

// Object represent real objects which proxy will delegate data
type Object struct {
action string
}

// ObjDo implement IObject interface and handel's all logic
func (obj *Object) ObjDo(action string) {
fmt.Printf("I am real Object ObjDo. I can, %s", action)
}

// ProxyObject represent proxy object with intercept action
type ProxyObject struct {
object *Object
}

// ObjDo are implement IObject and intercept action before send into real object.
func (proxy *ProxyObject) ObjDo(action string) {
if proxy != nil {
proxy.object = new(Object)
}
fmt.Printf("I am in proxy object. intercept %s", action)
if action == "Run" {
proxy.object.ObjDo(action)
}
}
1 change: 1 addition & 0 deletions proxy/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The proxy pattern provides an object that controls access to another object, intercepting all calls.
24 changes: 24 additions & 0 deletions shell/validate_ip.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env bash

IPADDR=$1

valid_ip () {
local ip=$1
local stat=1

if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
OIFS=$IFS
IFS='.'
ip=($ip)
IFS=$OIFS
[[ ${ip[0]} -le 255 && ${ip[1]} -le 255 \
&& ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
stat=$?
fi
return $stat
}

if ! valid_ip "${IPADDR}";then
echo "ip address error !!! please check '${IPADDR}' is ip address? "
exit 1
fi
58 changes: 58 additions & 0 deletions synchronization/semaphore_pattern/semaphore.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package semaphore_pattern

import (
"errors"
"os"
"time"
)

var (
ErrNoTickets = errors.New("semaphore: could not aquire semaphore")
ErrIllegalRelease = errors.New("semaphore: can't release the semaphore without acquiring it first")
)

type Interface interface {
Acquire() error
Release() error
}

type implementation struct {
sem chan struct{}
timeout time.Duration
}

func (s *implementation) Acquire() error {
select {
case s.sem <- struct{}{}:
return nil
case <-time.After(s.timeout):
return ErrNoTickets
}
}

func (s *implementation) Release() error {
select {
case _ = <-s.sem:
return nil
case <-time.After(s.timeout):
return ErrIllegalRelease
}
}

func NewSemaphore(tickets int, timeout time.Duration) Interface {
return &implementation{
sem: make(chan struct{}, tickets),
timeout: timeout,
}
}

func SemaphoreWithTimeouts() {
tickets, timeout := 0, 3*time.Second
s := NewSemaphore(tickets, timeout)
if err := s.Acquire(); err != nil {
if !errors.Is(err, ErrNoTickets) {
panic(err)
}
os.Exit(1)
}
}

0 comments on commit e0d5052

Please sign in to comment.